观察者面向对象的设计模式仅仅是实现回调的一种形式吗? [英] Is the observer object oriented design pattern just a form of implementing a callback?

查看:62
本文介绍了观察者面向对象的设计模式仅仅是实现回调的一种形式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近有人问我,观察者OO设计模式中的回调和观察者之间有什么区别.我的理解是,回调是作为参数传递给函数的任何可执行代码,当特定事件发生时,该函数将被调用.它可以用不同的编程语言以不同的形式实现,例如面向对象范式中的函数指针,匿名函数和观察者/侦听器.

开发人员通常与代码执行同步实现回调注册,并且仅支持一个回调,而观察者/监听者的回调注册始终与代码执行异步,并且支持多种注册.

但是,我认为回调中同步和基数的那些通常限制并不是其定义所固有的.是吗?

参考: https://en.wikipedia.org/wiki/Callback_(computer_programming)

解决方案

我不知道回调有任何正式定义.至少,它没有显示在 SE Vocab 中.它确实显示在 SO 上,但是您会看到答案的一些变化./p>

观察者 确实具有官方定义(GoF),它与发布/订阅.当然,有一些开发人员定义了足够广泛的回调以涵盖pub/sub.其他人在两者之间绘制区别.

您必须选择一个回调定义.然后,您将对这个问题有一个答案.

顺便说一下,对于Wikipedia而言,存在着非常不准确的信息,并且误导有关众多设计的信息那里的模式;因此,我绝不会将其视为任何技术性术语的正式定义.

I was recently asked what is the difference between a callback and an observer in the Observer OO design pattern. My understanding is that callback is any executable code passed as an argument to a function which is called when a specific event happens. It may be implemented in different forms in different programming languages, such as function pointers, anonymous functions and observers/listeners in the object-oriented paradigm.

Developers usually implement callback registration synchronously with code execution and support only one callback, while observers/listeners callback registration is always asynchronous with code execution and multiple registration is supported.

However, I believe that those usual limitations of sync and cardinality in a callback are not inherent in its definition. Is that right?

Reference: https://en.wikipedia.org/wiki/Callback_(computer_programming)

解决方案

I don't know that there is any official definition of callback. At least, it doesn't show up in SE Vocab. It does show up on SO, but you can see some variation in the answers.

Observer does have an official definition (GoF) and it is synonymous with publish/subscribe. Surely there are developers who define callback broadly enough to encompass pub/sub. Others draw a distinction between the two.

You have to choose a callback definition. Then you will have an answer to this question.

By the way, regarding Wikipedia, there is extremely inaccurate and misleading information on numerous design patterns there; so I would in no way consider it an official definition for anything technical.

这篇关于观察者面向对象的设计模式仅仅是实现回调的一种形式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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