This is the 19th day of my participation in the More text Challenge. For details, see more text Challenge

preface

It took me nearly a month to complete my study notes for 22 design patterns and one interpreter mode that I planned to cover later.

Personally, I think my design pattern series is more suitable for those who want to learn design patterns from scratch, because my development experience is not very rich, and the content does not involve actual development, just for reference.

If there is something wrong, I hope you can help me one or two. In order to have time to correct errors, I plan to post articles one by one and correct them immediately if there are problems.

What are the design patterns?

The essence of design patterns is the practical application of object-oriented design principles, the full understanding and application of class encapsulation, inheritance, polymorphism, class association and portfolio management.

The concept of design patterns has been introduced from architectural design. Design patterns summarize some of the scenarios and solutions that are often encountered in software development. Designing a software is like designing a house. If we want to build a tall building, we must consider how to build it from the foundation. If we just want to build a small bungalow, we can just build it.

In software design, function modules and technical framework are need to use design patterns, functional modules in general is a design pattern + algorithm, data structures, most of them are in the technical framework is applicable to various design patterns, so to learn design patterns, we were able to read the source code of open source framework, which can be applied in the development.

After I learn design patterns, my biggest feeling is, learning design patterns can help us improve the scalability of the system (can easily add new functionality), readability (programming specification, also learned with others, you don’t learn don’t understand, roll up), reusability, improve the use of the same code), reliability (new features will not affect the function of the old), Finally, the purpose of reducing coupling and improving cohesion is achieved.

Before I write the code is completely without thinking, I did not understand it is best to adopt the way of combination object to implement new features, overall be fixed after the code architecture, whenever there is a demand, I is to modify the original internal code, such development process very troublesome, if not for my code is very familiar with, others may be completely change is not, Even with a lot of annotations and very little reuse of objects, the system memory overhead increases.

“The best martial art is to forget all the moves.” It is the same with learning design patterns. When we learn how to apply and apply design patterns, we also learn how to use object-oriented language. These essence are the key to helping us improve the quality of code!

The 23 design patterns can be divided into three categories: creation pattern, structure pattern, and behavior pattern. These three design patterns apply the seven design principles from different perspectives.

In the creation pattern, the main concern is how the object is created and how the creation and use can be separated. For some classes to shoulder the responsibilities of creating a class, class objects users don’t need to care about the process of creating, just like we go to the mall to buy things, that we don’t need to know is how to produce goods, after all, is to use the goods we want to do, as to how it is produced manufacturers will need to focus on.

In structural pattern, the main concern is how to build the structure of classes or objects, which can be divided into class structure pattern and object structure pattern. The former is to build the structure of interfaces and classes through inheritance, and the latter is to build the structure of objects through combination or aggregation. The object structure pattern is more flexible than the class structure pattern because the object structure pattern satisfies the principle of composite reuse. In addition, the class structure model establishes the relationship between classes through inheritance, which is a bit like hard coding, so it is not too flexible, while the object structure model establishes the relationship between classes by creating the objects of the class, and the classes maintain their own encapsulation, and the degree of coupling is not too high. Like the phone case we all use, the phone case is used to protect the phone, if the phone case is integrated with the phone, it can indeed play a role in protecting the phone, but we want to change the phone case can only be changed through the repair shop, it is really troublesome.

In the model of behavior, the main concern is how to collaborate between classes or objects, divided into class behavior model and the object behavior model, the former is by the parent class has written method calls in a subclass of the abstract methods to complete the collaboration, the latter is by combining or aggregation object, then the operation entrusted to the object method to complete the collaboration. The object behavior pattern here satisfies the principle of composition reuse, so.. You know.

Scope purpose Creation pattern Structural mode Behavioral pattern
Class model The factory method (Class) adapter Template methods, interpreters
Object pattern The singleton prototype abstracts the factory builder The proxy (object) adapter bridge decorates the appearance element combination Policy command responsibility chain state observer mediator iterator visitor memo

Seven Design Principles

Speaking of the 23 design patterns, we have to say the following seven design principles. Design patterns are all implemented around the seven design principles. It can be said that learning the seven design principles well is half of learning the design patterns.

Seven Design Principles

Creation pattern

This pattern provides a way to create objects while hiding the creation logic.

The factory pattern contains the abstract factory pattern, the factory method pattern, and the simple factory pattern. I have seen some blogs that separate them, but I have put them all together here. The factory pattern and the singleton pattern have more content.

Factory pattern: Encapsulates how objects are created.

Singleton pattern: Certain classes are guaranteed to have only one object instance.

Builder pattern: Makes it easier to create an object.

Prototype pattern: Provides a way to copy an object.

The factory pattern

The factory pattern

The singleton pattern

The singleton pattern

Builder pattern

Builder pattern

The prototype pattern

The prototype pattern

Structural mode

This design pattern focuses on the composition of classes and objects.

Among them, the agent mode and enjoy the yuan mode are more complex. You can consider looking at other modes first and then look at these two. The big guy can look directly.

Adapter pattern: To make some classes conform to the interfaces now defined.

Bridge pattern: classes that implement dimensions using composite objects instead of inheritance.

Composite pattern: masks the differences between composite objects and individual objects.

Decorator pattern: Use composite objects instead of inheritance to extend class functionality.

Facade mode: Provides high-level interfaces and shields low-level operation logic.

Share meta pattern: Share and reuse objects.

Proxy mode: Agents are used to add functions unrelated to services.

Adapter mode

Adapter mode

The bridge model

The bridge model

Portfolio model

Portfolio model

Decorator pattern

Decorator pattern

Facade pattern

Facade pattern

The flyweight pattern

The flyweight pattern

The proxy pattern

The proxy pattern

Behavioral pattern

This design pattern focuses on communication between objects.

Chain of responsibility pattern: Divide the responsibilities of different dimensions into different classes and link them together through a chain.

Command pattern: Decouple requestor and implementer, encapsulating operations into commands.

Policy pattern: Encapsulate different algorithm policies and processing policies into classes.

The iterator pattern, which is commonly used for collection iterators

In the intermediary mode, change the network structure between objects to the star structure

Memo mode, which holds the state of an object

Observer pattern, publish subscribe pattern

State mode, different states call different objects to handle

A template pattern in which a parent class defines abstract methods that are implemented by a child class

Visitor patterns, where different visitors use objects in different ways.

Interpreter mode, more difficult, not yet learned

Chain of responsibility model

Command mode

Interpreter mode

Iterator pattern

Iterator pattern

The mediator model

Memorandum Mode

Memorandum Mode

Observer mode

The state pattern

The strategy pattern

The strategy pattern

Template pattern

Visitor pattern

Visitor pattern