Nodejs: How to handle event listening between objects?

后端 未结 2 1306
旧巷少年郎
旧巷少年郎 2021-02-06 08:40

I currently have the problem that I have an object which should listen on another object.

The question is: how should I handle the subscription? Currently, I only know o

2条回答
  •  走了就别回头了
    2021-02-06 08:49

    It really depends on how the objects will broadcast and listen and how coupled or decoupled you require them to be. Generally speaking, the best use of this type of event architecture is where an object can indiscriminately announce events to other objects and those objects can choose to listen (register) or stop listening (unregister) at their own discretion.

    1. The listener is dependent on the broadcaster

    It might make sense in some scenarios to make the listener a dependent of the broadcaster, in which case it will be strongly coupled to the event name and the arguments of the event being announced by that broadcaster.

    var Listener = function(broadcaster) {
      var self = this;
      broadcaster.on("event", function(info) {
        self.doSomething(info);  
      });
    };
    

    Other listener classes can also be created and register themselves with the same broadcaster. The broadcaster has no knowledge of the listeners only the listener is aware of the relationship.

    2. The broadcaster is dependent on the listener

    The broadcaster announces events only for the listener. In this scenario the event architecture may be overkill, you could replace the events with direct calls. Your second example hints at this because of the one to one relationship with the listener in the broadcaster's constructor.

    var Broadcaster = function(listener) {
      this.doSomething = function() {
        // do something...
        listener.doSomething(info);
      };
    };
    

    This creates strong coupling between the broadcaster and the interface for the listener. This pattern is not as limiting as it may first appear though. A common approach to extending the usefulness of this pattern is to replace the basic listener with a broadcasting adapter. The broadcasting adapter appears the same as the basic listener in that it carries the same doSomething interface but implements an event architecture to pass this call on to other objects. This keeps the broadcaster very simple and externalizes anything to do with events at the cost of an additional class.

    3. Both are independent

    Your first example is a good example of releasing this coupling the cost is an additional class. The intermediate class acts as a bridge or adapter between the two classes so that neither is dependent on each other's information. The event signature is hidden from the listener and the method signature for the listener is hidden from the broadcaster. Often this level of decoupling is unnecessary but is an important tool to be aware of where it is important to keep two classes isolated from each other.

    var Bridge = function(listener, broadcaster) {
      broadcaster.on("event", function(info) {
        // if necessary translate the arguments as well
        listener.doSomething(info);
      });
    };
    

提交回复
热议问题