PHP設(shè)計模式之觀察者模式定義與用法分析
本文實例講述了PHP設(shè)計模式之觀察者模式定義與用法。分享給大家供大家參考,具體如下:
觀察者模式 當(dāng)一個對象的狀態(tài)發(fā)生改變時,依賴他的對象會全部收到通知,并自動更新
場景:當(dāng)一個事件發(fā)生后,要執(zhí)行一連串更新操作,傳統(tǒng)的編程方式,就是在事件的代碼之后直接加入處理邏輯,當(dāng)更新邏輯增多之后,代碼變得難以維護,這種方式是耦合式的,侵入式的,增加新的邏輯需要改變事件主題的代碼
觀察者模式實現(xiàn)了低耦合,非侵入式的通知與更新
abstract class EventGenerator { private $ObServers = []; //增加觀察者 public function add(ObServer $ObServer) { $this->ObServers[] = $ObServer; } //事件通知 public function notify() { foreach ($this->ObServers as $ObServer) { $ObServer->update(); } } } /** * 觀察者接口類 * Interface ObServer */ interface ObServer { public function update($event_info = null); } /** * 觀察者1 */ class ObServer1 implements ObServer { public function update($event_info = null) { echo "觀察者1 收到執(zhí)行通知 執(zhí)行完畢!\n"; } } /** * 觀察者1 */ class ObServer2 implements ObServer { public function update($event_info = null) { echo "觀察者2 收到執(zhí)行通知 執(zhí)行完畢!\n"; } } /** * 事件 * Class Event */ class Event extends EventGenerator { /** * 觸發(fā)事件 */ public function trigger() { //通知觀察者 $this->notify(); } } //創(chuàng)建一個事件 $event = new Event(); //為事件增加旁觀者 $event->add(new ObServer1()); $event->add(new ObServer2()); //執(zhí)行事件 通知旁觀者 $event->trigger();
運行結(jié)果:
觀察者1 收到執(zhí)行通知 執(zhí)行完畢!
觀察者2 收到執(zhí)行通知 執(zhí)行完畢!
1 抽象的事件產(chǎn)生類,定義一個添加觀察者方法,和通知方法(執(zhí)行觀察者方法)
2 定義觀察者接口,實現(xiàn)方法 ,觀察者實現(xiàn)
3 定義具體實現(xiàn)類繼承抽象事件,實現(xiàn)通知方法
4 創(chuàng)建對象,增加旁觀者,更新
具體注冊實例
<?php /** * 3.1php設(shè)計模式-觀測者模式 * 3.1.1概念:其實觀察者模式這是一種較為容易去理解的一種模式吧,它是一種事件系統(tǒng),意味 * 著這一模式允許某個類觀察另一個類的狀態(tài),當(dāng)被觀察的類狀態(tài)發(fā)生改變的時候, * 觀察類可以收到通知并且做出相應(yīng)的動作;觀察者模式為您提供了避免組件之間 * 緊密耦合的另一種方法 * 3.1.2關(guān)鍵點: * 1.被觀察者->追加觀察者;->一處觀察者;->滿足條件時通知觀察者;->觀察條件 * 2.觀察者 ->接受觀察方法 * 3.1.3缺點: * 3.1.4觀察者模式在PHP中的應(yīng)用場合:在web開發(fā)中觀察者應(yīng)用的方面很多 * 典型的:用戶注冊(驗證郵件,用戶信息激活),購物網(wǎng)站下單時郵件/短信通知等 * 3.1.5php內(nèi)部的支持 * SplSubject 接口,它代表著被觀察的對象, * 其結(jié)構(gòu): * interface SplSubject * { * public function attach(SplObserver $observer); * public function detach(SplObserver $observer); * public function notify(); * } * SplObserver 接口,它代表著充當(dāng)觀察者的對象, * 其結(jié)構(gòu): * interface SplObserver * { * public function update(SplSubject $subject); * } */ /** * 用戶登陸-詮釋觀察者模式 */ class User implements SplSubject { //注冊觀察者 public $observers = array(); //動作類型 CONST OBSERVER_TYPE_REGISTER = 1;//注冊 CONST OBSERVER_TYPE_EDIT = 2;//編輯 /** * 追加觀察者 * @param SplObserver $observer 觀察者 * @param int $type 觀察類型 */ public function attach(SplObserver $observer, $type) { $this->observers[$type][] = $observer; } /** * 去除觀察者 * @param SplObserver $observer 觀察者 * @param int $type 觀察類型 */ public function detach(SplObserver $observer, $type) { if($idx = array_search($observer, $this->observers[$type], true)) { unset($this->observers[$type][$idx]); } } /** * 滿足條件時通知觀察者 * @param int $type 觀察類型 */ public function notify($type) { if(!empty($this->observers[$type])) { foreach($this->observers[$type] as $observer) { $observer->update($this); } } } /** * 添加用戶 * @param str $username 用戶名 * @param str $password 密碼 * @param str $email 郵箱 * @return bool */ public function addUser() { //執(zhí)行sql //數(shù)據(jù)庫插入成功 $res = true; //調(diào)用通知觀察者 $this->notify(self::OBSERVER_TYPE_REGISTER); return $res; } /** * 用戶信息編輯 * @param str $username 用戶名 * @param str $password 密碼 * @param str $email 郵箱 * @return bool */ public function editUser() { //執(zhí)行sql //數(shù)據(jù)庫更新成功 $res = true; //調(diào)用通知觀察者 $this->notify(self::OBSERVER_TYPE_EDIT); return $res; } } /** * 觀察者-發(fā)送郵件 */ class Send_Mail implements SplObserver { /** * 相應(yīng)被觀察者的變更信息 * @param SplSubject $subject */ public function update(SplSubject $subject) { $this->sendMail($subject->email, $title, $content); } /** *發(fā)送郵件 *@param str $email 郵箱地址 *@param str $title 郵件標(biāo)題 *@param str $content 郵件內(nèi)容 */ public function sendEmail($email, $title, $content) { //調(diào)用郵件接口,發(fā)送郵件 } } ?>
更多關(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è)計有所幫助。
相關(guān)文章
php版微信數(shù)據(jù)統(tǒng)計接口用法示例
這篇文章主要介紹了php版微信數(shù)據(jù)統(tǒng)計接口用法,結(jié)合實例形式分析了php微信數(shù)據(jù)統(tǒng)計接口功能及相關(guān)的使用技巧,需要的朋友可以參考下2016-10-10php實現(xiàn)mysql備份恢復(fù)分卷處理的方法
這篇文章主要介紹了php實現(xiàn)mysql備份恢復(fù)分卷處理的方法,包括完整的MySQL備份恢復(fù)類文件及用法實例,注釋包含了詳盡的用法說明,是非常實用的技巧,需要的朋友可以參考下2014-12-12