Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Introduction to design patterns


May 27, 2021 Design mode


Table of contents


Introduction to design patterns

Design patterns represent the best practices and are often adopted by experienced object-oriented software developers. D esign pattern is the solution to the general problems that software developers face in the software development process. These solutions are summed up by many software developers after a considerable period of trial and error.

Design pattern is a set of repeated use, most people know, classified, code design experience summary. D esign patterns are used to reuse code, make it easier for others to understand, and ensure code reliability. T here is no doubt that design patterns are win-win for others in the system, design patterns make code writing truly engineering, design patterns are the cornerstone of software engineering, like a brick in the building. The reasonable use of design patterns in the project can solve many problems perfectly, each pattern in reality has the corresponding principle to correspond to, each pattern describes a problem that is recurring around us, as well as the core solution of the problem, which is why the design pattern can be widely used.

What is GOF (Gang of Four, Gang of Four)?

In 1994, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides co-authored a book called Design Patterns - Elements of Reusable Object-Oriented Software (Chinese design patterns - reusable object-oriented software elements), which for the first time referred to the concept of design patterns in software development.

The four authors are jointly known as GOF (Gang of Four, Gang of Four). The design patterns they proposed are mainly based on the following object-oriented design principles.

  • Program the interface, not the implementation.
  • Use object combinations over inheritance first.

The use of design patterns

Design patterns are used for two main purposes in software development.

A common platform for developers

Design patterns provide a standard terminology system, and are specific to specific scenarios. For example, a single-case design pattern means using a single object so that all developers familiar with the single-case design pattern can use a single object, and you can tell each other in this way that the program is using a single-case pattern.

Best practice

Design patterns have been developing for a long time and provide the best solution to the general problems faced in the software development process. Learning these patterns helps inexperienced developers learn software design in a quick and easy way.

The type of design pattern

There are a total of 23 design patterns, as mentioned in the design pattern reference book Design Patterns - Elements of Reusable Object-Oriented Software (Chinese Translation: Design Patterns - Reusable Object-Oriented Software Elements). T hese patterns can be divided into three broad categories: Creational Patterns, Structural Patterns, and Behavioral Patterns. Of course, we'll also discuss another type of design pattern: the J2EE design pattern.

Serial number Mode & Description include
1 Create mode
These design patterns provide a way to create logic while creating objects, rather than using new operators.This makes the program more flexible when determining which objects need to be created for a given instance.
  • Factory Pattern
  • Abstract factory model (Abstract Factory Pattern)
  • Singleton Pattern
  • Builder Pattern
  • Prototype Pattern
2 Structure mode
These design patterns focus on the combination of classes and objects.The concept of inheritance is used to combine the interface and define a combination object to get a new feature.
  • Adapter Pattern
  • Bridge mode (Bridge Pattern)
  • Filter mode (Filter, Criteria Pattern)
  • Combination pattern (Composite Pattern)
  • Decorator mode (Decorator Pattern)
  • Appearance mode (FACADE PATTERN)
  • Enjoyment mode (Flyweight Pattern)
  • Proxy Pattern
3 Behavior mode
These design patterns are particularly concerned about communication between objects.
  • Chain of Responsibility Pattern
  • Command mode (Command Pattern)
  • Interpreter Pattern
  • Iterator Pattern
  • Mediator Pattern
  • Memorando Pattern
  • Observer Pattern
  • Status mode (State Pattern)
  • Null Object Pattern
  • Strategy Pattern
  • Template mode (Template Pattern)
  • Visitor mode (Visitor Pattern)
4 J2EE mode
These design patterns are particularly concerned about the representation.These modes are identified by Sun Java Center.
  • MVC mode (MVC Pattern)
  • Business Delegate Pattern
  • Combined entity mode (Composite Entity Pattern)
  • Data Access Object Pattern (Data Access Object Pattern)
  • Front Controller Pattern
  • Intercept filter mode (Intercepting Filter Pattern)
  • Service Locator Pattern
  • Transmission object mode (Transfer Object Pattern)

Here's a picture to describe the relationship between design patterns as a whole:

Introduction to design patterns

Six principles of design patterns

1. Open Close Principle

The open-close principle means: open to extensions and closed to modifications. W hen the program needs to be expanded, you can't modify the original code to achieve a hot-swappable effect. I n short, it's designed to make the program scalable and easy to maintain and upgrade. To achieve this, we need to use interfaces and abstract classes, which we'll mention in the specific design that follows.

2. Richter Replacement Principle (Liskov Substitution Principle)

The Richter replacement principle is one of the basic principles of object-oriented design. T he Richter replacement principle states that where any base class can appear, sub-classes must appear. L SP is the cornerstone of inheritance and reusability, and the base class can only be truly reused if the derived class can replace the base class and the functionality of the software unit is not affected, and the derived class can add new behavior to the base class. T he Richter replacement principle complements the open and closed principle. The key step to realize the principle of opening and closing is abstraction, and the inheritance relationship between the base class and the sub-class is the concrete realization of abstraction, so the Richter subsetermanization principle is the specification of the concrete step of realizing abstraction.

3. Dependence Inversion Principle

This principle is the basis of the open-closed principle, which is specific: for interface programming, it depends on abstraction rather than concrete.

4. Interface Segregation Principle

This principle means that using multiple isolated interfaces is better than using a single interface. I t also has another meaning: reduce the coupling between classes. Thus, in fact, the design pattern is from the large software architecture, easy to upgrade and maintain the software design idea, it emphasizes to reduce dependence, reduce coupling.

5. Dimit's Law, also known as the Principle of Least Know (Demeter Principle)

The least-known principle is that an entity should interact with as little as possible with other entities, making the system functional modules relatively independent.

6. Synthetic Reuse Principle

The principle of composition re-use is to use composition/aggregation as much as possible, not inheritance.