Design Pattern

SOLID

S - Single Responsibility

  • one purpose one class

O - Open Close Principle

  • The modification of the class method is not encouraged for new feature

  • The creation of new class method or new child class is encouraged for new feature

L - Liskov Substitution

  • When designing your classes , It is needed to consider that any subclass can be used as a substitute for the base class without unexpected behavior.

  • If you have a base class Bird with a method fly(), and you create a subclass Penguin which also has a fly() method but throws an exception because penguins can't fly, you would be violating the Liskov Substitution Principle.

I - Interface segregation

  • the interface should consider the necessary class method, in order to make the class more focused and maintainable

D - Dependency injection

  • Loose coupled between the classes

Singleton

  • Single instance only for a class

  • Global configuration ( e.g: db connection, logger) is one of the use case

Factory

  • Act as a agent to building the target class

  • Suitable when the class constructor is non-important and duplicated, the method can help to hide the detail

Builder

  • Suitable to apply when there are lots of attribute in class

  • Easier to understand and read

Adaptor

  • Provide a convert class, to convert the format from a to b

Strategy

  • Applying dependency injection, A high order function/ class to make the low-level class be interchangable

Last updated

Was this helpful?