PHP觀察者模式原理與簡(jiǎn)單實(shí)現(xiàn)方法示例
本文實(shí)例講述了PHP觀察者模式原理與簡(jiǎn)單實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
當(dāng)一個(gè)對(duì)象狀態(tài)發(fā)生改變后,會(huì)影響到其他幾個(gè)對(duì)象的改變,這時(shí)候可以用觀察者模式。像wordpress這樣的應(yīng)用程序中,它容外部開發(fā)組開發(fā)插件,比如用戶授權(quán)的博客統(tǒng)計(jì)插件、積分插件,這時(shí)候可以應(yīng)用觀察者模式,先注冊(cè)這些插件,當(dāng)用戶發(fā)布一篇博文后,就回自動(dòng)通知相應(yīng)的插件更新。
觀察者模式符合接口隔離原則,實(shí)現(xiàn)了對(duì)象之間的松散耦合。
觀察者模式UML圖:

在php SPL中已經(jīng)提供SplSubject和SqlOberver接口
interface SplSubject
{
function attach(SplObserver $observer);
function detach(SplObserver $observer);
function notify();
}
interface SqlObserver
{
function update(SplSubject $subject);
}
下面具體實(shí)現(xiàn)上面例子
class Subject implements SplSubject
{
private $observers;
public function attach(SplObserver $observer)
{
if (!in_array($observer, $this->observers)) {
$this->observers[] = $observer;
}
}
public function detach(SplObserver $observer)
{
if (false != ($index = array_search($observer, $this->observers))) {
unset($this->observers[$index]);
}
}
public function post()
{
//post相關(guān)code
$this->notify();
}
private function notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
public function setCount($count)
{
echo "數(shù)據(jù)量加" . $count;
}
public function setIntegral($integral)
{
echo "積分量加" . $integral;
}
}
class Observer1 implements SplObserver
{
public function update($subject)
{
$subject-> setCount(1);
}
}
class Observer2 implements SplObserver
{
public function update($subject)
{
$subject-> setIntegral(10);
}
}
class Client
{
public function test()
{
$subject = new Subject();
$subject->attach(new Observer1());
$subject->attach(new Observer2());
$subject->post();//輸出:數(shù)據(jù)量加1 積分量加10
}
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP設(shè)計(jì)模式之觀察者模式(Observer)詳細(xì)介紹和代碼實(shí)例
- php設(shè)計(jì)模式 Observer(觀察者模式)
- php觀察者模式應(yīng)用場(chǎng)景實(shí)例詳解
- php設(shè)計(jì)模式之觀察者模式的應(yīng)用詳解
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)觀察者模式(Observer)
- PHP設(shè)計(jì)模式之觀察者模式實(shí)例
- php設(shè)計(jì)模式 Template (模板模式)
- php設(shè)計(jì)模式 DAO(數(shù)據(jù)訪問對(duì)象模式)
- php設(shè)計(jì)模式 Proxy (代理模式)
- php單態(tài)設(shè)計(jì)模式(單例模式)實(shí)例
- PHP觀察者模式示例【Laravel框架中有用到】
相關(guān)文章
php中header設(shè)置常見文件類型的content-type
這篇文章主要介紹了php中header設(shè)置常見文件類型的content-type的相關(guān)資料,需要的朋友可以參考下2015-06-06
PHP直接修改表內(nèi)容DataGrid功能實(shí)現(xiàn)代碼
最近想做一個(gè)通過PHP實(shí)現(xiàn)DataGrid功能的東西,這樣可以直接修改數(shù)據(jù)庫(kù)中表的內(nèi)容,而不用開發(fā)【新增數(shù)據(jù)頁(yè)面】和【編輯頁(yè)面】,本篇首先介紹基于MySQL的使用方法,再簡(jiǎn)單介紹對(duì)于Oracle連接(基于sqlrelay)的二次開發(fā)。2015-09-09
echo, print, printf 和 sprintf 區(qū)別
echo, print, printf 和 sprintf 區(qū)別...2006-12-12
php usort 使用用戶自定義的比較函數(shù)對(duì)二維數(shù)組中的值進(jìn)行排序
這篇文章主要介紹了php usort 使用用戶自定義的比較函數(shù)對(duì)二維數(shù)組中的值進(jìn)行排序,需要的朋友可以參考下2017-05-05
解決php表單重復(fù)提交實(shí)現(xiàn)方法
這篇文章主要介紹了解決php表單重復(fù)提交實(shí)現(xiàn)方法,需要的朋友可以參考下2015-09-09
php絕對(duì)路徑與相對(duì)路徑之間關(guān)系的的分析
php絕對(duì)路徑與相對(duì)路徑之間關(guān)系的的深入研究2010-03-03
解析array splice的移除數(shù)組中指定鍵的值,返回一個(gè)新的數(shù)組
array_splice這個(gè)數(shù)組函數(shù)在w3school的解釋是把數(shù)組中的一部分去掉并用其它值取代。所以我在做數(shù)組移除的時(shí)候沒有考慮到用這個(gè)函數(shù),或者因?yàn)閺膩頉]有用過2013-07-07

