全局观察者对象与 mixin 的优缺点

发布于 2024-12-22 15:41:05 字数 1285 浏览 0 评论 0原文

创建复杂的 JS 应用程序时,使用触发事件并让所有其他对象订阅的全局观察者对象与在负责触发自己事件的所有对象上混合或原型化 pub/sub 方法有何优缺点?

以具有庄家、玩家和桌子对象的纸牌游戏为例(伪代码如下)

// "Global" observer version

var observer = {
    // publish and subscribe methods defined here
};

dealer.deal = function(cards) {
    // performs logic for dealing cards
    observer.publish('dealer:dealt', cards, this);
};

player.play = function(cards) {
    // performs logic for which card is played
    observer.publish('player:played', cards, this);
};

table.showCards = function(cards, player) {
    // performs logic for showing cards that the dealer dealt
    // or that the player played
};

observer.subscribe('dealer:dealt', table.showCards);
observer.subscribe('player:played', table.showCards);

// Pub/sub mixin/prototype version

dealer.deal = function(cards) {
    // performs logic for dealing cards
    this.publish('dealt', cards);
};

player.play = function(cards) {
    // performs logic for which card is played
    this.publish('played', cards);
};

table.showCards = function(cards) {
    // performs logic for showing cards that the dealer dealt
    // or that the player played
};

dealer.subscribe('dealt', table.showCards);
player.subscribe('played', table.showCards);

When creating a complex JS application, what are the pros and cons of using a global observer object which fires events and which all other objects subscribe to vs. mixing in or prototyping pub/sub methods on all objects which are responsible for triggering their own events?

Take for example a card game which has dealer, player, and table objects (psuedocode-ish follows):

// "Global" observer version

var observer = {
    // publish and subscribe methods defined here
};

dealer.deal = function(cards) {
    // performs logic for dealing cards
    observer.publish('dealer:dealt', cards, this);
};

player.play = function(cards) {
    // performs logic for which card is played
    observer.publish('player:played', cards, this);
};

table.showCards = function(cards, player) {
    // performs logic for showing cards that the dealer dealt
    // or that the player played
};

observer.subscribe('dealer:dealt', table.showCards);
observer.subscribe('player:played', table.showCards);

vs

// Pub/sub mixin/prototype version

dealer.deal = function(cards) {
    // performs logic for dealing cards
    this.publish('dealt', cards);
};

player.play = function(cards) {
    // performs logic for which card is played
    this.publish('played', cards);
};

table.showCards = function(cards) {
    // performs logic for showing cards that the dealer dealt
    // or that the player played
};

dealer.subscribe('dealt', table.showCards);
player.subscribe('played', table.showCards);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2024-12-29 15:41:05

在您的示例中,两者似乎都是有效的选择,但是在处理动态事件名称(也是动态“发布者”名称)时可以看到差异。

因此,当您需要使用通配符订阅事件时,使用全局发射器是很好的选择。示例:

eventEmitter.subscribe('*:delt', handler);

另一个区别是您可以使用一个变量而不是 2,3 ... N,我相信这对记忆更好。

In your examples both seem a valid choice, but the difference can be seen when dealing with dynamic event names (also dynamic 'publisher' names).

So using a global emitter is good when you need to subscribe to events using a wildcard. Example:

eventEmitter.subscribe('*:delt', handler);

Another difference is that you can have one variable instead of 2,3 ... N, which is better for memory I believe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文