Introduction to Design Patterns
Design patterns represent best practices, usually adopted by experienced object-oriented software developers. Design patterns are solutions to general problems faced by software developers during the software development process. These solutions are summarized by many software developers after a considerable period of trial and error.
Design patterns are a set of repeatedly used, well-known, categorized, and cataloged summaries of code design experience. The purpose of using design patterns is to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns are win-win for oneself, others, and the system; design patterns make code compilation truly engineering, and design patterns are the cornerstone of software engineering, just like the bricks of a building. The reasonable use of design patterns in a project can perfectly solve many problems; each pattern has a corresponding principle in reality, and each pattern describes a problem that repeatedly occurs around us and the core solution to that problem, which is why design patterns can be widely applied.
What is GOF (short for Gang of Four)?
In 1994, four authors - Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides - jointly published a book named Design Patterns - Elements of Reusable Object-Oriented Software (Chinese translation: Design Patterns - Elements of Reusable Object-Oriented Software) , which first mentioned the concept of design patterns in software development.
The four authors are collectively called GOF (short for Gang of Four). The design patterns they proposed are mainly based on the following object-oriented design principles.
- Program to an interface, not to an implementation.
- Favor object composition over inheritance.
Use of Design Patterns
Two main uses of design patterns in software development.
A common platform for developers
Design patterns provide a standard terminology system, specific to particular scenarios. For example, the Singleton design pattern means using a single object, so all developers familiar with the Singleton design pattern can use a single object, and can tell each other in this way that the program uses the Singleton pattern.
Best practices
Design patterns have gone through a long period of development, and they provide the best solutions to general problems faced in the software development process. Learning these patterns helps inexperienced developers learn software design in a simple and quick way.
Types of design patterns
According to the reference book on design patterns Design Patterns - Elements of Reusable Object-Oriented Software (Chinese translation: Design Patterns - Elements of Reusable Object-Oriented Software) , there are 23 design patterns in total. These patterns can be divided into three categories: Creational Patterns, Structural Patterns, and Behavioral Patterns. Of course, we will also discuss another category of design patterns: J2EE design patterns.
| No. | Pattern & Description | Includes |
|---|---|---|
| 1 | Creational Patterns | |
| These design patterns provide a way to hide the creation logic while creating objects, instead of instantiating objects directly with the new operator. This makes the program more flexible when deciding which objects need to be created for a given instance. |
|
- Adapter pattern (Adapter Pattern)
- Bridge pattern (Bridge Pattern)
- Filter Pattern (Filter, Criteria Pattern)
- Composite pattern (Composite Pattern)
- Decorator pattern (Decorator Pattern)
- Facade pattern (Facade Pattern)
- Flyweight pattern (Flyweight Pattern)
- Proxy pattern (Proxy Pattern)
- Chain of Responsibility pattern (Chain of Responsibility Pattern)
- Command pattern (Command Pattern)
- Interpreter pattern (Interpreter Pattern)
- Iterator pattern (Iterator Pattern)
- Mediator pattern (Mediator Pattern)
- Memento pattern (Memento Pattern)
- Observer pattern (Observer Pattern)
- State pattern (State Pattern)
- Null Object pattern (Null Object Pattern)
- Strategy pattern (Strategy Pattern)
- Template Pattern
- Visitor pattern (Visitor Pattern)
- MVC Pattern (MVC Pattern)
- Business Delegate pattern (Business Delegate Pattern)
- Composite Entity pattern (Composite Entity Pattern)
- Data Access Object pattern (Data Access Object Pattern)
- Front Controller pattern (Front Controller Pattern)
- Intercepting Filter pattern (Intercepting Filter Pattern)
- Service Locator pattern (Service Locator Pattern)
- Transfer Object pattern (Transfer Object Pattern)
The following picture describes the relationships between design patterns as a whole:

Advantages of design patterns
- Provides a shared design vocabulary and concepts, enabling developers to communicate better and understand each other's design intentions.
- Provides proven solutions that can improve software maintainability, reusability, and flexibility.
- Promotes code reuse and avoids repeated design and implementation.
- By following design patterns, errors and problems in the system can be reduced and code quality improved.
The six principles of design patterns
1. Open Close Principle
The Open Close Principle means:open for extension, closed for modification. When the program needs to be extended, the original code cannot be modified, achieving a hot-swappable effect. In short, it is to make the program extensible, easy to maintain and upgrade. To achieve this effect, we need to use interfaces and abstract classes, which we will mention in the specific designs later.
2. Liskov Substitution Principle
The Liskov Substitution Principle is one of the basic principles of object-oriented design. It states that wherever a base class can appear, a subclass can definitely appear. LSP is the cornerstone of inheritance reuse; only when a derived class can replace the base class and the functionality of the software unit is not affected can the base class be truly reused, and the derived class can add new behaviors on the basis of the base class. The Liskov Substitution Principle is a supplement to the Open Close Principle. The key step in implementing the Open Close Principle is abstraction, and the inheritance relationship between base and derived classes is the concrete implementation of abstraction, so the Liskov Substitution Principle is a specification of the concrete steps for implementing abstraction.
3. Dependence Inversion Principle
This principle is the foundation of the Open Close Principle. Specifically: program to interfaces and depend on abstractions rather than on concretions.
4. Interface segregation principle (Interface Segregation Principle)
This principle means that using several isolated interfaces is better than using a single interface. It also has another meaning: reducing the degree of coupling between classes. Thus it can be seen that design patterns are actually a software design idea starting from large-scale software architecture, convenient for upgrading and maintenance; it emphasizes reducing dependency and reducing coupling.
5. Law of Demeter, also known as the Least Knowledge Principle (Demeter Principle)
The Least Knowledge Principle states that an entity should interact as little as possible with other entities, so that the functional modules of the system are relatively independent.
6. Composite reuse principle (Composite Reuse Principle)
The Composite Reuse Principle states: use composition/aggregation as much as possible rather than inheritance.