PHP觀察者模式實(shí)例分析【對(duì)比JS觀察者模式】
本文實(shí)例講述了PHP觀察者模式。分享給大家供大家參考,具體如下:
1.用js實(shí)現(xiàn)觀察者模式
<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> div{width: 100px;height: 100px;border: 1px #999 solid;margin-bottom: 5px;} </style> </head> <body> <!-- 我們讓div對(duì)象觀察select的變化,selecte變化就會(huì)通知這個(gè)2個(gè)對(duì)象,并引起這2個(gè)對(duì)象的變化,實(shí)現(xiàn)觀察者模式。 --> <h1>用觀察者模式切換頁面風(fēng)格</h1> <select> <option value="male">男式風(fēng)格</option> <option value="female">女士風(fēng)格</option> </select> <button onclick="t1()">觀察學(xué)習(xí)區(qū)</button> <button onclick="t2()">不觀察學(xué)習(xí)區(qū)</button> <div id="content">我是內(nèi)容</div> <div id="ad">我是廣告</div> <div id="study">學(xué)習(xí)</div> </body> <script type="text/javascript"> var sel = document.getElementsByTagName('select')[0]; sel.observers = {}; sel.attach = function(key,obj){ this.observers[key] = obj; } sel.detach = function(key){ delete this.observers[key]; } sel.onchange = sel.notify = function(){ for(var key in this.observers){ this.observers[key].update(this); } } //客戶端 var content = document.getElementById('content'); var ad = document.getElementById('ad'); content.update = function(ob){ if (ob.value == 'male') { this.style.backgroundColor = 'gray'; }else if(ob.value == 'female'){ this.style.backgroundColor = 'pink'; } } ad.update = function(ob){ if (ob.value == 'male') { this.innerHTML = '汽車'; }else if(ob.value == 'female'){ this.innerHTML = '減肥'; } } //讓content觀察select的變化 sel.attach('content',content); sel.attach('ad',ad); //新增監(jiān)聽study區(qū) var study = document.getElementById('study'); study.update = function(ob){ if (ob.value == 'male') { this.innerHTML = '學(xué)習(xí)計(jì)算機(jī)'; }else if(ob.value == 'female'){ this.innerHTML = '學(xué)習(xí)美容'; } } sel.attach('study',study); function t1(){ sel.attach('study',study); } function t2(){ sel.detach('study'); } </script> </html>
2.用php實(shí)現(xiàn)觀察模式
<?php //php實(shí)現(xiàn)觀察者 //php5中提供觀察者observer和被觀察者subject的接口 class User implements SplSubject { public $lognum; public $hobby; protected $observers = null; public function __construct($hobby) { $this->lognum = rand(1,10); $this->hobby = $hobby; $this->observers = new SplObjectStorage(); } public function login() { //操作session等 $this->notify(); } public function attach(SPLObserver $observer) { $this->observers->attach($observer); } public function detach(SPLObserver $observer) { $this->observers->detach($observer); } public function notify() { $this->observers->rewind(); while ($this->observers->valid()) { $observer = $this->observers->current(); $observer->update($this); $this->observers->next(); } } } //用戶安全登錄模塊 class Safe implements SPLObserver { public function update(SplSubject $subject) { if ($subject->lognum < 3) { echo '這是第' . $subject->lognum . '次安全登錄<br>'; }else{ echo '這是第' . $subject->lognum . '次登錄,異常<br>'; } } } //廣告模塊 class Ad implements SPLObserver { public function update(SplSubject $subject) { if ($subject->hobby == 'sports') { echo '英超開始啦<br>'; }else{ echo '好好學(xué)習(xí)<br>'; } } } //實(shí)施觀察 // $user = new User('sports'); $user = new User('study'); $user->attach(new Safe()); $user->attach(new Ad()); $user->login();//登錄
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP封裝XML和JSON格式數(shù)據(jù)接口操作示例
這篇文章主要介紹了PHP封裝XML和JSON格式數(shù)據(jù)接口操作,結(jié)合實(shí)例形式分析了php針對(duì)xml與json格式數(shù)據(jù)接口封裝相關(guān)操作技巧,需要的朋友可以參考下2019-03-03php 自寫函數(shù)代碼 獲取關(guān)鍵字 去超鏈接
根據(jù)權(quán)重獲取關(guān)鍵字 去掉文章中的超鏈接簡單,簡潔2010-02-02一個(gè)php生成16位隨機(jī)數(shù)的代碼(兩種方法)
這篇文章分享一個(gè)php生成16位隨機(jī)數(shù)的代碼,php生成隨機(jī)數(shù)的二種方法,但簡單,但很實(shí)用,需要的朋友可以參考下2014-09-09PHP讀取網(wǎng)頁文件內(nèi)容的實(shí)現(xiàn)代碼(fopen,curl等)
php小偷程序中經(jīng)常需要獲取遠(yuǎn)程網(wǎng)頁的內(nèi)容,下面是一些實(shí)現(xiàn)代碼,需要的朋友可以慘況下。2011-06-06PHP擴(kuò)展之kafka安裝應(yīng)用案例詳解
這篇文章主要介紹了PHP擴(kuò)展之kafka安裝應(yīng)用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09