Introduction to the
Design patterns are used to reuse code, make it easier for others to understand, and ensure code reliability
Design pattern types
model | including | describe | formula |
---|---|---|---|
Creation pattern (5) |
Factory Pattern Abstract Factory Pattern Singleton Pattern Builder Pattern Prototype Pattern |
These design patterns provide a way to hide the creation logic while creating objects, rather than instantiating objects directly using the new operator. This gives the program more flexibility in deciding which objects to create for a given instance. (Used to create objects) | Single extraction component (construction) factory |
Structural mode (7) |
Adapter Pattern Bridge Pattern Composite Pattern Decorator Pattern Facade Pattern Flyweight Pattern Proxy Pattern |
These design patterns focus on combinations of classes and objects. The concept of inheritance is used to combine interfaces and define how composite objects acquire new functionality. (Handles combinations of classes or objects) | Overseas Chinese (bridge) group members (yuan) wear accessories |
Behavioral pattern (11) |
Chain of Responsibility Pattern Command Pattern Interpreter Pattern Iterator Pattern Mediator Pattern Memento Pattern Observer Pattern State Pattern Strategy Pattern Template Pattern Visitor Pattern |
These design patterns are particularly concerned with communication between objects. (Describe how classes and objects interact and how responsibilities are assigned) | Observe (model) to (repeat) policy, order liberation (visit), quit (mediation) forget Taiwan (state) |
Detailed instructions
Six principles of design patterns
1. Open Close Principle
The open closed principle means: open for extensions, closed for modifications. When the program needs to be extended, you cannot modify the original code to achieve a hot plug effect. In short, to make the program extensible, easy to maintain and upgrade. To achieve this, we need to use interfaces and abstract classes, which will be discussed later in the concrete design.
2. Liskov Substitution Principle
Richter’s substitution principle is one of the basic principles of object-oriented design. Richter’s rule of substitution says that wherever a base class can appear, a subclass must appear. LSP is the cornerstone of inheritance reuse. The base class can be reused only when the derived class can replace the base class and the function of the software unit is not affected. The derived class can also add new behaviors on the base class. Richter’s substitution principle is a complement to the open – close principle. Abstract is the key step to realize the open and close principle, and the inheritance relationship between base class and subclass is the concrete realization of abstraction, so the Richter substitution principle is the specification of the concrete steps to realize abstraction.
3, Dependence Inversion Principle
This principle is the basis of the open closed principle, the concrete content: programming for interfaces that rely on abstraction rather than on concrete.
4. Interface Segregation Principle
This principle means that it is better to use multiple isolated interfaces than a single one. It also has another meaning: reducing coupling between classes. Thus, in fact, design mode is a software design idea that starts from large-scale software architecture and is easy to upgrade and maintain. It emphasizes reducing dependence and coupling.
5. Demeter Principle, also known as the Least Known Principle
The least known principle means that an entity should interact with other entities as little as possible, so that the functional modules of the system are relatively independent.
Composite Reuse Principle
The principle of composite reuse is to use composition/aggregation rather than inheritance whenever possible.