Perspective of design patterns

A pattern is a general solution to a problem in a context.
There are some principles of design patterns you may refer to.

  • Single-Responsibility principle.
    One class, for one responsibility.
  • Liskov substitution principle.
    1.Where super class object can use, where sub-class object can substitute, transparently.
    2.Sub-class can extend super class, but should not override its non-abstract methods.
    3.When implementing abstract methods, the return condition should be more strict.
    4.Relax conditions when overloading super class methods.
  • Dependency inversion principle.
    1.Dependency should be on abstract object(abstract class, interface, super class), not concrete objects.
    2.Variable type tries to be abstract object or interface.
  • ** Interface segregation principle**.
    1.Simplify interface. Segregate the big one into appropriate small ones, to reduce redundant abstract methods.
    2.Customize the implementing class. Expose the necessary methods, hide the unnecessary ones.
    3.Reduce the interaction.
  • ** The law of Demeter **.
    Lower coupling. Keep distance with other classes.
  • Open to extension, closed to modification.
    1.Try not to modify your code that runs well.
    2.Try not to insert new code into the previous. If you need to add new functions, extend it with an independent module.

If you have difficulty with the 6 principles, you can also refer to the following words.

The more simple, the more beautiful.
The less, the better.
Hierarchical coding.
No need to make details public.
Easy to reuse.
Interface-oriented.
Once and only once.
You may need template.
Don't repeat yourself.
Lower coupling, higher cohesion.

你可能感兴趣的:(Perspective of design patterns)