23

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?

blueshift
  • 831
  • 2
  • 12
  • 24
  • This is something like only one Observer with multiple Observable. So all Observable will update a single Observer when their state changes, and still Observer can differentiate different Observable when its update(Observable o, Object arg) is called. – Kanagavelu Sugumar May 29 '16 at 14:32
  • I feel the only change here is Observer should dynamically register to new Observable when it is available during run time.Means When new Observable is brought up; then this has to be communicated to Observer to register it up. The other way is when new Observer is brought up; this Observer object is always built with Observable in their constructor. So something like Observable constructor should accept that single Observer when it is brought up. – Kanagavelu Sugumar May 29 '16 at 14:41

5 Answers5

15

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.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • 2
    My thoughts exactly. This still sounds like an observer pattern to me. – Matt Ball Feb 10 '11 at 03:02
  • 1
    Unless he only wants the event handler to run once, no matter how many publishers fire the event. – Charles Bretana Feb 10 '11 at 03:03
  • 1
    To add to this: http://sourcemaking.com/design_patterns/observer (using a single Observer is "left to the discretion of the designer"), and http://www.eecho.info/Echo/design-pattern/observer-java-pattern/ (variants are "Single observer versus multiple observers") – anon Feb 10 '11 at 03:06
  • @Charles: that's a little vague (and perhaps contrived), but running with it as best I can: how would the "event" be distinguished from the next event? It's only possible if the publishers are themselves given some token saying "this is a triggered by underlying event 123", then they could pass that to the top-level observer that could put 123 in a set of already-handled events, ignoring further reports.... – Tony Delroy Feb 10 '11 at 03:09
  • Thanks to all of you for clarifying this. @anon: thanks for the links. – blueshift Feb 10 '11 at 03:14
  • @Tony, if this is what the op wants then the event handler would have to disable future events in some way.. . see my answer below, but apparently this is not what he was after ... – Charles Bretana Feb 10 '11 at 03:16
  • So the opposite of the observer pattern... is the observer pattern. – NotAPro May 06 '22 at 16:32
  • @NotAPro: exactly, the question said "As I understand it, the observer pattern allows for multiple observers to monitor a single subject." - that's not the case - it can be implemented as a many-to-many pattern. – Tony Delroy May 07 '22 at 01:04
3

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)

WuHoUnited
  • 8,279
  • 3
  • 24
  • 27
1

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.

Community
  • 1
  • 1
Petr Cibulka
  • 2,452
  • 4
  • 28
  • 45
1

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

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • I may want to use your advice here since I want the subject to be disabled after it raises its Notify event. – blueshift Feb 10 '11 at 03:15
1

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.

hewei1997
  • 11
  • 1