设计模式| 观察者模式

mervyn 2018年12月19日14:12:33设计模式设计模式| 观察者模式已关闭评论642

目录

什么是观察者模式

软件设计中是一个对象,维护一个依赖列表,当任何状态发生改变自动通知它们。当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。观察者模式属于行为型模式。文章源自编程技术分享-https://mervyn.life/5c14ee92.html

跟发布订阅模式很像,具体区别可以参考:观察者模式 vs 发布-订阅模式文章源自编程技术分享-https://mervyn.life/5c14ee92.html

在观察者模式中有如下角色:文章源自编程技术分享-https://mervyn.life/5c14ee92.html

  • Subject:抽象主题(抽象被观察者),抽象主题角色把所有观察者对象保存在一个集合里,每个主题都可以有任意数量的观察者,抽象主题提供一个接口,可以增加和删除观察者对象。文章源自编程技术分享-https://mervyn.life/5c14ee92.html

  • ConcreteSubject:具体主题(具体被观察者),该角色将有关状态存入具体观察者对象,在具体主题的内部状态发生改变时,给所有注册过的观察者发送通知。文章源自编程技术分享-https://mervyn.life/5c14ee92.html

  • Observer:抽象观察者,是观察者者的抽象类,它定义了一个更新接口,使得在得到主题更改通知时更新自己。文章源自编程技术分享-https://mervyn.life/5c14ee92.html

  • ConcrereObserver:具体观察者,实现抽象观察者定义的更新接口,以便在得到主题更改通知时更新自身的状态。文章源自编程技术分享-https://mervyn.life/5c14ee92.html

代码实现

由于 PHP 通过内置的 SPL 扩展提供了对观察者模式的原生支持,其中的观察者由 3 个元素组成 : SplObserver 接口、 SplSubject 接口和 SplObjectStorage 工具类。接下来只通过SPL 的方式来实现该设计模式。文章源自编程技术分享-https://mervyn.life/5c14ee92.html

已微信公众号为例,当微信公众号发布一篇文章的时候需要给关注着推送消息。这种一对多的关系就可以采用观察者模式文章源自编程技术分享-https://mervyn.life/5c14ee92.html

<?php
class WechatUser implements SplObserver {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function update(SplSubject $subject) {
        echo $this->name . "! There is an new article: " . $subject->getContent() . "\n" ;
    }
}

class WechatSubscription implements SplSubject {
    private $observers;
    private $content;

    public function __construct() {
        $this->observers = new SplObjectStorage();
    }

    public function attach(\SplObserver $observer) {
        $this->observers->attach($observer);
    }

    public function detach(\SplObserver $observer) {
        $this->observers->detach($observer);
    }

    public function notify() {
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }

    public function sentArticle($content) {
        $this->content = $content;
        $this->notify();
    }

    public function getContent() {
        return $this->content;
    }
}

$wechat = new WechatSubscription();

$user1 = new WechatUser("Mervyn");
$user2 = new WechatUser("Tangtz");
$wechat->attach($user1);
$wechat->attach($user2);

$wechat->sentArticle("article content");

/*
以上输出:
Mervyn! There is an new article: article content
Tangtz! There is an new article: article content
*/

$wechat->detach($user2);
$wechat->notify();
/*
输出:

Mervyn! There is an new article: article content
*/

$wechat->sentArticle("article content");
/*
输出:

Mervyn! There is an new article: article content
*/

参考链接:文章源自编程技术分享-https://mervyn.life/5c14ee92.html

观察者模式文章源自编程技术分享-https://mervyn.life/5c14ee92.html

https://blog.csdn.net/itachi85/article/details/50773358文章源自编程技术分享-https://mervyn.life/5c14ee92.html

weinxin
我的微信公众号
微信扫一扫
mervyn
设计模式| 观察者模式 设计模式

设计模式| 观察者模式

什么是观察者模式 软件设计中是一个对象,维护一个依赖列表,当任何状态发生改变自动通知它们。当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。观察者模式属于行为型模式。 跟...