I. Intention
Converts the interface of a class into another interface that the customer expects. The adapter mode makes it possible to work together because the interface is incompatible and cannot work together
Second, adapter mode structure diagram
third, the main 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 a specific class
Four, adapter mode applicable to the scene
1, you want to use an already existing class, and its interface does not meet your needs
2. You want to create a reusable class that can work in conjunction with other unrelated classes or unforeseeable classes
3. You want to use a subclass that already exists, but it is not possible to subclass each one to match their interface. Object adapters can be adapted to its parent class interface (object adapters only)
v. Class adapter mode and Object adapters
class Adapters:Adapter and Adaptee are inherited relationships
1, with a specific adapter class and target to match. The result is that when we want a match for a class and all of its subclasses, class adapter will not work
2, so that adapter can redefine the adaptee part of the behavior, because adapter is a subset of Adaptee
3. The introduction of only one object does not require additional pointers to indirectly obtain adaptee
Object adapters:Adapter and Adaptee are the principal relations
1. Allow one adapter to work with multiple adaptee at the same time. Adapter can also add functionality to all adaptee at once
2, the use of redefining the behavior of adaptee more difficult
Adapter mode and other modes
Bridge Mode: Bridge mode is similar to object adapter, but the starting point of bridge mode is different: Bridge mode is designed to separate the interface parts from the implementation part, so that they can be changed relatively easily and independently. The object adapter pattern means changing the interface of an existing object
Adorner mode (decorator mode): Decorative mode enhances the functionality of other objects without altering its interface. As a result, the decorative pattern is more transparent to the application than the adapter.
class Adapter mode PHP sample
The class adapter uses an inheritance
<?php
/**
* target role
/
interface Target {
/**
* Source class also some 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 ();
}
Client::main ();
? >
Vii. Object Adapter Mode PHP sample
The object adapter is using a delegate
<?php/** * Target role/interface Target {/** * Source class also have 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 ' Adaptee sample
Method1 <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->samplemeth
Od1 (); There is no SampleMethod2 method in the/** * source class, which supplements the */Public Function sampleMethod2 () {echo ' Adapter sampleMethod2 <br
/> ';
} 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 use of PHP to implement the adapter pattern code, there are some of the concept of adapter mode to distinguish, I hope to help you learn.