{S.O.L.I.D}

Principles of Object-Oriented Design in TypeScript

S  -  Single Responsibility Principle      -  SRP

O  -  Open Closed Principle                -  OCP

L   -  Liskov Substitution Principle       -  LSP

I    -  Interface Segregation Principle   - ISP

D  -  Dependency Inversion Principle  - DIP

S.O.L.I.D. stands for:

# Single responsibility principle

A class should have one and only one reason to change, meaning that a class should only have one job to do.

# Open-closed Principle

Objects or entities should be open for extension, but closed for modification.

# Liskov substitution principle

Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.

every subclass/derived class should be substitutable for their base/parent class.

# Liskov substitution principle

class ShapesAreaCalculator {
  constructor(protected shapes: IShape[]) {}
  calc() {
    return this.shapes
      .map((shape) => shape.area())
      .reduce((total, area) => (total += area));
  }
}


class ShapesVolumeCalculator extends ShapesAreaCalculator{
    constructor(protected shapes: IShape[]) {
        super(shapes)
    }
    calc() {
        // ...
    }
}

# Interface segregation principle

A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.

 

# Dependency inversion principle

  • High-level modules shouldn't depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details.
    Details should depend on abstractions.

1

Single Responsibility Principle
- SRP -

2

Open Closed Principle
- OCP -

3

Liskov Substitution Principle
- LSP -

5

Dependency Inversion Principle

- DIP -

4

Interface Segregation Principle

- ISP -