Learning notes for PHP adapter mode

Source: Internet
Author: User
Tags abstract


"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 ();

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.