Rumah > pembangunan bahagian belakang > tutorial php > PHP 观察者模式_PHP教程

PHP 观察者模式_PHP教程

WBOY
Lepaskan: 2016-07-20 11:14:35
asal
1232 orang telah melayarinya

<span //</span><span 被观察者抽象类</span>
<span class</span> Observed <span implements</span><span  SplSubject{
    </span><span protected</span> <span $_name</span><span ;
    </span><span protected</span> <span $_observers</span><span ;

    </span><span //</span><span 实例化,生成一个观察者对象</span>
    <span public</span> <span function</span><span  __construct(){
        </span><span $this</span>->_observers = <span new</span><span  SplObjectStorage();
    }

    </span><span //</span><span  添加观察者对象</span>
    <span public</span> <span function</span> attach(SplObserver <span $observer</span><span ){
        </span><span $this</span>->_observers->attach(<span $observer</span><span );
    }

    </span><span //</span><span 删除观者对象</span>
    <span public</span> <span function</span> detach(SplObserver <span $observer</span><span ){
        </span><span $this</span>->_observers->detach(<span $observer</span><span );
    }

    </span><span //</span><span 通知消息</span>
    <span public</span> <span function</span><span  notify(){
        </span><span foreach</span>(<span $this</span>->_observers <span as</span> <span $observer</span><span ){
            </span><span $observer</span>->showMessage(<span $this</span><span );
        }
    }

    </span><span //</span><span 普通方法: 设置值</span>
    <span public</span> <span function</span> setName(<span $name</span><span ){
        </span><span $this</span>->_name = <span $name</span><span ;
        </span><span $this</span>-><span notify();
    }

    </span><span //</span><span 普通方法: 获取值</span>
    <span public</span> <span function</span><span  getName(){
        </span><span return</span> <span $this</span>-><span _name;
    }

    </span><span //</span><span 普通方法:设置年龄</span>
    <span public</span> <span function</span> setAge(<span $age</span><span ){
        </span><span $this</span>->age = <span $age</span><span ;
        </span><span foreach</span>(<span $this</span>->_observers <span as</span> <span $observer</span><span ){
            </span><span $observer</span>->showAge(<span $this</span>->_name,<span $this</span>-><span age);
        }
    }

}

</span><span //</span><span  观察者抽象类</span>
<span class</span> Observer <span implements</span><span  SplObserver{

    </span><span //</span><span 显示消息提示</span>
    <span public</span> <span function</span> showMessage(SplSubject <span $obj</span><span ){
        </span><span $user</span> = <span $obj</span>-><span getName();
        </span><span if</span>(<span $user</span>==='admin'<span ){
            </span><span echo</span> '您好, ',<span $user</span>,'欢迎您进入管理后台<br/>'<span ;
        }</span><span else</span><span {
            </span><span echo</span> "你好, '<span $user</span>' 你已经被添加到了用户列表<br/>"<span ;
        }
    }
    </span><span //</span><span 这是继承父类的抽象方法</span>
    <span public</span> <span function</span> update(SplSubject <span $subject</span><span ) {}

    </span><span //</span><span 显示个人年龄</span>
    <span public</span> <span function</span> showAge(<span $name</span>,<span $age</span><span ){
        </span><span echo</span> "<script>alert('<span $name</span> 的年龄是: <span $age</span>')</script>"<span ;
    }
}

</span><span $subject</span> = <span new</span> Observed();  <span //</span><span 生成一个被观察者对象</span>
<span $observer</span> = <span new</span> Observer(); <span //</span><span 生成一个观察者对象</span>
<span $subject</span>->attach(<span $observer</span>);<span //</span><span 把观察者传入到被观察中去</span>
<span $subject</span>->setName('张三'); <span //</span><span 调用 setName 方法</span><span 
/*</span><span 
 * 通过面的 setName 就会调用  $this->notify();
* 通过调用  $this->notify()就会调用 $observer->showMessage($this)方法,
* 即每个观察者对象的 showMessage($obj)方法;
</span><span */</span>
<span $subject</span>->setName('admin'<span );
</span><span $subject</span>->setAge(24);
Salin selepas log masuk

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440273.htmlTechArticleObserved -_observers = attach(SplObserver -_observers-attach( detach(SplObserver -_observers-detach( ( -_observers -showMessage( setName( -_name = - - setAge( -age = ( -_observers...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan