您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在PHP中應(yīng)用觀察者模式,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
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對象觀察select的變化,selecte變化就會(huì)通知這個(gè)2個(gè)對象,并引起這2個(gè)對象的變化,實(shí)現(xiàn)觀察者模式。 --> <h2>用觀察者模式切換頁面風(fēng)格</h2> <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中應(yīng)用觀察者模式就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。