知识屋:更实用的电脑技术知识网站
所在位置:首页 > 编程技术 > PHP编程

PHP设计模式系列 - 中介者模式

发布时间:2015-05-27 19:33:48作者:知识屋

中介者模式
中介者模式用于开发一个对象,这个对象能够在类似对象相互之间不直接相互的情况下传送或者调解对这些对象的集合的修改。一般处理具有类似属性,需要保持同步的非耦合对象时,最佳的做法就是中介者模式。PHP中不是特别常用的设计模式。
设计场景:
我们有一个CD类和一个MP3类,两个类的结构相似。
我们需要在CD类更新的时候,同步更新MP3类。
传统的做法就是在CD类中实例化MP3类,然后去更新,但是这么做的话,代码就会很难维护,如果新增一个同样的MP4类,那么就没法处理了。
中介者模式很好的处理了这种情况,通过中介者类,CD类中只要调用中介者这个类,就能同步更新这些数据。
我们的phpwind论坛中,之前有用到过这个设计模式。
代码:
[php]
<?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");   
作者:initphp
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜