“This is the 17th day of my participation in the August More Text Challenge.

preface

At the advanced end of the interview, there are often some questions about design patterns, and the answers are not good. Coincides with the activities of more challenges in August, I plan to spend a month to clarify the knowledge points about design patterns, to increase the confidence of my interview.

In the previous article implements a simple publish-subscribe pattern, but there are two serious shortcomings, every publishers will have to add subscribers, release information, method of removing subscribers, also need a cache the list of subscribers, such a serious waste of memory resources, the other is the subscriber to know publishers to subscribe, There is some coupling between them.

So there needs to be a mediation between the publisher and the subscriber, and this article implements this mediation so that the publisher doesn’t have to have a way to add subscribers, publish messages, remove subscribers, cache the list of subscribers, and hand it all over to the mediator. The subscriber does not need to know which publisher the message is coming from, nor does the publisher know which subscribers the message is being pushed to, and the intermediary connects the subscriber with the publisher.

Mediation-based publish-subscribe

var Event = (function () { let clientList = {}; Const listen = function (key, fn) {const listen = function (key, fn) {if (! clientList[key]) { clientList[key] = []; } clientList[key].push(fn); }; Const trigger = function () {/ / by ` trigger ` release method of the first parameter to obtain a subscriber identification const key = Array. The prototype. The shift. The call (the arguments); const fns = clientList[key]; if (! fns || fns.length === 0) { return false; } for (let i = 0, fn; fn = fns[i++];) { fn.apply(this, arguments); }}; const remove = function (key, fn) { const fns = clientList[key]; if (! fns) { return false; } if (! fn) { fns && (fns.length = 0); } else { for (let l = fns.length - 1; l >= 0; l--) { const _fn = fns[l]; if (_fn === fn) { fns.splice(l, 1); }}}}; return { listen: listen, trigger: trigger, remove: remove } })();Copy the code

Subscribers subscribe to messages via event.listen:

Function (price) {console.log(${price}, ${price}, ${price}, ${price}, ${price}, ${price} `)});Copy the code

Publishers use event.trigger to publish messages:

Event.trigger(' tuhao Gold ', 9999); // Output: the phone arrived, the color is tuhao gold, the price is 9999 yuan, come to buy it!Copy the code

Publish – subscribe in JavaScript

In JavaScript, the publish-subscribe model can be implemented simply in the form of a registration callback function. If the publish-subscribe model is implemented as a class, the subscriber object is passed into the publisher object, and the subscriber object provides a method to update the updata that the publisher can call at some point.

There is also a publish-subscribe model, as well as a push or pull model, in which the publisher pushes all the changed state and data to the subscriber at once when an event occurs. Pull model is where the publisher just notifies the subscriber that I have published, and provides some public interface for the subscriber to pull data actively. The advantage of the model is that the subscriber can get information on demand.

In JavaScript, arguments can easily represent the arguments received by a function, so in JavaScript, the push model is usually used and the apply method is used to push all arguments to the subscriber.

Advantages and disadvantages

The advantages of the publish-subscribe model are obvious: first, decoupling in time, and second, decoupling between objects.

There are drawbacks to the publish-subscribe model. Creating a subscriber itself takes time and memory, and when you subscribe to a message, it may never happen, but the subscriber is always in memory. In addition, the publish-subscribe model can weaken relationships between objects, but if overused, the necessary relationships between objects can be hidden, and bugs can be difficult to track and fix when multiple publishers and subscribers are associated with each other.