观察者设计模式与“听众” [英] Observer Design Pattern vs "Listeners"

查看:107
本文介绍了观察者设计模式与“听众”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,GOF中描述的Observer设计模式与Listeners在各种工具包中发现的一样。概念之间有区别,还是听众和观察者真的是一回事。



(我不是在寻找任何特定的计算机语言实现,只是想从设计的角度来理解差异(如果有的话)是的,我知道关于SOF的类似问题有几个答案,但它们植根于特定语言的具体问题 - 我正在寻找设计答案,而不是语言答案。)

解决方案

术语监听者是指观察者模式是否取决于上下文。例如,Java Swing的事件侦听器是Observer模式实现的一部分,而.Net跟踪侦听器不是。



框架作者并不罕见为参与给定模式实现的组件分配不同的名称,但是在讨论模式本身时通常使用官方模式名称。



关于设计,给定模式的实现经常受到使用的语言和平台的影响。因此,给定框架(可能恰好使用术语监听器来描述ConcreteObserver的作用)的Observer模式的特定实现可能与设计模式一书中描述的略有不同。


It seems to me that the Observer design pattern as described in GOF is really the same thing as Listeners found in various toolkits. Is there a difference between the concepts, or are Listeners and Observers really the same thing.

(I'm not looking for any specific computer language implementation, I just want to understand the difference (if any) from a design point of view. Yes, I know there are several answers to similar questions on SOF, but they're rooted in specific questions about specific languages -- I'm looking for a design answer, not a language answer.)

解决方案

Whether the term "listener" refers to the Observer pattern or not will depend upon the context. For example, Java Swing's "Event Listeners" are part of an Observer pattern implementation while .Net "Trace Listeners" are not.

It isn't uncommon for framework authors to assign different names to components participating in a given pattern implementation, but the official pattern names are generally used when discussing the patterns themselves.

Concerning design, the implementation of a given pattern will often be influenced by the language and platform being used. As such, a particular implementation of the Observer pattern within a given framework (which may happen to use the term "listener" to describe the role of the ConcreteObserver) might differ slightly from that described in the Design Patterns book.

这篇关于观察者设计模式与“听众”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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