濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP觀察者模式實(shí)例分析【對比JS觀察者模式】

PHP觀察者模式實(shí)例分析【對比JS觀察者模式】

熱門標(biāo)簽:余姚電話機(jī)器人 電銷機(jī)器人問門薩維品牌my 廣東廣州在怎么申請400電話 咸寧銷售電銷機(jī)器人系統(tǒng) 百度地圖標(biāo)注偏差 外呼系統(tǒng)能給企業(yè)帶來哪些好處 百度地圖怎樣標(biāo)注圖標(biāo) 400電話蘭州申請請 開發(fā)地圖標(biāo)注類網(wǎng)站

本文實(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對象觀察select的變化,selecte變化就會通知這個2個對象,并引起這2個對象的變化,實(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ī)';
        }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è)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • PHP設(shè)計模式之觀察者模式入門與應(yīng)用案例詳解
  • PHP 觀察者模式深入理解與應(yīng)用分析
  • php模式設(shè)計之觀察者模式應(yīng)用實(shí)例分析
  • PHP使用觀察者模式處理異常信息的方法詳解
  • php設(shè)計模式之觀察者模式定義與用法經(jīng)典示例
  • PHP中常用的三種設(shè)計模式詳解【單例模式、工廠模式、觀察者模式】
  • PHP設(shè)計模式之觀察者模式定義與用法分析
  • PHP觀察者模式定義與用法實(shí)例分析
  • 解析PHP觀察者模式Observer

標(biāo)簽:十堰 鷹潭 臨沂 銅陵 衡陽 麗江 巴彥淖爾 重慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP觀察者模式實(shí)例分析【對比JS觀察者模式】》,本文關(guān)鍵詞  PHP,觀察者,模式,實(shí)例分析,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP觀察者模式實(shí)例分析【對比JS觀察者模式】》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP觀察者模式實(shí)例分析【對比JS觀察者模式】的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    福清市| 青浦区| 项城市| 独山县| 山东| 崇信县| 怀安县| 九台市| 青海省| 泽州县| 长寿区| 呼伦贝尔市| 健康| 平武县| 张家口市| 凤翔县| 当阳市| 特克斯县| 衡山县| 区。| 措美县| 车险| 介休市| 三河市| 健康| 泾源县| 巨鹿县| 鸡东县| 手游| 平罗县| 唐海县| 朔州市| 繁峙县| 大荔县| 长乐市| 阜城县| 衡南县| 河北区| 延寿县| 通城县| 昭通市|