Three categories of PHP design patterns

Introduction to design patterns

Design patterns represent best practices and are usually adopted by experienced object-oriented software developers. Design patterns are solutions to general problems faced by software developers in the software development process. These solutions are summed up by many software developers after a long period of trial and error.
A design pattern is a set of code design experience that has been used repeatedly, is well known by most people, and is catalogued. The use of design patterns is to reuse code, make it easier for others to understand, and ensure code reliability. There is no doubt that design patterns are win-win for others and systems. Design patterns make code preparation truly engineering. Design patterns are the cornerstone of software engineering, just like the bricks of a building. The reasonable use of design patterns in the project can perfectly solve many problems. Each pattern has a corresponding principle to correspond to it in reality. Each pattern describes a problem that continues to occur around us, and the problem The core solution is the reason why design patterns can be widely used.

What is GOF (Gang of Four)?

In 1994, a book titled Design Patterns-Elements of Reusable Object-Oriented Software (Chinese translation: Design Patterns-Reusable Object-Oriented Software) was co-authored by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. Elements), which first mentioned the concept of design patterns in software development.
The four authors are collectively called GOF (Gang of Four). The design patterns they proposed are mainly based on the following object-oriented design principles.
Program the interface rather than the implementation.
Use object composition in preference to inheritance.

Use of design patterns

The two main uses of design patterns in software development.

A common platform for developers

Design patterns provide a standard terminology system that is specific to a specific situation. For example, the singleton design pattern means the use of a single object, so that all developers familiar with the singleton design pattern can use a single object, and in this way can tell the other party 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 common problems faced in the software development process. Learning these patterns helps inexperienced developers to learn software design in a simple and fast way

Insert picture description here

Six principles of design patterns

1. Open Close Principle

The principle of opening and closing means: open for extension and closed for modification. When the program needs to be expanded, the original code cannot be modified to achieve 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 design later.

2. Liskov Substitution Principle

The Richter substitution principle is one of the basic principles of object-oriented design. The Richter substitution principle says that wherever a base class can appear, a subclass must appear. LSP is the cornerstone of inheritance and reuse. Only when the derived class can replace the base class and the function of the software unit is not affected, the base class can really be reused, and the derived class can also add new ones on the basis of the base class. behavior. The Richter Substitution Principle is a supplement to the opening and closing principle. The key step to realize the principle of opening and closing is abstraction, and the inheritance relationship between the base class and the subclass is the concrete realization of abstraction, so the Richter substitution principle is the specification of the concrete steps to realize the abstraction.

3. Dependence Inversion Principle

This principle is the basis of the opening and closing principle, the specific content: for interface programming, rely on abstraction instead of concrete.

4. Interface Segregation Principle

This principle means that using multiple isolated interfaces is better than using a single interface. It also has another meaning: reduce the degree of coupling between classes. It can be seen that, in fact, the design pattern is a software design idea that starts from a large-scale software architecture and is easy to upgrade and maintain. It emphasizes reducing dependence and reducing coupling.

5. Demeter Principle, also known as the Demeter Principle

The least-know principle means that an entity should interact with other entities as little as possible to make the system functional modules relatively independent.

6. Composite Reuse Principle

The principle of composition reuse is: try to use composition/aggregation instead of inheritance.

Guess you like

Origin blog.csdn.net/qq_41526316/article/details/108454159