Home > Article > PHP Framework > What design patterns does thinkphp5 use?
1. Introduction to design patterns
First of all, let’s understand what design patterns are:
Design patterns It is a summary of a set of reliable code design experiences that have been used repeatedly, are easily understood by others, and are reliable.
Design patterns are not a patent of Java. We can also use 23 design patterns well in PHP using object-oriented methods.
So what is the relationship between the architecture, framework and design patterns we often talk about?
Architecture is a set of system structures and the overall solution for the project; the framework is semi-finished software that can be reused and is the specific program code. Architecture generally involves what kind of framework to use to speed up and optimize the solution of certain problems, and good framework codes use many design patterns reasonably.
Related recommendations: "ThinkPHP Tutorial"
2. Refining several principles of design patterns:
(1) Open-Closed Principle: Modules should be open for extension but closed for modification.
(2) Liskov substitution principle: If the parent class is called, then it can be run if it is replaced by a subclass.
(3) Dependency inversion principle: abstraction does not depend on details, interface-oriented programming, and passed parameters try to reference high-level classes.
(4) Interface isolation principle: Each interface is only responsible for one role.
(5) Principle of composition/aggregation reuse: Use composition/aggregation as much as possible and do not abuse inheritance.
3. What is the function of design pattern?
Design patterns can solve the problem of replacing messy code and form a good code style. The code is easy to read and engineers can easily understand when adding new functions without modifying the interface. It has strong scalability
Stability It has good performance, and generally there will be no unknown problems that cannot be solved by design patterns:
(1) Design patterns are templates used to organize your code, rather than libraries that are directly called;
(2) Design Patterns are not the most efficient, but code readability and maintainability are more important;
(3) Don’t blindly pursue and apply design patterns, think more about it when refactoring.
4. Design Pattern Classification
1. Creation Pattern:
Singleton Pattern, Factory Pattern (Simple Factory, Factory Method, Abstract Factory ), creator mode, prototype mode.
2. Structural mode:
Adapter mode, bridge mode, decoration mode, combination mode, appearance mode, flyweight mode, proxy mode.
3. Behavioral patterns:
Template method pattern, command pattern, iterator pattern, observer pattern, mediator pattern, memo pattern, interpreter pattern, state pattern, strategy pattern, Chain of responsibility model, visitor model.
The above is the detailed content of What design patterns does thinkphp5 use?. For more information, please follow other related articles on the PHP Chinese website!