Design pattern is a set of summary of code design experiences that are repeatedly used, known to most people, classified and catalogued. The design pattern is used to make code reusable, make it easier for others to understand, and ensure code generation reliability. There is no doubt that the design model is a win-win solution for others and the system. The design model enables code compilation to be truly engineered. The design model is the foundation of software engineering, just like the building blocks. The rational use of the design pattern in the project can perfectly solve many problems. Each pattern has a corresponding principle in the present, each pattern describes a recurring problem around us and its core solution, which is also the reason for its wide application.
I. Classification of design patterns
In general, the design model is divided into three categories:
There are five creation modes: Factory method mode, abstract factory mode, Singleton mode, builder mode, and prototype mode.
There are seven structural modes: adapter mode, decorator mode, proxy mode, appearance mode, bridging mode, combination mode, and meta mode.
11 behavior models: rule mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, command mode, memorandum mode, state mode, visitor mode, intermediary mode, interpreter mode.
There are actually two types: Parallel hair mode and Thread Pool Mode. Use an image to describe it as a whole:
Ii. six principles of the design model
1. Open Close Principle)
The principle of opening/closing is to sayOpen to extension and disable Modification. When the program needs to be expanded, you cannot modify the original code to achieve a hot swapping effect. So in a word, it is easy to maintain and upgrade the program to make it more scalable. To achieve this, we need to use interfaces and abstract classes. We will mention this in the specific design.
2. liskov substitution principle)
Liskov substitution principle LSP is one of the basic principles of object-oriented design. In the Rys replacement principle, subclasses can certainly appear where any base class can appear. LSP is the cornerstone of inheritance reuse. Only when the primary class can replace the base class and the functions of the software unit are not affected can the base class be reused, the category class can also add new behaviors based on the base class. The Li's replacement principle is a supplement to the "open-closed" principle. The key step for implementing the "open-close" principle is abstraction. The inheritance relationship between the base class and sub-classes is the specific implementation of abstraction. Therefore, the Li's replacement principle is the standard for the specific steps to implement abstraction. -- From Baidu encyclopedia
3. Dependence inversion principle)
This is the basis of the open/closed principle. The specific content is: True interface programming, dependent on abstraction and not on specifics.
4. Interface segregation principle)
This principle means that using multiple isolated interfaces is better than using a single interface. We can see from this that the design mode is a software design idea, starting from the large-scale software architecture, for the convenience of upgrading and maintenance. So it appears many times above: reducing dependencies and reducing coupling.
5. Demeter Principle)
What is the principle of least understanding? That is to say, an entity should interact with other entities as little as possible so that the system function modules are relatively independent.
6. Composite Reuse Principle)
The principle is to use synthesis/aggregation as much as possible, rather than inheritance.
Design patterns: the basis for reusable Object-Oriented Software