全局观察者对象与 mixin 的优缺点
创建复杂的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的示例中,两者似乎都是有效的选择,但是在处理动态事件名称(也是动态“发布者”名称)时可以看到差异。
因此,当您需要使用通配符订阅事件时,使用全局发射器是很好的选择。示例:
另一个区别是您可以使用一个变量而不是 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:
Another difference is that you can have one variable instead of 2,3 ... N, which is better for memory I believe.