Do you know what the observer model is? What is the subscription publishing model? What’s the difference? I think carefully, is really asked, the brain began to round up.

Borrow this question, reviewed once again.

Subscription publishing model

Personally, the subscription and publication mode is actually like subscribing to a newspaper in the newsstand, or subscribing to the official account on wechat. I subscribe to the event through the newsstand, and then once there is this event

And when it does, the newsstand will publish it, and I will receive the event. Newsstands are middlemen who subscribe to publications. This is the biggest difference from the observer model. There’s no point in talking, so let’s show it in code.

var pubsub = (() = > {
    var events = {};
    function subscripe(eventName, fn) {
        if(! events[eventName]) { events[eventName] = [] } events[eventName] = fn; }function publish(eventName, ... args) {
        if(! events[eventName]) {// Indicates that the event is not subscribed
            return;
        }
        for (let fn of  events[eventName]) {
            fn(...args);
        }
    }
    return {
        subscripe,
        publish
    }
})()

/ / use
pubsub.subscribe('aa'.(name, age) = > {
   console.log(`I am ${name}, my age is ${age}`)
})
pubsub.publish('aa'.'zj'.20)
// => I am zj, my age is 20;
// As soon as the event is published, the subscription event is received and the print method is executed.
Copy the code

Observer model

Like I have a camera in my house, and my family has access to it. So the family is the observer, the situation in the home is the observed. This is a direct one-to-many relationship. Directly observed.

It’s like auctioning something, and a lot of people are looking at it, so it’s being observed, and everyone in this room is an observer. Another example is the live broadcast room. The anchor is the observed,

The viewer is the observer. So when the anchor says something, the observer keeps observing, and the anchor mentions whose name, all the observers can see.

// For example, subject is an anchor
class Subject {
    constructor() {
        this.name = ' ';
        this.people = [];
    }
    attatch(person) {
        this.people.push(person);
    }
    getName() {
        return this.name;
    }
    setName(name) {
        this.name = name;
        this.notifyAll();
    }
    notifyAll() {
        this.people.forEach((person) = >{ person.update(); }}})class Observer {
    constructor(name, subject) {
        this.name = name;
        this.subject = subject;
        this.subject.attatch(this);
    }
    update() {
        console.log(`The ${this.name} heard The ${this.subject.getName()}`)}}var s = new Subject()
var a1 = new Observer('a1', s)
var a2 = new Observer('a2', s)
s.setName('zj')
Copy the code

The difference between

  1. Subscription publishing can be done through a middleman, the observer mode where the observer and the observed communicate directly.

  2. Subscribing to a publication is to use a middleman to subscribe to an event that, if it exists, will be published and executed by the subscriber. The observer model is different, as long as the observer, then the observed has

All observers will be updated.

Copy the code