Introduction to Design Patterns

Design patterns represent the best practices, usually adopted by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers face during the software development process. These solutions have been summarized by many software developers through trial and error over a considerable period of time.

Design patterns are a set of repeatedly used, widely 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 a win-win for oneself, others, and the system. Design patterns make code compilation truly engineering-based, and design patterns are the cornerstone of software engineering, just like bricks and stones of a building. The reasonable use of design patterns in projects can perfectly solve many problems. Each pattern has a corresponding principle in reality. Each pattern describes a problem that repeatedly occurs around us, as well as the core solution to the problem. This is also the reason why design patterns can be widely applied.

What is GOF (full name: Gang of Four)?

In 1994 year, by Erich Gamma、Richard Helm、Ralph Johnson and John Vlissides four人combine著出editionalreadyonebooknameisDesign Patterns - Elements of Reusable Object-Oriented Software(ChinesetranslateFirst name:Design Patterns - ReusableofObject-Oriented软件element)the book, which was the first to mention the concept of design patterns in software development.

Gang of FourGOF (full spelling: 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 that all developers familiar with the Singleton design pattern can use a single object, and in this way can tell others that the program uses the Singleton pattern.

Best practice

Design patterns have undergone a long period of development, and they provide the best solutions to general problems faced in the software development process. Learning these patterns helps less experienced developers learn software design in a simple and quick way.

Types of Design Patterns

According to the design patterns reference bookDesign Patterns - Elements of Reusable Object-Oriented Software(ChinesetranslateFirst name:Design Patterns - ReusableofObject-Oriented软件element)As mentioned, there are a total of 23 design patterns. These patterns can be divided into three major categories: Creational Patterns, Structural Patterns, and Behavioral Patterns. Of course, we will also discuss another category of design patterns: J2EE design patterns.

Serial NumberPatterns & Descriptionsincluding
1Creational pattern
These design patterns provide a way to hide creation logic while creating objects, rather than using the new operator to directly instantiate objects. This makes the program more flexible in determining which objects need to be created for a given instance.
  • Factory Pattern(Factory Pattern)
  • Abstract Factory Pattern
  • Singleton Pattern
  • Builder Pattern
  • Prototype Pattern
2Structural pattern
These patterns focus on the composition and relationships between objects, aiming to solve how to build flexible and reusable class and object structures.
  • Adapter Pattern
  • Bridge Pattern
  • Filter Pattern (Filter, Criteria Pattern)
  • Composite Pattern
  • Decorator Pattern
  • Facade Pattern
  • Flyweight Pattern
  • Proxy Pattern
3Behavioral pattern
These patterns focus on communication and interaction between objects, aiming to solve the assignment of responsibilities among objects and the encapsulation of algorithms.
  • Chain of Responsibility Pattern
  • Command Pattern
  • Interpreter Pattern
  • Iterator Pattern
  • Mediator Pattern
  • Memento Pattern
  • Observer Pattern
  • State Pattern
  • Null Object Pattern
  • Strategy Pattern
  • Template Pattern
  • Visitor Pattern
4J2EE Patterns
These design patterns pay special attention to the presentation layer. These patterns were identified by Sun Java Center.
  • MVC Pattern
  • Business Delegate Pattern
  • Composite Entity Pattern
  • Data Access Object Pattern
  • Front Controller Pattern
  • Intercepting Filter Pattern
  • Service Locator Pattern
  • Transfer Object Pattern

Below, an image is used to describe the relationships among design patterns as a whole:

设计模式之间的关系

Advantages of Design Patterns

  • Provides a shared design vocabulary and concepts, enabling developers to better communicate and understand each other's design intentions.
  • Provides proven solutions that can improve software maintainability, reusability, and flexibility.
  • Promotes code reuse, avoiding duplicate design and implementation.
  • By following design patterns, errors and problems in the system can be reduced, and code quality can be improved.

The Six Principles of Design Patterns

1. Open Close Principle

The Open-Closed Principle means:Open for extension, closed for modificationWhen the program needs to be extended, the original code must not be modified, achieving a hot-swappable effect. In short, it is to make the program highly extensible and easy to maintain and upgrade. To achieve this effect, we need to use interfaces and abstract classes, which we will mention in the later specific design.

2. Liskov Substitution Principle

The Liskov Substitution Principle is one of the basic principles of object-oriented design. The Liskov Substitution Principle states that wherever a base class can appear, a subclass must be able to appear as well. LSP is the cornerstone of inheritance and reuse. Only when a derived class can replace the base class without affecting the functionality of the software unit can the base class truly be reused, and the derived class can also add new behaviors on the basis of the base class. The Liskov Substitution Principle is a supplement to the Open-Closed Principle. The key step in implementing the Open-Closed Principle is abstraction, and the inheritance relationship between base classes and subclasses is the concrete implementation of abstraction. Therefore, the Liskov Substitution Principle is a specification of the concrete steps for implementing abstraction.

3. Dependency Inversion Principle

This principle is the foundation of the Open-Closed Principle. Specific content: program for interfaces, depend on abstraction rather than on concrete implementations.

4. Interface Segregation Principle (Interface Segregation Principle)

The meaning of this principle is: using multiple isolated interfaces is better than using a single interface. It also has another meaning: reduce the coupling between classes. From this, it can be seen that design patterns are actually software design ideas derived from large-scale software architecture, convenient for upgrading and maintenance, emphasizing the reduction of dependencies and coupling.

5. Demeter's Law, also known as the Least Knowledge Principle (Demeter Principle)

The Least Knowledge Principle means: an entity should interact with other entities as little as possible, so that system functional modules are relatively independent.

6. Composite Reuse Principle

The Composite Reuse Principle means: try to use composition/aggregation instead of inheritance.

other extensions