php设计模式学习之观察者模式

什么都不说,先看代码:

interface userOperateImpl
{
    public function operate($username);
}

class userLoginLog implements userOperateImpl
{
    public function operate($username)
    {
        echo '用户名为 ['.$username.'] 的用户登陆记录日志<br/>';
    }
}

class UserLoginPush implements userOperateImpl
{
    public function operate($username)
    {
        echo '用户名为 ['.$username.'] 的用户登陆推送消息<br/>';
    }
}

class UserLogin
{
    private $_obj = array();
    public $username;

    public function __construct($username,$_objArr = array())
    {
        $this->username = $username;
        $this->_obj = $_objArr;
    }

    public function userLogin()
    {
        echo "用户名为 [".$this->username."] 的用户登陆!<br/>";
        $this->trigger();
    }

    private function trigger()
    {
        if(!empty($this->_obj)){
            foreach($this->_obj as $obj){
                /** @var userOperateImpl $obj */
                $obj->operate($this->username);
            }
        }
    }
}

$user = new UserLogin('Mr Li', array(
        new userLoginLog(),
        new UserLoginPush()
    )
);
$user->userLogin();

解释:

编写接口userOperateImpl表示用户操作,将登陆记录的日志,推送实现该接口,代表两个观察者.

编写主题类UserLogin,构造函数里出入用户名和观察者,在用户登陆方法中将消息传递给观察者!

注:

在观察者模式中,会改变的是主题的状态以及观察者的数目。用这个模式,你可以改变依赖于主题状态的对象,却不必改变主题。——找出程序中会变化的方面,然后将其和固定不变的方面相分离!
 
 主题和观察者都使用接口:观察者利用主题的接口向主题注册,而主题利用观察者接口通知观察者。这样可以让两者之间运作正常,又同时具有松耦合的优点! ——针对接口编程,不针对实现编程!
 观察者模式利用“组合”将许多观察者组合进主题中。对象(观察者——主题)之间的这种关系不是通过继承产生的,而是在运行时利用组合的方式产生的。 ——多用组合,少用继承!
原文地址:https://www.cnblogs.com/sign-ptk/p/5689488.html