Home>Article>Backend Development> Are PHP design patterns commonly used?
Design pattern
##Design pattern is a It is a summary of code design experience that has been used repeatedly, is known to most people, and has been classified and cataloged. 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 ourselves, 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. (Recommended learning:PHP programming from entry to proficiency)
Why should we advocate design pattern (Design Pattern)?
The fundamental reason is to reuse code and increase maintainability.Six principles of design patterns
Open and closed principle: A software entity such as a class, module, and function should be open to extension and closed to modification. Richter Substitution Principle: All places that reference a base class must be able to transparently use objects of its subclasses. Dependency Inversion Principle: High-level modules should not depend on low-level modules, both should Depend on its abstraction; abstractions should not depend on details; details should depend on abstractions. Single Responsibility Principle: Do not have more than one reason for class changes. In layman's terms, a class is only responsible for one responsibility. 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. Demeter's Law: An object should keep the least knowledge about other objects. Design patterns implement these principles to achieve code reuse and increase maintainability.Single case mode
The so-called singleton mode is to ensure that there is only one instance of a certain class, and it instantiates itself and provides this instance to the entire system, that is, in the application Only one instance of this class will exist in the program.Usually the singleton mode is used in instances that only allow database access to objects, thereby preventing multiple database connections from being opened. The singleton mode is a common design pattern. In computer systems, thread pools, caches, log objects, Dialog boxes, printers, database operations, and graphics card drivers are often designed as singletons.
So why use the PHP singleton pattern?
One of the main application scenarios of PHP is the scenario where the application deals with the database. In an application, there will be a large number of database operations. For the behavior of connecting the database handle to the database, using the singleton mode can avoid a large number of operations. new operation. Because every new operation consumes system and memory resources.The above is the detailed content of Are PHP design patterns commonly used?. For more information, please follow other related articles on the PHP Chinese website!