Home >Backend Development >PHP Tutorial >PHP design patterns, coupling and polymorphism_PHP tutorial
A design pattern is a template that teaches you how to organize your code using real and reliable designs.
All design patterns have some common characteristics: a name, a problem statement, and a solution.
1. The identification of a design pattern is important because it allows other programmers to immediately understand the purpose of your code without having to study too deeply (at least through this identification programmers will be familiar with this pattern) .
2. The problem description is used to illustrate the application field of this model.
3. The solution describes the execution of this model. A good discussion of a design pattern should cover the advantages and disadvantages of using the model.
For example, a shirt you buy from a store is a code library. Its color, style and size are determined by the designer and manufacturer, but it meets your needs. However, if nothing in the store suits you, then you can create your own shirt (design its shape, choose the fabric, and have it sewn together). But if you are not a tailor, you may find it easy to find a suitable pattern and then design your own shirt according to this pattern. Using a mockup, you can get a proficiently designed shirt in less time.
php has a total of 23 traditional design patterns,
Interpreter (interpreter mode), Factory (factory mode), Facade (appearance mode), Decorator (decoration mode), Builder (builder mode)
Adapter (adapter mode), Template (template mode), Command (command chain mode), Singleton (single case mode), Observer (observer mode)
Strategy (strategy mode), Visitor (visitor mode), Memento (memo mode), Prototype (prototype mode), Mediator (mediator mode)
FlyWeight (flyweight mode), Chain Of Responsibility (chain of responsibility mode), Bridge (bridge mode), Proxy (agent mode), State (state mode)
Composite (composition mode), Interator (iterator mode), DAO (data access object mode), Delegation (delegation mode)
There are five common design patterns in PHP
1. Factory mode 2. Singleton mode (single element mode) 3. Observer mode 4. Command chain mode 5. Strategy mode
Coupling, also called coupling degree, is a measure of the degree of association between modules. The strength of the coupling depends on the complexity of the interface with the module, the way the module is called, and the amount of data transferred through the interface. The coupling degree between modules refers to the dependency relationship between modules, including control relationship, calling relationship, and data transfer relationship. The more connections between modules, the stronger their coupling and the worse their independence. In software design, coupling and cohesion are usually used as criteria to measure the degree of module independence. One criterion for dividing modules is high cohesion and low coupling
"For object-oriented programming languages, polytyping is the third most basic feature (the first two are data abstraction and inheritance."
"Polymorphism" separates the interface from the specific implementation details from another perspective, that is, it realizes the separation of the "what" and "how to do" modules. Using the concept of polymorphism, code organization and readability can be improved. In addition, "easily extensible" programs can be created. They can easily "grow" both during the creation of the project and when new features need to be added.