Home  >  Article  >  PHP common design patterns (big summary)

PHP common design patterns (big summary)

藏色散人
藏色散人forward
2019-04-17 10:07:2716181browse

This article mainly uses the PHP language as the basis to explain how to implement various common design patterns in PHP.

PHP common design patterns (big summary)

Design pattern (Design pattern) is a set of classification and cataloging summary of code design experience that is used repeatedly, known to most people. The purpose of using design patterns is to reuse code, make the code easier to understand by others, and ensure code reliability. There is no doubt that design patterns are win-win for oneself, others and the system; design patterns make coding truly engineering; design patterns are the cornerstone of software engineering, just like the structure of a building.

To learn more about PHP design pattern knowledge points, please sign up for six online courses: http://www.php.cn/k.html

Design pattern related knowledge

1. Overview of design pattern

Design pattern (Design pattern) is A summary of code design experience that has been used repeatedly, is known to most people, and has been cataloged. The purpose of using design patterns is to reuse code, make the code easier to understand by others, and ensure code reliability...

For details, click: "Design Pattern Overview"

2. Opening and Closing Principle

The Opening and Closing Principle (OCP) is the cornerstone of "reusable design" in object-oriented design and one of the most important principles in object-oriented design. First, many other design principles are a means to realize the opening and closing principle...

For details, click: "Opening and Closing Principle"

3. Liskov Substitution Principle

Liskov Substitution Principle LSP talks about the relationship between base classes and subclasses. Only when this relationship exists does the Richter substitution relationship exist. If the relationship between two specific classes A and B violates the design of LSP...

For details, click: "Richter Substitution Principle"

4. Interface isolation principle

The client should not rely on interfaces it does not need; the dependence of one class on another class should be based on the smallest interface. It is better to use multiple specialized interfaces than a single overall interface. The dependence of one class on another class should be based on the smallest interface...

For details, click: "Interface Isolation Principle"

Creative pattern

1. Factory pattern

Factory pattern is our most commonly used example The object model is a model that uses factory methods to replace the new operation. The famous Jive forum makes extensive use of the factory pattern. The factory pattern can be seen everywhere in the Java program system...

For details, click: "Factory Pattern"

2. Singleton pattern (Singleton pattern)

The singleton pattern (Singleton pattern) is a commonly used software design pattern. It contains only one special class called a singleton class in its core structure. The singleton mode can ensure that there is only one instance of a class in the system and that the instance is easy to access from the outside world, thereby facilitating the control of the number of instances and saving system resources...

For details, click: "Single Case Pattern

3. Builder Pattern

Builder Pattern is the object creation pattern. It can separate the internal representation of a product from the product generation process, so that a construction process can generate product objects with different internal representations...

For details, click: "Builder Pattern

4. Prototype Pattern

Prototype Prototype pattern is a creational design pattern. Prototype pattern allows one object to create another Customizable objects do not need to know any details about how to create them. The working principle is...

For details, click: "Prototype Mode"

Structural Pattern

1. Adapter Pattern

Adapter pattern is also called adapter pattern and is one of the structural patterns. The interface form of an existing class (or external class) can be changed through the Adapter pattern. Adapter pattern application scenarios...

For details, click: "Adapter Pattern"

2. Composite Pattern

Composition mode: allows customers to combine objects into a tree structure to express the "whole/part" hierarchy. Combination allows customers to process individual objects and combinations of objects in a consistent manner...

For details, click: "Composition Mode"

3. Appearance Mode ( Facade mode)

Appearance mode refers to the packaging of appearance, so that the application can only see the appearance object, but not the specific detail object, which will undoubtedly reduce the complexity of the application. , and improves the maintainability of the program...

For details, click: "Appearance Mode"

4. Agent Mode

The role of the proxy mode is similar to that of inheritance, interfaces and combinations. They are all designed to aggregate common parts and reduce the code of common parts. The difference is that compared to inheritance, their contexts are different. The meaning expressed by inheritance is is-a, while the meaning expressed by proxy is closer to interface, which is has-a. And when using proxy, the sentence should be "less". Use inheritance and multiple combinations"...

For details, click: "Agent Mode"

5. Decoration Mode

Dynamically extend the functionality of an object without changing the original class file and using inheritance. It wraps the real object by creating a packaging object, that is, decoration...

For details, click: "Decoration Mode"

Statement:
This article is reproduced at:hcoder.net. If there is any infringement, please contact admin@php.cn delete