Nodejs:如何处理对象之间的事件监听? [英] Nodejs: How to handle event listening between objects?

查看:126
本文介绍了Nodejs:如何处理对象之间的事件监听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到的问题是我有一个应该侦听另一个对象的对象。

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 of two possible ways:

使用registrator:

With registrator:

var Registrator = function() {
    this.listener = new Listener();
};

Registrator.prototype.register = function(subsriber) {
    var self = this;
    subscriber.on('event', function(info) {
        self.listener.doSomething(info);
    });

};

在构造函数中:

var Subscriber = function() {
    var listener = require('./instanced-listener');
    this.on('event', function(info) {
        listener.doSomething(info);
    });

};

正如您所看到的,两种方法都不是那么整洁。我可以使用另一种模式解决这个问题吗?

As you can see, both methods are not that neat. Is there another pattern which I could use for this problem?

问候

PS:

如果示例看起来过于抽象,则源代码如下:

If the example looks too abstract, here is the source:

https://github.com/bodokaiser/documents/blob/master/lib/repository.js

检查构造函数以查看我的不快乐:)

Check the constructor to see my unhappiness :)

推荐答案

这实际上取决于对象将如何广播和收听,以及您需要它们如何耦合或分离。一般来说,这种类型的事件体系结构的最佳用途是对象可以不加区别地向其他对象宣告事件,并且这些对象可以自行选择监听(注册)或停止监听(取消注册)。

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.

在某些情况下,使监听器成为广播公司的依赖者可能是有意义的,在这种情况下,它将与事件强烈耦合该广播公司宣布的活动名称和参数。

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.

广播公司仅为听众宣布活动。在这种情况下,事件体系结构可能过度,您可以使用直接调用替换事件。你的第二个例子暗示了这一点,因为广播公司的构造函数中与侦听器的一对一关系。

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);
  };
};

这会在广播公司和听众界面之间产生强大的耦合。这种模式并不像它首先出现的那样具有限制性。扩展此模式的有用性的常用方法是用广播适配器替换基本侦听器。广播适配器看起来与基本侦听器相同,因为它带有相同的 doSomething 接口,但实现了一个事件体系结构以将此调用传递给其他对象。这使得广播公司非常简单,并且可以以额外的课程为代价将与事件有关的任何事情外化。

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.

你的第一个例子是发布这种耦合的一个很好的例子,成本是一个额外的类。中间类充当两个类之间的桥接器或适配器,因此两者都不依赖于彼此的信息。事件签名对于侦听器是隐藏的,并且侦听器的方法签名对广播器是隐藏的。通常这种脱钩程度是不必要的,但却是一个重要的工具,要注意保持两个类彼此隔离的重要性。

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);
  });
};

这篇关于Nodejs:如何处理对象之间的事件监听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆