设计模式(17)mediator模式

 

目的

用中介对象来封装一系列对象交互,使各个对象不需要显式的互相引用,可以独立的改变他们的之间的交互。

mediator

//中介者接口:可以是公共的方法,如Change方法,也可以是小范围的交互方法。  
//同事类定义:比如,每个具体同事类都应该知道中介者对象,也就是每个同事对象都会持有中介者对象的引用,这个功能可定义在这个类中。  
  
//抽象国家  
abstract class Country  
{  
    protected $mediator;  
    public function __construct(UnitedNations $_mediator)  
    {  
        $this->mediator = $_mediator;  
    }  
}  
  
//具体国家类  
//美国  
class USA extends Country  
{  
    function __construct(UnitedNations $mediator)  
    {  
        parent::__construct($mediator);  
    }  
  
    //声明  
    public function Declared($message)  
    {  
        $this->mediator->Declared($message,$this);  
    }  
  
    //获得消息  
    public function GetMessage($message)  
    {  
        echo "美国获得对方消息:$message<br/>";  
    }  
}  
//中国  
class China extends Country  
{  
    public function __construct(UnitedNations $mediator)  
    {  
        parent::__construct($mediator);  
    }  
    //声明  
    public function  Declared($message)  
    {  
        $this->mediator->Declared($message, $this);  
    }  
  
    //获得消息  
    public function GetMessage($message)  
    {  
        echo "中方获得对方消息:$message<br/>";  
    }  
}  
  
//抽象中介者  
//抽象联合国机构  
abstract class UnitedNations  
{  
    //声明  
    public abstract function Declared($message,Country $colleague);  
}  
  
//联合国机构  
class UnitedCommit extends UnitedNations  
{  
    public $countryUsa;  
    public $countryChina;  
  
    //声明  
    public function Declared($message, Country $colleague)  
    {  
        if($colleague==$this->countryChina)  
        {  
            $this->countryUsa->GetMessage($message);  
        }  
        else  
        {  
            $this->countryChina->GetMessage($message);  
        }  
    }  
}  


//测试代码块  
$UNSC = new UnitedCommit();  
$c1 = new USA($UNSC);  
$c2 = new China($UNSC);  
$UNSC->countryChina = $c2;  
$UNSC->countryUsa =$c1;  
$c1->Declared("姚明的篮球打的就是好");  
$c2->Declared("谢谢。");