: This article mainly introduces the Adapter mode of php mode design. if you are interested in the PHP Tutorial, refer to it. I haven't written any articles for more than half a month. it's mainly because there are a lot of messy things in my senior high school, hitting the wall for a job. We also blame ourselves for the poor strength, but also for the lack of practical experience, which is difficult to attract HR interest. So I am still making a small advertisement here. I hope any one of them can give me a job, so it doesn't matter if I have an internship, the best way to practice is not to pay (I have no bottom line ). The location is casual, but at present I can only read the remaining one year's book in Chengdu. I can write code anywhere.
To put it bluntly, I always feel that I "know a lot about the truth, that is, I cannot live my life ". TheseBlogWhether shared by anyone or not, you always need to write your own current cognition. More importantly, you want to get guidance from your predecessors.
Return to the topic. we have discussed three basic things.Design Mode, Today we share another mode that we don't feel very often used, but this is a convenient mode-the Adapter mode.
Is there anyObjectBoth must shout "orientedObject"Age, master orientedObjectIt will bring us unexpected convenience. From the beginning, programmers can write a few lines of code to implement simple functions, and then learn how to combine repeated operations to form a "function ", then, the "functions" and attributes are combined to form a "class ". Step by step, we are considering improving the efficiency of machine running code while also reducing the workload of programmers. So what are the more important considerations of the adapter model we are talking about today? It is the workload of programmers.
When will the Adapter mode be used?
In fact, the simplest example is when we reference a third-party class library. As the version of this class library changes, the APIs it provides may also change. If, unfortunately, when an API referenced in your application has changed, in addition to silently shouting "wocao" in your mind, you have to work hard to change a lot of code.
Is that true? According to the routine, I will answer "no ". We have the Adapter mode ~~
The adapter mode is useful when the interface changes.
Example
If you can understand it through the simple description above, you can only admire your superior comprehension ability. The average person must be on Alibaba Cloud. For ease of understanding, I will reference an example from a Boyou. Original address.
Initial harmony
The black jujube toy company specializes in the production of toys, which are not limited to dogs, cats, lions, fish and other animals. Each toy can perform the "mouth opening" and "shut up" operations, respectively calling the openMouth and closeMouth methods.
At this time, we can easily think of defining an abstract class Toy or even an interface Toy, which is not a problem. other classes inherit the parent class and implement the parent class method. A piece of harmony and confidence.
Balance destruction
To expand its business, the company now cooperates with the jujube remote control company, which can use remote control equipment to control the animal's mouth. However, the remote control device of the jujube remote control company calls the doMouthOpen and doMouthClose methods of animals. What programmers of the black jujube Toys company need to do now is to upgrade the Toy series so that Toy can call the doMouthOpen and doMouthClose methods.
When considering the implementation method, we think about it very directly. if you need it, I will add these two methods to my parent class subclass. When you repeatedly add these two methods to the parent class subclass, you will always think about repetitive work. can't you solve this problem? When there are hundreds of sub-classes, programmers will go crazy. Programmers are often more "lazy" than those who do not affect efficiency ". In doing so, programmers will feel silly. (In fact, I often act as such a fool)
Abstractclass Toy {publicpolicactfunction openMouth (); publicpolicactfunction closeMouth (); // add the doMouthOpen method publicpolicactfunction doMouthOpen () to the control interface of the jujube remote control company (); // add the doMouthClose method publicabstractfunction doMouthClose () for the control interface of the jujube remote control company;} class Dog extends Toy {publicfunction openMouth () {echo "Dog open Mouth \ n ";} publicfunction closeMouth () {echo "Dog open Mouth \ n";} // added publicfunction doMouthOpen () {$ this-> doMouthOpen ();} // added method publicfunction doMouthClose () {$ this-> closeMouth () ;}} class Cat extends Toy {publicfunction openMouth () {echo "Cat open Mouth \ n ";} publicfunction closeMouth () {echo "Cat open Mouth \ n";} // added publicfunction doMouthOpen () {$ this-> doMouthOpen ();} // added method publicfunction doMouthClose () {$ this-> closeMouth ();}}
More irritable
The programmer just finished the code and drank water. suddenly another message came.
The company also needs to cooperate with the remote control company, because the remote control equipment of the company is cheaper and more stable. However, the remote control device of the green jujube remote control company calls the operMouth ($ type) method of the animal to implement mouth control. If $ type is 0, "shut up". Otherwise, you can open your mouth.
Now, the programmer has to upgrade Toy and its sub-classes so that Toy can call the operMouth () method. No one is calm.
Abstractclass Toy {publicpolicactfunction openMouth (); publicpolicactfunction closeMouth (); publicpolicactfunction doMouthOpen (); publicpolicactfunction doMouthClose (); // add the doMouthClose method publicpolicactfunction operateMouth ($ type = 0) for the control interface of the remote control company of green jujube;} class Dog extends Toy {publicfunction openMouth () {echo "Dog open Mouth \ n";} publicfunction closeMouth () {echo "Dog open Mouth \ n";} publicfunction doMouthOpen () {$ this-> doMouthOpen ();} publicfunction doMouthClose () {$ this-> closeMouth ();} publicfunction operateMouth ($ type = 0) {if ($ type = 0) {$ this-> closeMouth ();} else {$ this-> operateMouth () ;}} class Cat extends Toy {publicfunction openMouth () {echo "Cat open Mouth \ n";} publicfunction closeMouth () {echo "Cat open Mouth \ n";} publicfunction doMouthOpen () {$ this-> doMouthOpen ();} publicfunction doMouthClose () {$ this-> closeMouth ();} publicfunction operateMouth ($ type = 0) {if ($ type = 0) {$ this-> closeMouth ();} else {$ this-> operateMouth ();}}}
At this time, programmers have to work hard to find a solution. even if they are diligent, when the remote control companies such as zizao, Qingzao, and huangzao all come, ignore their increasing workload, this Toy class is getting bigger and bigger. one day, programmers will not crash, and the system will crash.
Where is the problem?
As code is written above, code implementation violates the "open-close" principle. a software entity should be open to extensions and closed to modifications. That is, when designing a module, the module should be extended without being modified. That is to say, every corpse is a small kingdom. you can let me participate in your affairs, but you cannot modify my interior unless my internal code can be optimized.
With this idea, we have learned how to use inheritance, how to use polymorphism, and even how to achieve "high cohesion and low coupling ".
Back to this problem, we are now faced with such a problem that I want to implement the new interface method, and the old interface (Toy abstract class) cannot be moved, so there must be a solution. That is to introduce a new class-the main character of this article-the adapter. The functions to be completed by the adapter are clear, and the methods of the existing interface are referenced to implement the new interface. More like its name description, if your interface is not changed, I will use the existing interface to connect with you.
At this point, the solution is ready, and the code is pasted below.
Adaptee = $ adaptee;} // delegate the sampleMethod1 method publicfunction doMouthOpen () {$ this-> Adaptee-> openMouth ();} publicfunction doMouthClose () {$ this-> adaptee-> closeMouth () ;}// class adapter role: greendate remote control company class GreenAdapter implements GreenTarget {private $ adaptee; function _ construct (Toy $ adaptee) {$ this-> adaptee = $ adaptee;} // delegate to call the operateMouth method publicfunction operateMouth ($ type = 0) of Adaptee: GreenTarget) {if ($ type) {$ this-> adaptee-> openMouth () ;}else {$ this-> adaptee-> closeMouth ();}}} class testDriver {publicfunction run () {// instantiate a Dog toy $ adaptee_dog = new Dog (); echo "append the red jujube adapter to the Dog \ n "; $ adapter_red = new RedAdapter ($ adaptee_dog); // mouth opening $ adapter_red-> doMouthOpen (); // shut up $ adapter_red-> doMouthClose (); echo "green jujube adapter \ n"; $ adapter_green = new GreenAdapter ($ adaptee_dog); // opens your mouth $ adapter_green-> operateMouth (1 ); // shut up $ adapter_green-> operateMouth (0) ;}$ test = new testDriver (); $ test-> run ();
The final result is that the Toy class and its subclass implement different interfaces through the adapter without changing themselves.
Conclusion
Converts an interface of a class to another interface that the customer wants. classes that are originally incompatible and cannot work together can work together.
Core idea of the adapter mode: convert operations on some similar classes into a unified "interface" (here is a metaphor)-An adapter, or an interface ", unifies or shields the details of those classes. The adapter mode also constructs a "mechanism" to make the "adaptive" class easy to increase or decrease without modifying the code that interacts with the adapter, comply with the design principles of "reducing code coupling.
Above
Series of articles:
Php pattern designSingleton mode
Factory model of php pattern design
Php pattern design-registration tree pattern
Php pattern design-Adapter pattern
The above introduces the php mode Adapter mode, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.