PHP设计模式之中介者模式(Mediator Pattern)入门与应(2)

  • 中介者模式很好的处理了这种情况,通过中介者类,CD类中只要调用中介者这个类,就能同步更新这些数据。
  • 代码实例如下:

    <?php
    class CD {
     public $band = '';
     public $title = '';
     protected $_mediator;
    
     public function __construct(MusicContainerMediator $mediator = NULL) {
     $this->_mediator = $mediator;
     }
    
     public function save() {
     //具体实现待定
     var_dump($this);
     }
    
     public function changeBandName($bandname) {
     if ( ! is_null($this->_mediator)) {
      $this->_mediator->change($this, array("band" => $bandname));
     }
     $this->band = $bandname;
     $this->save();
     }
    }
    //MP3Archive类
    class MP3Archive {
     protected $_mediator;
    
     public function __construct(MusicContainerMediator $mediator = NULL) {
     $this->_mediator = $mediator;
     }
    
     public function save() {
     //具体实现待定
     var_dump($this);
     }
    
     public function changeBandName($bandname) {
     if ( ! is_null($this->_mediator)) {
      $this->_mediator->change($this, array("band" => $bandname));
     }
     $this->band = $bandname;
     $this->save();
     }
    
    }
    
     //中介者类
    class MusicContainerMediator {
     protected $_containers = array();
    
     public function __construct() {
     $this->_containers[] = "CD";
     $this->_containers[] = "MP3Archive";
     }
    
     public function change($originalObject, $newValue) {
     $title = $originalObject->title;
     $band = $originalObject->band;
     foreach ($this->_containers as $container) {
      if ( ! ($originalObject instanceof $container)) {
      $object = new $container;
      $object->title = $title;
      $object->band = $band;
      foreach ($newValue as $key => $val) {
       $object->$key = $val;
      }
      $object->save();
      }
     }
     }
    }
    
    //测试实例
    $titleFromDB = "Waste of a Rib";
    $bandFromDB = "Never Again";
    $mediator = new MusicContainerMediator();
    $cd = new CD($mediator);
    $cd->title = $titleFromDB;
    $cd->band = $bandFromDB;
    $cd->changeBandName("Maybe Once More");
    
    

    运行结果:

    object(MP3Archive)#3 (3) {
      ["_mediator":protected]=>
      NULL
      ["title"]=>
      string(14) "Waste of a Rib"
      ["band"]=>
      string(15) "Maybe Once More"
    }
    object(CD)#2 (3) {
      ["band"]=>
      string(15) "Maybe Once More"
      ["title"]=>
      string(14) "Waste of a Rib"
      ["_mediator":protected]=>
      object(MusicContainerMediator)#1 (1) {
        ["_containers":protected]=>
        array(2) {
          [0]=>
          string(2) "CD"
          [1]=>
          string(10) "MP3Archive"
        }
      }
    }

    还有个实例,一起看下吧:

    //抽象同事类,家教
    abstract class Tutor{
      protected $message;  //个人信息
      protected $mediator; //为家教服务的中介机构
      function __construct($message,Mediator $mediator){
        $this->message = $message;
        $this->mediator = $mediator;
      }
      //获取个人信息
      function getMessage(){
        return $this->message;
      }
      //找学生
      abstract function findStudent();
    }
    //具体同事类,大学生家教
    class UndergraduateTutor extends Tutor{
      //家教类型
      public $type = "UndergraduateTutor";
    
      function __construct($message,Mediator $mediator){
         parent::__construct($message,$mediator);
      }
      //找学生,让中介机构代为寻找
      function findStudent(){
         $this->mediator->matchStudent($this);
      }
    }
    //具体同事类,高中生家教
    class SeniorStudentTutor extends Tutor{
      //家教类型
      public $type = "SeniorStudentTutor";
    
      function __construct($message,Mediator $mediator){
         parent::__construct($message,$mediator);
      }
      //找学生,让中介机构代为寻找
      function findStudent(){
         $this->mediator->matchStudent($this);
      }
    }
    //具体同事类,初中生家教
    class MiddleStudentTutor extends Tutor{
      //家教类型
      public $type = "MiddleStudentTutor";
    
      function __construct($message,Mediator $mediator){
         parent::__construct($message,$mediator);
      }
      //找学生,让中介机构代为寻找
      function findStudent(){
         $this->mediator->matchStudent($this);
      }
    }
    
    //抽象中介类
    abstract class AbstractMediator{
      abstract function matchStudent(Tutor $tutor);
    }
    //具体中介类,为家教匹配合适的学生
    class Mediator extends AbstractMediator{
      //定义其服务的所有家教,不在范围内的不服务
      private $serveObject = array("UndergraduateTutor","SeniorStudentTutor","MiddleStudentTutor");
      //匹配学生
      function matchStudent(Tutor $tutor){
         for($i=0;$i<count($this->serveObject);$i++){
           if($tutor->type == $this->serveObject[$i]){
             $message = $tutor->getMessage();
             echo "该家教个人信息为".print_r($message)."<br/>将为其匹配合适的学生";
             break;
           }
         }
         if($i>=count($this->serveObject)){
           echo "该家教不在我中介机构服务范围内";
         }
      }
    }
    
    //测试
    $mediator = new Mediator();
    $undergraduateTutor = new UndergraduateTutor(array("name"=>"张三","age"=>22),$mediator);
    $undergraduateTutor->findStudent();
    //结果:该家教个人信息为 Array ( [name] => 张三 [age] => 22 ),将为其匹配合适的学生
    
    
          

    内容版权声明:除非注明,否则皆为本站原创文章。

    转载注明出处:http://www.heiqu.com/1955.html