新聞中心
PHP中怎么實(shí)現(xiàn)訪問者模式,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
涉縣ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
Gof類圖及解釋
GoF定義:表示一個(gè)作用于某對(duì)象結(jié)構(gòu)中的各元素的操作。它使你可以在不改變各元素的類的前提下定義作用于這些元素的新操作
GoF類圖
代碼實(shí)現(xiàn)
interface Visitor { public function VisitConcreteElementA(ConcreteElementA $a); function VisitConcreteElementB(ConcreteElementB $b); } class ConcreteVisitor1 implements Visitor { public function VisitConcreteElementA(ConcreteElementA $a) { echo get_class($a) . "被" . get_class($this) . "訪問", PHP_EOL; } public function VisitConcreteElementB(ConcreteElementB $b) { echo get_class($b) . "被" . get_class($this) . "訪問", PHP_EOL; } } class ConcreteVisitor2 implements Visitor { public function VisitConcreteElementA(ConcreteElementA $a) { echo get_class($a) . "被" . get_class($this) . "訪問", PHP_EOL; } public function VisitConcreteElementB(ConcreteElementB $b) { echo get_class($b) . "被" . get_class($this) . "訪問", PHP_EOL; } }
抽象的訪問者接口及兩個(gè)具體實(shí)現(xiàn)??梢钥醋魇且患倚煽趤砦覀兗易骺涂?!
interface Element { public function Accept(Visitor $v); } class ConcreteElementA implements Element { public function Accept(Visitor $v) { $v->VisitConcreteElementA($this); } public function OperationA() { } } class ConcreteElementB implements Element { public function Accept(Visitor $v) { $v->VisitConcreteElementB($this); } public function OperationB() { } }
元素抽象及實(shí)現(xiàn),也可以看作是要訪問的實(shí)體。當(dāng)然就是我和我媳婦啦。
class ObjectStructure { private $elements = []; public function Attach(Element $element) { $this->elements[] = $element; } public function Detach(Element $element) { $position = 0; foreach ($this->elements as $e) { if ($e == $element) { unset($this->elements[$position]); break; } $position++; } } public function Accept(Visitor $visitor) { foreach ($this->elements as $e) { $e->Accept($visitor); } } }
這是一個(gè)對(duì)象結(jié)構(gòu),用于保存元素實(shí)體并進(jìn)行訪問調(diào)用。大家在客廳里見面,互相寒暄嘛,這里就是個(gè)客廳
$o = new ObjectStructure(); $o->Attach(new ConcreteElementA()); $o->Attach(new ConcreteElementB()); $v1 = new ConcreteVisitor1(); $v2 = new ConcreteVisitor2(); $o->Accept($v1); $o->Accept($v2);
客戶端的調(diào)用,總算讓大家正式見面了,互相介紹握手。一次訪問就愉快的完成了。
讓訪問者調(diào)用指定的元素。這里需要注意的,訪問者調(diào)用元素的行為一般是固定的,很少會(huì)改變的。也就是VisitConcreteElementA()、VisitConcreteElementB()這兩個(gè)方法。也就是定義對(duì)象結(jié)構(gòu)的類很少改變,但經(jīng)常需要在此結(jié)構(gòu)上定義新的操作時(shí),會(huì)使用訪問者模式
需要對(duì)一個(gè)對(duì)象結(jié)構(gòu)中的對(duì)象進(jìn)行很多不同的并且不相關(guān)的操作,而你想避免讓這些操作“污染”這些對(duì)象的類時(shí),適用于訪問者模式
訪問者模式適合數(shù)據(jù)結(jié)構(gòu)不變化的情況。所以,它是一種平常你用不上,但一旦需要的時(shí)候就只能用這種模式的模式。GoF:“大多時(shí)候你并不需要訪問者模式,但當(dāng)一旦你需要訪問者模式時(shí),那就是真的需要它了”。因?yàn)楹苌儆袛?shù)據(jù)結(jié)構(gòu)不發(fā)生變化的情況
訪問者模式的一些優(yōu)缺點(diǎn):易于增加新的操作;集中相關(guān)的操作而分離無關(guān)的操作;增加新的ConcreteElement類很困難;通過類層次進(jìn)行訪問;累積狀態(tài);破壞封裝
我們公司的賬務(wù),只有收入和支出兩項(xiàng)(Element),但是不同的部門(Visitor)訪問的時(shí)候會(huì)給出不同的內(nèi)容。比如我查看的時(shí)候只需要查看每月或每季度的匯總數(shù)據(jù)即可,財(cái)務(wù)總監(jiān)則需要詳細(xì)的收支記錄,而會(huì)計(jì)在做賬時(shí)更是需要完整的明細(xì)??梢?,公司的運(yùn)營還真的是需要非常廣泛的知識(shí)的,不僅是管理能力,賬務(wù)知識(shí)也是必要了解的內(nèi)容?。?/em>
完整代碼:https://github.com/zhangyue0503/designpatterns-php/blob/master/23.visitor/source/visitor.php
實(shí)例
最后一個(gè)模式的例子還是回到我們的信息發(fā)送上來。同樣的還是多個(gè)服務(wù)商,它們作為訪問者需要去使用各自的短信發(fā)送及APP推送接口。這時(shí),就可以使用訪問者模式來進(jìn)行操作,實(shí)現(xiàn)這些訪問者的全部操作。
訪問者模式信息發(fā)送
完整源碼:https://github.com/zhangyue0503/designpatterns-php/blob/master/23.visitor/source/visitor-msg.php
PushMsg($this); } } class SendMessage implements Message { public function Msg(ServiceVisitor $v) { echo '短信腳本啟動(dòng):'; $v->SendMsg($this); } } class ObjectStructure { private $elements = []; public function Attach(Message $element) { $this->elements[] = $element; } public function Detach(Message $element) { $position = 0; foreach ($this->elements as $e) { if ($e == $element) { unset($this->elements[$position]); break; } $position++; } } public function Accept(ServiceVisitor $visitor) { foreach ($this->elements as $e) { $e->Msg($visitor); } } } $o = new ObjectStructure(); $o->Attach(new PushMessage()); $o->Attach(new SendMessage()); $v1 = new AliYun(); $v2 = new JiGuang(); $o->Accept($v1); $o->Accept($v2);
看完上述內(nèi)容,你們掌握PHP中怎么實(shí)現(xiàn)訪問者模式的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
新聞標(biāo)題:PHP中怎么實(shí)現(xiàn)訪問者模式
標(biāo)題路徑:http://fisionsoft.com.cn/article/pjpjgs.html