PHP設(shè)計(jì)模式之中介者模式淺析
中介者模式
中介者模式(Mediator Pattern)是一種常用的設(shè)計(jì)模式,用于解決各個(gè)對(duì)象之間的復(fù)雜依賴關(guān)系,使得各個(gè)對(duì)象之間可以獨(dú)立地改變自己的行為,而不需要與其他對(duì)象發(fā)生直接的交互。中介者模式通過引入中介者對(duì)象來封裝一系列的對(duì)象交互,中介者對(duì)象可以協(xié)調(diào)各個(gè)對(duì)象之間的行為,從而減少對(duì)象之間的耦合度。 中介者模式包含以下角色:
- 抽象中介者(Mediator):定義了各個(gè)對(duì)象之間的通信接口,可以是抽象類或接口。
- 具體中介者(ConcreteMediator):實(shí)現(xiàn)抽象中介者接口,負(fù)責(zé)協(xié)調(diào)各個(gè)對(duì)象之間的通信。
- 抽象同事類(Colleague):定義了各個(gè)對(duì)象之間的通信接口,可以是抽象類或接口。
- 具體同事類(ConcreteColleague):實(shí)現(xiàn)抽象同事類接口,維護(hù)與其他對(duì)象之間的通信關(guān)系。
PHP中的中介者模式
PHP中的中介者模式可以通過實(shí)現(xiàn)抽象中介者和抽象同事類來實(shí)現(xiàn)。下面是一個(gè)簡(jiǎn)單的示例:
<?php // 抽象中介者 abstract class Mediator { abstract public function send($message, $colleague); } // 抽象同事類 abstract class Colleague { protected $mediator; public function __construct(Mediator $mediator) { $this->mediator = $mediator; } abstract public function send($message); abstract public function receive($message); } // 具體中介者 class ConcreteMediator extends Mediator { private $colleague1; private $colleague2; public function setColleague1(Colleague $colleague) { $this->colleague1 = $colleague; } public function setColleague2(Colleague $colleague) { $this->colleague2 = $colleague; } public function send($message, $colleague) { if ($colleague == $this->colleague1) { $this->colleague2->receive($message); } else { $this->colleague1->receive($message); } } } // 具體同事類 class ConcreteColleague1 extends Colleague { public function send($message) { $this->mediator->send($message, $this); } public function receive($message) { echo "ConcreteColleague1 received message: $message\n"; } } class ConcreteColleague2 extends Colleague { public function send($message) { $this->mediator->send($message, $this); } public function receive($message) { echo "ConcreteColleague2 received message: $message\n"; } }
以上代碼中,我們定義了抽象中介者Mediator
和抽象同事類Colleague
,并分別實(shí)現(xiàn)了具體中介者ConcreteMediator
和具體同事類ConcreteColleague1
、ConcreteColleague2
。在具體中介者ConcreteMediator
中保存了兩個(gè)具體同事類的引用,通過send()
方法來實(shí)現(xiàn)兩個(gè)具體同事類之間的通信。具體同事類中實(shí)現(xiàn)了send()
和receive()
方法,通過中介者來發(fā)送和接收消息。 我們可以使用以下代碼來測(cè)試中介者模式:
<?php $mediator = new ConcreteMediator; $colleague1 = new ConcreteColleague1($mediator); $colleague2 = new ConcreteColleague2($mediator); $mediator->setColleague1($colleague1); $mediator->setColleague2($colleague2); $colleague1->send("Hello, colleague2!"); $colleague2->send("Hi, colleague1!");
輸出結(jié)果:
ConcreteColleague2 received message: Hello, colleague2!
ConcreteColleague1 received message: Hi, colleague1!
以上就是PHP中中介者模式的簡(jiǎn)單介紹,希望對(duì)你有所幫助。
到此這篇關(guān)于PHP設(shè)計(jì)模式之中介者模式淺析的文章就介紹到這了,更多相關(guān)PHP中介者模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ThinkPHP模板標(biāo)簽eq if 中區(qū)分0,null,false的方法
下面小編就為大家?guī)硪黄猅hinkPHP模板標(biāo)簽eq if 中區(qū)分0,null,false的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03php程序內(nèi)部post數(shù)據(jù)的方法
這篇文章主要介紹了php程序內(nèi)部post數(shù)據(jù)的方法,涉及curl的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03php使用str_replace替換多維數(shù)組的實(shí)現(xiàn)方法分析
這篇文章主要介紹了php使用str_replace替換多維數(shù)組的實(shí)現(xiàn)方法,結(jié)合具體實(shí)例對(duì)比分析了php針對(duì)多維數(shù)組的遍歷與替換操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2017-06-06非集成環(huán)境的php運(yùn)行環(huán)境(Apache配置、Mysql)搭建安裝圖文教程
這篇文章主要介紹了非集成環(huán)境的php運(yùn)行環(huán)境(Apache配置、Mysql)搭建安裝圖文教程,感興趣的小伙伴們可以參考一下2016-04-04PHP 截取字符串函數(shù)整理(支持gb2312和utf-8)
常見的 PHP 截取字符串函數(shù)整理,支持gb2312和utf-8編碼,方法php開發(fā)中需要用到截取字符串的問題。2010-02-02PHP extract 將數(shù)組拆分成多個(gè)變量的函數(shù)
extract()函數(shù)提取關(guān)聯(lián)數(shù)組(對(duì)數(shù)字索引數(shù)組無效)每對(duì)key和value,生成以key為變量名、value為對(duì)應(yīng)值的多組新變量。2010-06-06