Observer scheme with two observer lists

I have a class MyObserver that listens for changes in Notifier. Notifier extends Observable and notifies of its events with notifyObservers (Object). An object passed as an argument is always an instance of the same class. The problem is that each observer must listen to different events. For example, one observer needs to listen to changed status events and others for all types of events. How can I do this with an observer pattern?

Thanks.

+4
source share
4 answers

If you can change the design a bit:

interface MyObserver { public void stateChangeEvent(); public void otherEvent(); } class MyObserverAdapter implements MyObserver { public void stateChangeEvent() { // some default implementation or no implementation. } public void otherEvent() { // some default implementation or no implementation. } } class MyStateChangeObserver extends MyObserverAdapter { public void stateChangeEvent() { // implement behavior specific to this class. } } class MyOtherObserver extends MyObserverAdapter { public void otherEvent() { // implement behavior specific to this class. } } 

Using:

 MyObserver stateObserver = new MyStateChangeObserver(); MyObserver otherObserver = new MyOtherObserver(); notifier.notifyObservers(stateObserver); notifier.notifyObservers(otherObserver); 
+3
source

Use notifyObservers (Object arg) and create an object of type event type. In your watch classes, simply filter the events passed in the class.

 public void update(Object observable, Object arg) { if ( (MyEvent) arg.isEventX() ) { /* do stuff */ } } 
+5
source

I think the native implementation of the Observer Java pattern is not suitable for your case.

In fact, the general Observer pattern can be used when only one Observed event type can occur. In the observer design pattern, all Observes is always notified.

So, in this case, you need to expand the general Observer template by defining your own Observable interface, for example, as follows:

 public enum EventKind { EVENT_A, EVENT_B, EVENT_C; } public interface Observable { public void registerObserver(EventKind eventKind); public void unregisterObserver(EventKind eventKind); public void notifyObservers(EventKind eventKind); } 

Then you can simply implement this Observable interface with internal lists to notify each type of event. You can use the built-in Observer interface if you want.

This approach has the following advantages:

  • You can flexibly add more events without affecting the Observers code.
  • You can register any observer in any event.
  • You only update observers who are effectively interested in each event.
  • You avoid "empty methods", "event type checking" and other tricks on the observer side.
+5
source

You can check the status change by doing the following in the Observable class:

 public void update(Observable o, Object arg) { if(o.hasChanged()) { // do something } } 

Observers who listen to something do not need this test. This is probably the easiest way if you only want to listen to state changes.

0
source

All Articles