"Purpose": Converts the interface of a class into another interface that the customer wishes, and the adapter mode makes it possible to work together because the interface is incompatible and cannot work together
"Primary Role"
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
Applicability
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)
Target role
Interface Target {
Public function simpleMethod1 ();
Public function simpleMethod2 ();
}
SOURCE role
Class Adaptee {
Public Function simpleMethod1 () {
Echo ' Adapter simpleMethod1 '. <br> ";
}
}
Class Adapter role
Class Adapter implements Target {
Private $adaptee;
function __construct (adaptee $adaptee) {
$this->adaptee = $adaptee;
}
SampleMethod1 method for delegating calls to Adaptee
Public Function simpleMethod1 () {
echo $this->adaptee->simplemethod1 ();
}
Public Function simpleMethod2 () {
Echo ' Adapter simpleMethod2 '. <br> ";
}
}
Client
Class Client {
public static function main () {
$adaptee = new Adaptee ();
$adapter = new Adapter ($adaptee);
$adapter->simplemethod1 ();
$adapter->simplemethod2 ();
}
}
Client::main ();
"Run Results"
Adapter simpleMethod1
Adapter SIMPLEMETHOD2
Example 2
The most common football substitution mechanism in life
<?php
/**
* Created by Phpstorm.
*/
-------------Abstract Interface---------------
/** Abstract athlete
* Interface IPlayer
*/
Interface SoccerPlayer
{
function Attack ();
function Defense ();
}
/** striker
* Class Forward
*/
Class Forward implements SoccerPlayer
{
function Attack ()
{
echo "Forward attack <br/>";
}
function Defense ()
{
echo "Forward defense <br/>";
}
}
/** Center
* Class Center
*/
Class Center implements SoccerPlayer
{
function Attack ()
{
echo "Center attack <br/>";
}
function Defense ()
{
echo "Center defense <br/>";
}
}
--------------to fit the object-----------
/** Messi just entered the strike athlete
* Class yaoming
*/
Class Messi
{
function offense ()
{
echo "Lionel Messi offense <br/>";
}
Function Defense ()
{
echo "Lionel Messi defense <br/>";
}
}
------------Adapter--------------
/** Adapter
* Class Adapter
*/
Class Adapter implements SoccerPlayer
{
Private $_player;
function __construct ()
{
$this->_player=new Messi ();
}
function Attack ()
{
$this->_player-> attack ();
}
function Defense ()
{
$this->_player-> Defense ();
}
}
Test as follows
$player 1=new Forward ();
Echo "forward play:<br/>";
$player 1->attack ();
$player 1->defense ();
Echo
Echo "Lionel Messi:<br/>";
$Messi =new Adapter ()
$Messi->attack ();
$Messi->defense ();