This is the third day of my participation in Gwen Challenge
What are design patterns?
Design pattern is a set of repeatedly used, most people know, cataloged code Design experience summary. Through abstraction, realization, decoupling to make the code has better scalability, reuse, and maintainability. You may not know design patterns, but you already use them.
Design pattern principles?
Open closed principle: In a nutshell, open for extension, closed for modification. It emphasizes the use of abstraction to build the framework, with the implementation of the extension of details, can improve the reusability and maintainability of the software system, to establish a stable and flexible system. For example, when the program is extended, the original code cannot be modified, but new functions can be added.
The single responsibility principle guides us to implement a single responsibility for classes, interfaces, and methods. To avoid modifying a method to affect the failure of another interface, two classes are used to achieve this decoupling, and demand changes do not affect each other. When designing interfaces, simplify and refine them. It is better to use multiple isolated interfaces rather than a single interface, and consider future business expansion.
Dependency inversion principle: dependency is a class-only dependency relationship, inversion means that both the upper level and the lower level modules depend on its abstraction; We want to program for interfaces, relying on abstractions rather than concrete ones; Dependency inversion can reduce the coupling between classes, improve the readability and maintainability of code, and reduce the risk of modification.
Richter’s substitution principle: We do not want to break the inheritance system, the substitution of a base class into its subclasses is not affected, the reverse is not true; So in the program, try to use the base class type to define the object, at run time to determine its subclass type, with subclass object to replace the parent class.
Demeter’s rule: an object should be as little as possible to keep the least understanding of other objects, so that the system function is relatively independent, as far as possible to reduce the degree of coupling between classes.
Principle of composite reuse: Try to use object combination (HAS-A)/ aggregation (Contanis-a) instead of inheritance relationship to achieve the purpose of software reuse and reduce the coupling degree.
Design pattern classification?
1. Create mode, a total of 5 kinds
Factory method pattern, Abstract Factory pattern, singleton pattern, Builder pattern, prototype pattern
2, structural mode, a total of 7
Adapter mode, decorator mode, appearance mode, bridge mode, Composite mode, share mode
3, behavioral patterns, a total of 11
Policy pattern, template method pattern, Observer pattern, iterator pattern, Chain of responsibility pattern, command pattern, memo pattern, state pattern, Visitor pattern, Mediator pattern, interpreter pattern