Learning php design patterns php implementation adapter patterns _ php skills

Source: Internet
Author: User
Tags php example
This article mainly introduces the Adapter mode in php design mode, and uses php to implement the Adapter mode. if you are interested, refer to it. I. intention
Converts an interface of a class to another interface that the customer wants. The Adapter mode makes it impossible to work together because the interface is incompatible.
II. structure of the adapter mode

III. main roles in Adapter mode
Target role: defines the interface used by the client for a specific domain, which is what we expect
Source (Adaptee) role: interface to be adapted
Adapter role: adapt the Adaptee interface to the Target interface. The Adapter is the core of this mode. the Adapter converts the source interface to the Target interface. this role is a specific class.
4. applicable scenarios of the adapter mode
1. you want to use an existing class, and its interface does not meet your needs.
2. you want to create a reusable class that can work collaboratively with other unrelated or unpredictable classes.
3. you want to use an existing subclass, but it is impossible to subclass each of them to match their interfaces. Object Adapter can adapt to its parent class interface (only for Object Adapter)
V. class Adapter mode and Object Adapter
Class adapter:The Adapter and Adaptee are inherited.
1. use a specific Adapter class to match with Target. The result is that when we want to match a class and all its subclasses, the class Adapter will not be competent.
2. the Adapter can redefine partial behavior of Adaptee because the Adapter is a subset of Adaptee.
3. introduce only one object, and no extra pointer is required to indirectly obtain adaptee.
Object Adapter:The Adapter and Adaptee are delegate relationships.
1. allow one Adapter to work with multiple Adaptee at the same time. The Adapter can also add functions to all Adaptee at a time.
2. it is difficult to use the redefinition of Adaptee.
Adapter mode and other modes
Bridge Mode: The bridge mode is similar to the Object Adapter, but the bridge mode has different starting points: the bridge mode aims to separate the interface part and the implementation part, therefore, they can be easily and independently changed. The Object Adapter mode means to change the interface of an existing object.
Decorator mode: the decorator mode enhances the functions of other objects without changing their interfaces. Therefore, the decoration mode is more transparent to the application than the adapter.
VI. example of class Adapter mode PHP
Class adapters use inheritance

<? Php/*** Target role */interface Target {/*** method 1 */public function sampleMethod1 () of the source class (); /*** method 2 not available in the source class */public function sampleMethod2 ();} /*** Source role */class Adaptee {/*** method contained in the source class */public function sampleMethod1 () {echo 'adaptee sampleMethod1
';}}/*** Class Adapter role */class Adapter extends Adaptee implements Target {/*** no sampleMethod2 method is available in the source class. add */public function sampleMethod2 () {echo 'adapter sampleMethod2
';}} Class Client {/*** Main program. */public static function main () {$ adapter = new Adapter (); $ adapter-> sampleMethod1 (); $ adapter-> sampleMethod2 () ;}} Client :: main ();?>

VII. Object Adapter mode PHP example
The Object Adapter uses the delegate

<? Php/*** Target role */interface Target {/*** method 1 */public function sampleMethod1 () of the source class (); /*** method 2 not available in the source class */public function sampleMethod2 ();} /*** Source role */class Adaptee {/*** method contained in the source class */public function sampleMethod1 () {echo 'adaptee sampleMethod1
';}}/*** Class Adapter role */class Adapter implements Target {private $ _ adaptee; public function _ construct (Adaptee $ adaptee) {$ this-> _ adaptee = $ adaptee;}/*** delegates to call the sampleMethod1 method of Adaptee */public function sampleMethod1 () {$ this-> _ adaptee-> sampleMethod1 ();}/*** the sampleMethod2 method is not found in the source class. add */public function sampleMethod2 () {echo 'adapter sampleMethod2
';}} Class Client {/*** Main program. */public static function main () {$ adaptee = new Adaptee (); $ adapter = new Adapter ($ adaptee); $ adapter-> sampleMethod1 (); $ adapter-> sampleMethod2 () ;}} Client: main () ;?>

The above is the code that uses php to implement the Adapter mode. there are some differences on the concept of the adapter mode. I hope it will be helpful for your learning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.