Adapter mode for PHP

Source: Internet
Author: User

Adapter mode (Adapter) mode: Converts the interface of a class into the interface of another class expected by the customer. Adapters allow classes that are incompatible with the original interface to work seamlessly.

"Primary role in adapter mode"

Target role: Defines a domain-specific interface used by the client, which is what we expect

Source (adaptee) Role: interface that needs to be fitted

Adapter (Adapter) Role: The interface to the Adaptee is adapted to the target interface; The adapter is the core of this mode, and the adapter converts the source interface to the target interface, which is the specific class.

In fact, there is a two-port socket on your wall (adaptee), but you'll need a three-port fan (Target), and you'll need a Adapter.

"Class Adapter mode PHP sample"

The class adapter uses an inheritance

/**
 * Target role
/interface Target {

    /**
     * Source class also has the Method 1 *
    /Public Function sampleMethod1 ();

    /**
     * Source class does not have a method 2
    /Public Function sampleMethod2 ();
}

/**
 * Source Role
/class Adaptee {

    /**
     * Source class contains methods *
    /Public Function sampleMethod1 () {
        Echo ' adaptee sampleMethod1 <br/> ';
    }

/**
 * Class Adapter Role/Class
Adapter extends Adaptee implements Target {

    /**
     * Source class does not have a sampleMethod2 method. This supplemental
     *
    /Public Function sampleMethod2 () {
        echo ' Adapter sampleMethod2 <br/> ';
    }

}

Class Client {

    /**
     * Main program.
     */public
    static function main () {
        $adapter = new Adapter ();
        $adapter->samplemethod1 ();
        $adapter->samplemethod2 ();

    }


"Object Adapter mode PHP Sample"

The object adapter is using a delegate

/** * Target role/interface Target {/** * Source class also has the method 1 */Public function sampleMethod1 ();
/** * Source class does not have the method 2/Public Function sampleMethod2 (); /** * Source Role/class Adaptee {/** * Source class contains methods */Public function sampleMethod1 () {echo ' Adap
    Tee sampleMethod1 <br/> ';

    }/** * Class Adapter role * * Classes Adapter implements Target {private $_adaptee;
    Public function __construct (adaptee $adaptee) {$this->_adaptee = $adaptee; /** * Delegate Calls Adaptee SampleMethod1 method */Public Function sampleMethod1 () {$this->_adaptee-&gt
    ; SampleMethod1 (); There is no SampleMethod2 method in the/** * source class, this supplemental */Public Function sampleMethod2 () {echo ' Adapter Samplemeth
    Od2 <br/> ';
     } class Client {/** * Main program.
        */public static function main () {$adaptee = new adaptee ();
        $adapter = new Adapter ($adaptee); $adapter->samplemEthod1 ();

    $adapter->samplemethod2 ();
 }

}

The class adapter uses "multiple inheritance" to implement the method, has brought the bad high coupling, therefore generally does not recommend the use. Object adapters take the form of "object Combination", which is more consistent with the spirit of loose coupling.

There is also a good description of the object adapter to look at the http://www.knowsky.com/890188.html.

Source: http://www.cnblogs.com/moxiaoan/p/6230018.html

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.