首页  >  文章  >  后端开发  >  PHP 观察者模式_PHP教程

PHP 观察者模式_PHP教程

WBOY
WBOY原创
2016-07-20 11:14:351210浏览

<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);

 

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...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn