What is the opposite of the observer pattern?

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)


如果观察者监视器的主体是相似的,那么你可以让观察者监视他们全部,如果不是的话,我认为你最好分开监视器,然后你将遵循单一责任规则。

链接地址: http://www.djcxy.com/p/40616.html

上一篇: 类名后面的分号是干什么的?

下一篇: 观察者模式的相反之处是什么?