亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2019年05月22日 11:50:30   作者:學(xué)習(xí)筆記666  
這篇文章主要介紹了PHP觀察者模式,結(jié)合實(shí)例形式對(duì)比分析JS觀察者模式實(shí)現(xiàn)方法,給出了php觀察者模式的完整定義與使用操作示例,需要的朋友可以參考下

本文實(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>用觀察者模式切換頁(yè)面風(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基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

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

相關(guān)文章

最新評(píng)論