[Php design patterns adapter mode]

  Adapter mode (object adapter, adapter type): 

  Converting the interface of a class clients expect another interface. Adapter mode for those classes of incompatible interfaces can work together.

  Mentioned in the definition of the interface adapter mode refers to a broad interface that may represent a collection of methods or method.
 Role:
  Target (target abstract class)
    desired target abstract class defines the customer interface, is an abstract class or interface, can also be a concrete class.
  Adapter (Adapter class)
    it can call another interface, as a converter for adapting Adaptee and Target. It is the core of the Adapter pattern.
  Adaptee (fitter class)
    fitter ie adapted role, which defines an existing interface. This interface requires adaptation, fitter class wrapped the client wants business methods.

Object Adapter:

interface Target{
    public function MethodOne();
    public function MethodTwo();
}

class Adaptee{
    public function MethodOne(){
        echo "+++++++++\n";
    }
}

class Adapter implements Target{
    private $adaptee;
    public function __construct(Adaptee $adaptee){
        $this->adaptee = $adaptee;
    }

    public function MethodOne(){
        $this->adaptee->MethodOne();
    }

    public function MethodTwo(){
        echo "------------";
    }
}

$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
$adapter->MethodOne();

 

Class Adapter:

class Adapter2 extends Adaptee implements Target{
    public function MethodTwo(){
        echo "-----------";
    }
}
$adapter2 = new Adapter2();
$adapter2->MethodOne();

 

Guess you like

Origin www.cnblogs.com/itsuibi/p/10949217.html