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

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

问题描述

在我看来,GOF 中描述的观察者设计模式与各种工具包中的侦听器实际上是同一回事.概念之间是否存在差异,或者监听器和观察器真的是一回事.

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.

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

(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.)

推荐答案

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

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天全站免登陆