观察者模式的反面是什么?

发布于 2024-10-16 20:53:24 字数 95 浏览 1 评论 0原文

据我了解,观察者模式允许多个观察者监视单个主题。相反的情况是否有模式?是否有一种模式可以让单个观察者监视多个主题并在其中任何一个主题引发(例如,Notify 事件)时做出响应?

As I understand it, the observer pattern allows for multiple observers to monitor a single subject. Is there a pattern for the opposite scenario? Is there a pattern for a single observer that monitors several subjects and responds when any one of them raises, say, a Notify event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥抱我好吗 2024-10-23 20:53:25

是的。它只是观察者模式的另一个应用。

观察者将自己添加到许多主题中。
如果您希望无论您正在观察哪个主题都执行相同的操作,那么这与您正在使用的观察者模式完全相同。

如果您想要根据哪个主题触发事件而执行单独的操作,那么您可以使用传递到观察者的 ActionPerformed 方法中的 Action 参数来帮助确定哪个主题触发了事件。 (这些名称可能会根据您选择的语言或库而变化)

Yes. It is just another application of the observer pattern.

The Observer adds itself to many Subjects.
If you want the same action to be performed no matter Which subject you're observing then this is exactly the same as the Observer pattern you are using.

If you want a separate action depending on which Subject triggered the event then you can use the Action parameter that is passed into the Observer's ActionPerformed method to help determine which subject triggered the event. (these names may change depending on your language or library of choice)

还要考虑相关的中介模式。

中介者模式定义了一个对象,该对象封装了一组对象如何交互(Wikipedia)< /p>

更多信息请参见: http://sourcemaking.com/design_patterns/mediator

我也非常喜欢@CDC关于中介者与观察者面向对象的设计模式

观察者模式:定义对象之间的一对多依赖关系,以便当一个对象更改状态时,其所有依赖对象都会收到通知并自动更新。

中介者模式:定义一个对象来封装一组对象如何交互。中介器通过防止对象显式地相互引用来促进松散耦合,并且它允许您独立地改变它们的交互。

Also consider related Mediator pattern.

Mediator pattern defines an object that encapsulates how a set of objects interact (Wikipedia)

More info here: http://sourcemaking.com/design_patterns/mediator

I also very like @CDC's answer on Mediator Vs Observer Object-Oriented Design Patterns:

The Observer pattern: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

The Mediator pattern: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

指尖上得阳光 2024-10-23 20:53:25

如果您只希望观察者做出反应一次,无论有多少受监视对象引发事件,那么一旦第一个源触发事件,您就必须让事件处理程序的一部分从所有其他源“取消注册”观察者,或者您必须决定应该使用多长时间或什么时间标准来决定来自另一个事件(或在某个定义的时间间隔后再次来自同一源)的事件何时应导致观察者再次做出反应......

if you only want the observer to react once, no matter how many monitored objects raise the event, then you will have to have part of the event handler "unregister" the observer from all other sources once the first source fires the event, or you will have to decide how often or what timing criteria should be used to decide when an event from another (or the same source again after some defined interval) should cause the observer to react again...

一身仙ぐ女味 2024-10-23 20:53:25

如果观察者监视的对象相似,那么你可以让观察者监视所有对象,如果不是,我认为你最好将监视者分开,然后你将遵循单一职责规则。

If the subjects the observer monitor are similar, then you can make the observer monitor them all, if not, i think you'd better seperate the montior, then you'll follow the single responsibility rule.

最舍不得你 2024-10-23 20:53:24

观察者模式仍然可以使用:只需将相同的对象注册为许多受监视对象的观察者即可。您可能希望“通知”事件接收某种被观察对象标识符(“this”指针、唯一的 ID 号等),以便观察者对象可以选择适合报告事件的对象的操作。

The Observer pattern can still be used: just have the same object register as an observer to many monitored objects. You'll probably want the "Notify" event to receive some kind of observed-object identifier (the "this" pointer, a unique id number etc) so that the observer object can choose an action appropriate to the object reporting the event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文