Observer、Pub/Sub 和数据绑定之间的区别 [英] Difference between Observer, Pub/Sub, and Data Binding

本文介绍了Observer、Pub/Sub 和数据绑定之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

观察者模式发布/订阅数据绑定?

我在 Stack Overflow 上搜索了一下,没有找到任何好的答案.

I searched around a bit on Stack Overflow and did not find any good answers.

我逐渐相信数据绑定是一个通用术语,并且有不同的实现方式,例如观察者模式或发布/订阅模式.使用观察者模式,观察者更新它的观察者.通过 Pub/Sub,0-多个发布者可以发布特定类别的消息,0-多个订阅者可以订阅特定类别的消息.

What I have come to believe is that data binding is a generic term and there are different ways of implementing it such as the Observer Pattern or the Pub/Sub pattern. With the Observer pattern, an Observable updates its Observers. With Pub/Sub, 0-many publishers can publish messages of certain classes and 0-many subscribers can subscribe to messages of certain classes.

是否有其他实现数据绑定"的模式?

推荐答案

以下是我对这三个方面的看法:

Here's my take on the three:

本质上,从本质上讲,这只是意味着对象 Y 上的属性 X 的值在语义上绑定到对象 B 上的属性 A 的值.没有假设 Y 如何知道或馈送对象 B 上的更改.

Essentially, at the core this just means "the value of property X on object Y is semantically bound to the value of property A on object B. No assumptions are made as to how Y knows or is fed changes on object B.

一种设计模式,通过该模式,对象能够将特定事件通知其他人 - 通常使用实际事件完成,这些事件有点像对象中具有特定函数/方法形状的插槽.可观察对象提供通知,观察者接收这些通知.在 .net 中,observable 可以公开一个事件,而观察者使用事件处理程序"形状的钩子订阅该事件.没有对通知发生的具体机制做出任何假设,也没有对一个可观察对象可以通知的观察者数量做出假设.

A design pattern by which an object is imbued with the ability to notify others of specific events - typically done using actual events, which are kind of like slots in the object with the shape of a specific function/method. The observable is the one who provides notifications, and the observer receives those notifications. In .net, the observable can expose an event and the observer subscribes to that event with an "event handler" shaped hook. No assumptions are made about the specific mechanism which notifications occur, nor about the number of observers one observable can notify.

Observable/Observer 模式的另一个名称(可能有更多的广播"语义),这通常意味着更动态"的风格——观察者可以订阅或取消订阅通知,一个可观察者可以向多个观察者大喊大叫".在 .NET 中,可以为此使用标准事件,因为事件是 MulticastDelegate 的一种形式,因此可以支持将事件传递给多个订阅者,也支持取消订阅.Pub/Sub 在某些上下文中的含义略有不同,通常涉及 event 和 eventer 之间更多的匿名性",可以通过任意数量的抽象来促进,通常涉及一些无所不知的中间人"(例如消息队列)方,但各个方并不了解对方.

Another name (perhaps with more "broadcast" semantics) of the Observable/Observer pattern, which usually implies a more "dynamic" flavor - observers can subscribe or unsubscribe to notifications and one observable can "shout out" to multiple observers. In .NET, one can use the standard events for this, since events are a form of MulticastDelegate, and so can support delivery of events to multiple subscribers, and also support unsubscription. Pub/Sub has a slightly different meaning in certain contexts, usually involving more "anonymity" between event and eventer, which can be facilitated by any number of abstractions, usually involving some "middle man" (such as a message queue) who knows all parties, but the individual parties don't know about each other.

在许多类似 MVC"的模式中,observable 公开了某种方式的属性更改通知",其中还包含有关特定属性更改的信息.观察者是隐式的,通常由框架创建,并通过一些绑定语法订阅这些通知以专门标识一个对象和属性,而事件处理程序"只是将新值复制过来,可能会触发任何更新或刷新逻辑.

In many "MVC-like" patterns, the observable exposes some manner of "property changed notification" that also contains information about the specific property changed. The observer is implicit, usually created by the framework, and subscribes to these notifications via some binding syntax to specifically identify an object and property, and the "event handler" just copies the new value over, potentially triggering any update or refresh logic.

数据绑定的替代实现?好的,这是一个愚蠢的:

An alternative implementation for data binding? Ok, here's a stupid one:

  • 启动后台线程,不断检查对象的绑定属性.
  • 如果该线程检测到自上次检查以来该属性的值已更改,则将该值复制到绑定项目.

这篇关于Observer、Pub/Sub 和数据绑定之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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