观察者模式的反面是什么?[英] What is the opposite of the observer pattern?

本文是小编为大家收集整理的关于观察者模式的反面是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我理解,观察者模式允许多个观察者监视单个主题.相反的情况有模式吗?单个观察者是否有一种模式来监视几个主题并在其中任何一个人提出时做出响应,例如,通知事件?

推荐答案

仍然可以使用观察者模式:只有与许多受监视对象的观察者具有相同的对象寄存器.您可能希望"通知"事件可以接收某种观察到的对象标识符(" This"指针,唯一ID号等),以便观察者对象可以选择适合事件的对象的操作.<<<<<<<<<<<<<

其他推荐答案

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

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

如果您想要单独的操作,具体取决于事件的主题触发了该事件,则可以使用传递到观察者的操作效果的方法的操作参数来帮助确定触发事件的主题. (这些名称可能会根据您选择的语言或库而更改)

其他推荐答案

如果您只希望观察者对一次进行反应,那么无论受监视的对象增加了事件,您都必须将一部分事件处理程序"解开",一旦第一个来源,所有其他来源都将"解放"观察者启动事件,否则您将必须决定使用频率或定时条件来决定何时来自另一个事件(或在某些定义间隔之后再次或同一源),应导致观察者再次做出反应...

本文地址:https://www.itbaoku.cn/post/627690.html

问题描述

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?

推荐答案

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.

其他推荐答案

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)

其他推荐答案

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