/**
* Class Toy
*
* @describe 原有的接口
*
* @author nick
*
*/
abstract class Toy
{
public abstract function openMonth();
public abstract function closeMonth();
}
class Dog extends Toy
{
public function openMonth()
{
echo '狗张嘴';
}
public function closeMonth()
{
echo '狗闭嘴';
}
}
class Cat extends Toy
{
public function openMonth()
{
echo '猫张嘴';
}
public function closeMonth()
{
echo '猫闭嘴';
}
}
// 目标公司 红枣公司
interface RedTarget
{
public function doMonthOpen();
public function doMonthClose();
}
// 目标公司 绿枣公司
interface GreenTarget
{
public function operateMouth( $type = 0 );
}
// 红枣公司适配器
class ReaAdapter implements RedTarget
{
private $adaptee;
// 只是接受 Toy 的子类
public function __construct( Toy $adaptee )
{
$this->adaptee = $adaptee;
}
public function doMonthOpen()
{
$this->adaptee->openMonth();
}
public function doMonthClose()
{
$this->adaptee->closeMonth();
}
}
// 绿枣公司适配器
class GreenAdapter implements GreenTarget
{
private $adaptee;
public function __construct(Toy $adaptee) {
$this->adaptee=$adaptee;
}
public function operateMouth( $type = 0 )
{
if($type){
$this->adaptee->openMonth();
}else{
$this->adaptee->closeMonth();
}
}
}
class testDriver
{
public function run(){
// 实例化一个玩具
$adaptee_dog = new Dog();
// 红枣公司适配器
echo '红枣公司适配器';
$adapter_red = new ReaAdapter($adaptee_dog);
$adapter_red->doMonthOpen();
$adapter_red->doMonthClose();
// 绿枣公司适配器
$adaptee_green = new GreenAdapter($adaptee_dog);
$adaptee_green->operateMouth(1);
$adaptee_green->operateMouth(0);
}
}
$test = new testDriver();
$test->run();