Php design patterns-summary, php design patterns --. Notes on php design patterns-summary, php design patterns-1. general definitions of the introduction of design patterns are not mentioned. I just want to talk about the design patterns I understand, notes on the main php design patterns I understand-summary, php design patterns --
I. INTRODUCTION
The general definition of the design pattern is not to mention. I just want to talk about the design pattern that I understand. The main purpose of the design pattern is to take advantage of the characteristics of the object-oriented (class, interface, etc, make code easierExtension, EasyReuse, EasyMaintenance. These three features require us not to accumulate too many functions into a class,AllocateTo more classes. Therefore, 20 or more design patterns are designed for the above four purposes.
Php design patterns this book talks about 19 design patterns, but in fact, most of the design patterns are the same in thinking or design. I will classify and summarize them below, so that you can better understand this book, but you 'd better take a look at this book. the examples used in it are relatively simple. remember this book to make the code easier when reading a book, some interfaces should be implemented to be more standardized without interfaces.
(Note: Because this article summarizes the book "php Design Patterns", I have mentioned the examples in the time book but have not written them in this article. you can view the ebook, in the future, I will also introduce the articles of various models or add examples in this article)
II. modes to be considered when creating classes
The final concept of object-oriented is class. class is an important step from process-oriented conversion to object. Therefore, it is very important to create a class and how dependencies between classes are implemented. the following models are designed to address this issue.
1. Builder mode
We should minimize the appearance of new in the code. this is an important way to create a class, but we should make it appear in the builder or factory as much as possible, instead of appearing in a class method. Cause 1: The initialization method of this class may be troublesome. we need not only new, but also passing parameters to its new constructor. First, we need to create those parameters, some objects may need new in those parameters, and the initialization operation is very long. cause 2: the requirement of this class may change in the future, so we need to change the initialization operation, the changed operation is also performed in other classes, which does not conform to the principle of independence between classes.
2. factory model-extended builder model
When a lot of code and framework talk about the factory model, what it actually does is the builder model. I think the significance of the factory model is more to make the builder model more flexible abstract factory. Next, the concept of the builder mode. if some classes have a set of standard creation processes, we can first create an abstract project, and then each specific class inherits this class, the pizza factory is a good example.
3. Supplement: The concept of service-the core of many php frameworks, and the implementation method of dependency injection. Dependency Injection is important. I will explain it in other articles.
If we look at the two php frameworks symfony and zend framework, we will find that there is a very important concept called dependency injection, that is, if the dependency between classes is solved, A very important concept of implementation is service or service container. In my understanding, this is similar to the factory model. zf can also point a service to a specific factory, and in the factory class, we can also implement abstract factories, therefore, these two are similar concepts, but they do not conflict with each other. It will be introduced separately later.
3. when the demand changes or features are added
The following code can be reused easily:
1. Adapter mode
We say that when the function changes, we should ensure that the class interface remains unchanged, so that when a class changes, the code of the class it uses or uses does not need to change. However, after all, there will be code and classes that violate this principle. for example, in the example in the book, the function of decomposing errorobject should be implemented by the logtocsvadapter class with a change in requirements, but for many reasons (for example, the code cannot be changed or is not open-source), this is not the case. The adapter mode is used to handle this situation. it can use extend (for example, the usage in books) or use it as a member variable to implement a new interface that meets the requirements.
2. decorator mode
Form: use the decorated class as a member to call the method.
You can use this mode when you want to add a function to a class but are afraid to break it down. However, this idea cannot fundamentally solve the problem.
Another common scenario is that you only want to present some functions of this class to others, rather than all functions.
3. intermediary mode (similar to event mode)
Form: multiple associated classes use the intermediary class as its member. calling a method changes the status of all classes registered in the intermediary through the intermediary, therefore, the registered class must implement an interface for the intermediary to call.
Similar to observer.
The structure is similar to the event mode, except that the registration is written to the dead (hardcode) in the intermediary (corresponding to the registration machine in the event), but its solution to the problem is a special case of the event, so it is called the intermediary mode.
Suppose there is a class, and another class is parallel to the first class in concept (it may be a class from the beginning, it may also be a class that changes with the demand). These two classes are associated, and a change in one class needs to change the status of the other class. in this case, the intermediary is required. The specific example is described in the event mode.
4. distribute functions to more classes
In actual projects, it is very troublesome to maintain a "big" class with many functions. This does not conform to the principle of high cohesion and low coupling. if a class has more functions, the more process-oriented, the closer the various functions are. In fact, the main problem to be solved by object-oriented is how to distribute functions to more classes in a more manageable way, it sounds simple. In fact, if you use a bad way to separate the classes, it will be even more messy. in a sense, this is also the reason for the emergence of the design model. For example, a person is responsible for maintaining a class and testing this class. if the result changes every time a function changes, he must change the test code of this class, if we distribute functions to more classes, we can ensure that the test code of this class does not change.
1. event mode (I will write an article for details here)
Many people, including my impressions of the event, remain in the window process. when we click the mouse or button, there will be an event, which will change all the objects registered with the event, possibly because the view has changed, it may also be a function defined by ourselves. the intermediary mode is similar to the event mode, except that all registered in the intermediary are equal, such as a table and a bar chart, changing the table content will change the column chart. shortening the column chart will change the table content. But in general application logic, what are the functions of the event mode and intermediary mode?
Aside from the meaning of the event mentioned above, the above is only a special case of the event. here we will describe the more general usage of the event. In fact, the event is essentially the intermediary mode. the intermediary places the registered container in the class that needs to be changed, which is equivalent to passing the object in the event, however, some classes were not designed as intermediaries at the beginning. that is to say, they did not store members of an intermediary, probably because they did not expect this requirement, it may also be because it is difficult to add this mechanism to the class, but the event mode that can implement the same function outside the class is selected. (So the intermediary mode can be converted to the event mode,Although the event mode saves the trouble in the class, it needs to provide an event mechanism, which is more troublesome than the intermediary in the class, but because it is separated into the class, therefore, there are many ready-made Intermediaries. for example, each framework basically implements this mechanism, and symfony can use this component in a framework other than symfony.
In addition to the operations on the preceding view interface, other examples may not be so intuitive and do not conform to the event name. The main function of the example is as described in the title, it is through the code structure of the event mode to better split the code function to make it easier to manage. To understand this, you must forget the general meaning of the event. Think about a complicated logic example. a telephone company will calculate the cost of a user's call record. as we all know, this is a complicated logic, there will be differences between different plans and regions of users. so should we write a long string of logic judgment code in a class? We certainly do not comply with our principles, so we will use the event model.Form(Repeated emphasis is only a form) to implement the division of logic into different classes. We register all the telephone services as listener, and each call record is used as the subscriber. the if statement is used to determine which service it belongs to, and then dispatch the services, listener obtains the Bill object and changes it. The final bill object is the final bill information. Through the event model, we split different processing logics into listener objects, making it easier for future extension.
The above example is to modify an object through listener, or simply use the obtained object information for related operations. for example, there will be a lot of operations when a blog post is published in the blog site, such as the need to store the database, you need to perform inverted sorting and tag operations. if you want to publish data to the home page, you also need a series of other operations. you can use some listener to listen to the Post blog posting operation, instead of modifying the Post object, the Post object information is used for operations.
Back to the general saying, the event mode is mainly used in plug-ins, of course, in a broader sense. This is symfony's introduction to Consider the real-world example where you want to provide a plugin system for your project. A plugin shoshould be able to add methods, or do something before or after a method is executed, without interfering with other plugins. this is not an easy problem to solve with single and multiple inheritance (were it possible with PHP) has its own drawbacks.
In the above sense, events are a mechanism that is more conducive to extended processing and processing objects. when you want to use this mechanism, events can be used, with the help of the event components in the existing framework, you can convert both the observer and intermediary into the event mode. Therefore, events are very common and can also be reflected in the importance of events in c.
You can also take a look at the symfony Event document to learn how to use the event.
2. Observer Mode
Format: $ this-> obserber-> update ($ this), while the intermediary form is $ this-> mediator-> chage ($ this, array ('band' => $ newname )). We can see that it is only to fulfill two different functional requirements. Both of them can be replaced by events. the former sends an update event, the latter sends a changeBandName event, and then changes by viewing the name of the input object.
3. delegation mode
By assigning or entrusting to other objects, the delegated design mode can remove the core objects.Decision (if statement)AndComplex. Similar to the concept of delegation in c #, in c #, the delegation is passed to other functions as parameters in the form of functions, so that the functionality is assigned to other equations. The delegation in the object-oriented model refers to assigning functionality to the delegate class.
Form: there is a member variable in the delegate class.
4. policy mode
When you can create interchangeable objects that are applied to object-based and composed of self-contained algorithms, the best practice is to use the policy mode.
It is basically the same as the delegate mode. in terms of form and meaning, only the example delegate mode in the book transfers $ this-> playlist to the delegate mode, however, the policy mode passes the entire $ this to the policy mode.
The rule mode also has an important significance. if a method of a class is not commonly used, it can be converted to the rule mode, so that you do not have to test the class every time in the class test.
5. proxy mode
The proxy mode can have different forms, as long as it is assigned the meaning of "proxy. In the book, the proxy is implemented through the inheritance form of the decorator.
V. others
The following are common models:
1. inherent object-oriented model-template mode
I will not introduce it in detail. it is an idea with abstract classes and interfaces.
2. facade)
The word facade is common in the naming of classes in some frameworks. similar to the function in process orientation, it encapsulates a series of code for a specific function into a static method in a class, this class usually only has this method.
3. abstract public methods into classes-Data Access Object mode
Abstract a common method into a class is the basic principle of object-oriented. it is not only an entity. if a function can be used by multiple entities or functions, we should separate it into a class, if other classes use it, it is the dependency injection mentioned above ).
The Data Access Object mode is also the basic practice of this mode. Can be combined with zend famework guide http://framework.zend.com/manual/current/en/in-depth-guide/preparing-db-backend.html for understanding. Basically, tablegateway is used to encapsulate data operations. tablegateway is also a mature mode in this field. it is basically a table class, but this limits the connection operations of the database itself, we can only implement connections through operations between classes. I will write them in other articles.
4. only one single-element mode is required in the system.
This mode is also common.
6. key sentence notes in the book
1. the connection between different objects is simplified.
Introduction -- I. general definition of the introduction of design patterns. I just want to talk about the design patterns I understand. What are the main design patterns I understand...