观察者(Observable)模式和发布订阅(Publisher

2020-09-05  本文已影响0人  匿于烟火中
思考一种场景

如果b,c,d每次都去询问a是否发生变化,然后a来判断下状态是否变化,也能实现这个场景,但是

观察者模式(observable pattern)


Subject Interface应该有几个方法
观察者observer

发布者Subject 的实现

export interface Subject {
    add(obs: Observable);
    remove(obs: Observable);
    notify(data);
}
export class DataSubject implements Subject {
    private observers = [];
    add(obs: Observable) {
        this.observers.push(obs);
    }

    remove(obs: Observable) {
        const i = this.observers.indexOf(obs);
        if (i > -1) {
            this.observers.splice(i, 1);
        }
    }

    notify(data) {
        this.observers.forEach(cur => {
            cur.update(data);
        })
    }
}

观察者Observable的实现

export interface Observable {
    update(data);
}

export class ObservableEntity implements Observable {
    public data:any;
    private subject:DataSubject = new DataSubject();

    initSubject(s:DataSubject){
        this.subject = s;
        this.subject.add(this);
    }

    update(data:any){
        this.data = data;
    }
}

观察者模式的特点

观察者模式提供了发布者和订阅者之间的松耦合模式。
因为观察者不用关心每一个订阅者的具体实现的细节是什么,只要订阅者订阅了接口,就给它发送更新的消息。
也就是说:
观察者模式中,所有消息的变化都是由发布者主动去通知所有的订阅者。
订阅者对于发布者发布的变化以及变化的内容,只能被动的接收。
但是,可能订阅者并不希望每一次都接收到消息变化的订阅。
观察者会接收到发布者发布的所有数据,但是有的观察者可能只需要其中的一点数据而已。

可观察对象Observable

export class Observable{
    private observers = [];
    private changed:boolean = false;
    add(obs: Observable) {
        this.observers.push(obs);
    }

    remove(obs: Observable) {
        const i = this.observers.indexOf(obs);
        if (i > -1) {
            this.observers.splice(i, 1);
        }
    }

    notify(data) {
        if(this.changed){
            this.observers.forEach(cur => {
                cur.update(data);
            })
        }
        this.changed = false;

    }

    setChanged(){
        this.changed = true;
    }
}

setChanged的方法可以用来标识发布者的状态是否变化
如果状态变化了,再通知观察者。
设置setChanged方法的优点在于,可观察对象可以更加灵活的处理在什么时候去通知发布者。
比如:温度变化时候每时每刻都在变化,通过setChanged状态的标记,可观察对象可以灵活决定,比如每变化超过1度才去通知。

需墙-Observer Pattern – Design Patterns (ep 2)
《head first设计模式》

发布订阅模式(publisher-subscribe pattern)


在发布订阅模式当中,发布者并不会主动去通知订阅者,他们之间的信息交互是通过第三者消息队列(Broker)去交流。

发布订阅模式
那么消息队列是如何进行消息的分发呢?

通常有两种模式来进行消息过滤:topic-based & content-based.

In a topic-based system, messages are published to "topics" or named logical channels. Subscribers in a topic-based system will receive all messages published to the topics to which they subscribe. The publisher is responsible for defining the topics to which subscribers can subscribe.
In a content-based system, messages are only delivered to a subscriber if the attributes or content of those messages matches constraints defined by the subscriber. The subscriber is responsible for classifying the messages.
Publish–subscribe pattern
基于主题过滤:当订阅者订阅了某个主题,那么只要任何发布者发布了这个主题相关的消息,消息队列就会通知这个订阅者
基于内容过滤:只有当发布的消息的内容或者属性复合订阅者订阅的具体条件,那么消息队列才会把对应的消息分发给订阅者

优点
发布订阅模式和观察者模式的异同
observable vs publisher
Observer vs Pub-Sub Pattern
In the observer pattern, the observers are aware of the Subject. The Subject maintains a record of the Observers. Whereas, in publisher-subscriber, publishers and subscribers don’t need to know each other. They simply communicate with the help of message queues or a broker.
In the publisher-subscriber pattern, components are loosely coupled as opposed to the observer pattern.
The observer pattern is mostly implemented synchronously, i.e. the Subject calls the appropriate method of all its observers when an event occurs. The publisher-subscriber pattern is mostly implemented asynchronously (using a message queue).
The observer pattern needs to be implemented in a single-application address space. On the other hand, the publisher-subscriber pattern is more of a cross-application pattern.
上一篇 下一篇

猜你喜欢

热点阅读