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->
; 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