Ways to implement some design patterns in thinkphp
First, let's look at the six principles that the design pattern follows
1. Single Responsibility Principle
(1) Definition: Do not have more than one cause of class changes. In layman's terms, a class is responsible for only one responsibility.
(2) The advantages of following a single responsibility are:
Can reduce the complexity of the class, a class is responsible for only one responsibility, its logic is certainly more than the responsibility of more than the responsibility of more simple;
Improve the readability of the class, improve the maintainability of the system;
The risk of change is reduced, the change is inevitable, if a single responsibility principle is well adhered to, when a function is modified, it can significantly reduce the impact on other functions.
2. The Richter replacement principle
Definition 1: If for each object of type T1, there is an object O2 of type T2, so that all program P defined by T1 is replaced with O1 for all objects O2, the behavior of program p does not change, then type T2 is a subtype of type T1.
Definition 2: All references to base classes must be able to transparently use objects of their subclasses.
Subclasses can implement the abstract methods of the parent class, but cannot override the non-abstract methods of the parent class.
Subclasses can add their own unique methods.
When a method of a subclass overloads a method of the parent class, the method's preconditions (that is, the parameter of the method) are more lenient than the input parameters of the parent class method.
When a method of a subclass implements an abstract method of the parent class, the post condition of the method (that is, the return value of the method) is stricter than the parent class.
3. Dependency Inversion principle
Definition: High-level modules should not be dependent on the lower layers, both should rely on their abstraction; abstractions should not depend on detail; detail should be dependent on abstraction.
There are three ways to pass the dependency relationship, the method used in the above example is interface passing, and there are two ways to transfer it: the construction method passing and setter method passing, I believe that using the spring framework, the way to rely on the transfer will not be unfamiliar.
In actual programming, we generally need to do the following 3 points:
The low-level module should have an abstract class or interface as much as possible, or both.
The declaration type of a variable is as abstract a class or interface as possible.
Follow the Richter substitution principle when using inheritance.
The core of the dependency inversion principle is that we can interface-oriented programming, understand the interface-oriented programming, and understand the dependency inversion.
4. Interface Isolation principle
Definition: The client should not rely on interfaces it does not need, and the dependency of one class on another should be based on the smallest interface.
When the interface is constrained by the interface isolation principle, the following points should be noted:
The interface is as small as possible, but there are limits. The refinement of the interface can improve the design flexibility is not a fact, but if too small, it will result in an excessive number of interfaces, resulting in complex designs. So be sure to be modest.
Customizing the service for an interface-dependent class exposes only the methods it needs to the calling class, and the methods it does not need are hidden. A minimal dependency can be established only by focusing on providing a customized service for a module.
Increase cohesion and reduce external interaction. Enable the interface to do the most things with the least amount of method.
The use of interface isolation principle, must be moderate, the interface design too large or too small are not good. When designing interfaces, it is only when you spend more time thinking and planning that you can accurately practice this principle.
5. Dimitri Law
Definition: An object should have minimal knowledge of other objects.
In the adoption of the Dimitri law, we should weigh repeatedly, not only to achieve a clear structure, but also high cohesion and low coupling.
6. Opening and closing principle
Definition: A software entity such as classes, modules, and functions should be open to extensions and closed for modification.
The opening and closing principle is the most basic design principle in object-oriented design, and it guides how to establish a stable and flexible system. The open/Closed principle is probably the most ambiguous one in the six principles of the design pattern, it only tells us that opening to the extension, closing the modification, but how to open the extension, the modification is closed, and does not explicitly tell us. In the past, if someone told me "you must obey the opening and shutting principle when designing," I will feel that he did not say anything, but seems to have said everything. Because the opening and closing principle really taixu.
The Open/close ratio principle is that some extensions can be edited while the core package content of the system is not allowed to be edited
Second, design mode
Design mode is the default, that is, to follow the six principles, but we also need to pay attention to the operation process
What is design mode?
Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are multi-win; Design patterns make code production truly engineering; Design patterns are the cornerstone of software engineering, like the structure of a building.
Features of the design pattern:
(1) There is reusability in a given scenario, and its solution is valid for the same type of environment with different problems.
(2) can impart sex, namely the problem appears many opportunities, solves the problem the plan is same, people are relatively acceptable.
(3) There is a name for the representation mode.
The design pattern mainly has the following functions:
(1) Reuse the design, reuse the proportion of the design more meaningful code, it will automatically bring code reuse.
(2) To provide common vocabulary for the design, each model name is a design vocabulary, its concept makes communication between programmers more convenient.
(3) using pattern vocabularies in development documents can make it easier for others to understand what you're thinking and understand why you're doing it and what you've done. Writing development documents is also easier.
(4) Applying design patterns can make refactoring systems easy, ensuring that the right code is developed, and reducing the likelihood of errors in design or implementation, and providing a good system framework for rewriting other applications.
(5) The correct use of design patterns, can save a lot of time.
Thirdly, we apply some design patterns to our familiar framework--->thinkphp
Here is a simple little example, using design patterns to help us make the odd even and special element 0 judgments
1. First, we need to write in our controller, import some of the classes we need, instantiate our classes and invoke the methods in our class
2. Something in the class Zhizheall.class.php file, used as a base class, I think the base class should be very familiar to a PHP programmer.
Here are just a few judgments and achieve different effects, three programs are roughly the same, only the judging conditions are different, the calling class is not the same.
3. In class Zhizhezero.class.php, we use it to make special judgments.
4. We used to make odd judgments in the file Zhizheodd.class.php.
5. We use the Zhizheeven.class.php to make even-numbered judgments in file
Results:
This allows us to apply design patterns to our familiar frameworks.
Six principles followed by design patterns