PHP設(shè)計模式之中介者模式(Mediator Pattern)入門與應(yīng)用案例詳解
本文實例講述了PHP設(shè)計模式之中介者模式(Mediator Pattern)。分享給大家供大家參考,具體如下:
咱們先來看下中介者模式(Mediator Pattern)的定義,它就是,用一個中介對象來封裝一系列的對象交互,中介者使各對象不需要顯式地相互引用,從而使其耦合松散,而且可以獨立地改變它們之間的交互,這種模式又稱為調(diào)停者模式,它是一種對象行為型模式。
我們先來看用戶與用戶直接聊天的設(shè)計方案。
在這個方案設(shè)計的過程中,我們可以發(fā)揮想象,用戶對象之間存在很強的關(guān)聯(lián)性,將導(dǎo)致系統(tǒng)出現(xiàn)如下問題:
- 系統(tǒng)結(jié)構(gòu)復(fù)雜:對象之間存在大量的相互關(guān)聯(lián)和調(diào)用,若有一個對象發(fā)生變化,則需要跟蹤和該對象關(guān)聯(lián)的其他所有對象,并進行適當處理
- 對象可重用性差:由于一個對象和其他對象具有很強的關(guān)聯(lián),若沒有其他對象的支持,一個對象很難被另一個系統(tǒng)或模塊重用,這些對象表現(xiàn)出來更像一個不可分割的整體,職責較為混亂
- 系統(tǒng)擴展性低:增加一個新的對象需要在原有相關(guān)對象上增加引用,增加新的引用關(guān)系也需要調(diào)整原有對象,系統(tǒng)耦合度很高,對象操作很不靈活,擴展性差
然而,在面向?qū)ο蟮能浖O(shè)計與開發(fā)過程中,根據(jù)“單一職責原則”,我們應(yīng)該盡量將對象細化,使其只負責或呈現(xiàn)單一的職責,但是,對于一個模塊來說,可能由很多對象構(gòu)成,而且這些對象之間可能存在相互的引用,為了減少對象兩兩之間復(fù)雜的引用關(guān)系,使之成為一個松耦合的系統(tǒng),我們需要使用中介者模式,這就是我們使用中介者模式的動機。
來看下中介者模式包含的角色:
- Mediator: 抽象中介者,在里面定義了各個同事之間相互交互所需要的方法。
- ConcreteMediator: 具體中介者,它需要了解并為維護每個同事對象,并負責具體的協(xié)調(diào)各個同事對象的交互關(guān)系。
- Colleague:抽象同事類,通常實現(xiàn)成為抽象類,主要負責約束同事對象的類型,并實現(xiàn)一些具體同事類之間的公共功能
- ConcreteColleague:具體同事類,實現(xiàn)自己的業(yè)務(wù),需要與其他同事對象交互,就通知中介對象,中介對象會負責后續(xù)的交互
再來看下圖片感受下:
來看一個完整的實例:
- 我們有一個CD類和一個MP3類,兩個類的結(jié)構(gòu)相似。
- 我們需要在CD類更新的時候,同步更新MP3類。
- 傳統(tǒng)的做法就是在CD類中實例化MP3類,然后去更新,但是這么做的話,代碼就會很難維護,如果新增一個同樣的MP4類,那么就沒法處理了。
- 中介者模式很好的處理了這種情況,通過中介者類,CD類中只要調(diào)用中介者這個類,就能同步更新這些數(shù)據(jù)。
代碼實例如下:
<?php class CD { public $band = ''; public $title = ''; protected $_mediator; public function __construct(MusicContainerMediator $mediator = NULL) { $this->_mediator = $mediator; } public function save() { //具體實現(xiàn)待定 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() { //具體實現(xiàn)待定 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");
運行結(jié)果:
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; //為家教服務(wù)的中介機構(gòu) function __construct($message,Mediator $mediator){ $this->message = $message; $this->mediator = $mediator; } //獲取個人信息 function getMessage(){ return $this->message; } //找學(xué)生 abstract function findStudent(); } //具體同事類,大學(xué)生家教 class UndergraduateTutor extends Tutor{ //家教類型 public $type = "UndergraduateTutor"; function __construct($message,Mediator $mediator){ parent::__construct($message,$mediator); } //找學(xué)生,讓中介機構(gòu)代為尋找 function findStudent(){ $this->mediator->matchStudent($this); } } //具體同事類,高中生家教 class SeniorStudentTutor extends Tutor{ //家教類型 public $type = "SeniorStudentTutor"; function __construct($message,Mediator $mediator){ parent::__construct($message,$mediator); } //找學(xué)生,讓中介機構(gòu)代為尋找 function findStudent(){ $this->mediator->matchStudent($this); } } //具體同事類,初中生家教 class MiddleStudentTutor extends Tutor{ //家教類型 public $type = "MiddleStudentTutor"; function __construct($message,Mediator $mediator){ parent::__construct($message,$mediator); } //找學(xué)生,讓中介機構(gòu)代為尋找 function findStudent(){ $this->mediator->matchStudent($this); } } //抽象中介類 abstract class AbstractMediator{ abstract function matchStudent(Tutor $tutor); } //具體中介類,為家教匹配合適的學(xué)生 class Mediator extends AbstractMediator{ //定義其服務(wù)的所有家教,不在范圍內(nèi)的不服務(wù) private $serveObject = array("UndergraduateTutor","SeniorStudentTutor","MiddleStudentTutor"); //匹配學(xué)生 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/>將為其匹配合適的學(xué)生"; break; } } if($i>=count($this->serveObject)){ echo "該家教不在我中介機構(gòu)服務(wù)范圍內(nèi)"; } } } //測試 $mediator = new Mediator(); $undergraduateTutor = new UndergraduateTutor(array("name"=>"張三","age"=>22),$mediator); $undergraduateTutor->findStudent(); //結(jié)果:該家教個人信息為 Array ( [name] => 張三 [age] => 22 ),將為其匹配合適的學(xué)生
好啦,本次記錄就到這里了。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
上一篇:在 Laravel 6 中緩存數(shù)據(jù)庫查詢結(jié)果的方法
欄 目:PHP編程
下一篇:PHP設(shè)計模式之策略模式(Strategy)入門與應(yīng)用案例詳解
本文標題:PHP設(shè)計模式之中介者模式(Mediator Pattern)入門與應(yīng)用案例詳解
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/11038.html
您可能感興趣的文章
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂碼
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)語句
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)據(jù)函數(shù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方案
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文件的語句


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法