Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform different operations within Observer's update() in Java?

I just started playing with Observable, Observer and it's update() method and I can't understand what should I do when different actions call notifyObservers().

I mean, my Observable class has a few different methods that call setChanged() and notifyObservers() in the end. Depending on the called method, some part of the UI (Swing) needs to be updated. However, there is only one update() method implemented in the Observer class.

I though of passing something to the notifyObservers() method and then I can check the argument on update() but it doesn't feel like a good way to do it. Even if it did, what should I pass? A string with a short description of the action/method? An int, like an action/method code? Something else?

What's the best way to handle this situation?

like image 307
rfgamaral Avatar asked Jun 01 '10 02:06

rfgamaral


People also ask

How do you use Observer pattern?

Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category.

How does the Observer pattern work in java?

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. The object which is being watched is called the subject. The objects which are watching the state changes are called observers or listeners.

What can I use instead of Observer in java?

So, what's the alternative we have ? You can use PropertyChangeEvent and PropertyChangeListener from java.


2 Answers

in general you should update everything from the observable when you get a call to update(). if that is not practical, you can pass a hint to notifyObservers().

the gang-of-book says that one of the consequences of the observer pattern is:

"Unexpected updates. Because observers have no knowledge of each other's presence, they can be blind to the ultimate cost of changing the subject. A seemingly innocuous operation on the subject may cause a cascade of updates to observers and their dependent objects. Moreover, dependency criteria that aren't well-defined or maintained usually lead to spurious updates, which can be hard to track down.

This problem is aggravated by the fact that the simple update protocol provides no details on what changed in the subject. Without additional protocol to help observers discover what changed, they may be forced to work hard to deduce the changes. " also under implementation, they say:

"Avoiding observer-specific update protocols: the push and pull models. Implementations of the Observer pattern often have the subject broadcast additional information about the change. The subject passes this information as an argument to Update. The amount of information may vary widely.

At one extreme, which we call the push model, the subject sends observers detailed information about the change, whether they want it or not. At the other extreme is the pull model; the subject sends nothing but the most minimal notification, and observers ask for details explicitly thereafter.

The pull model emphasizes the subject's ignorance of its observers, whereas the push model assumes subjects know something about their observers' needs. The push model might make observers less reusable, because Subject classes make assumptions about Observer classes that might not always be true. On the other hand, the pull model may be inefficient, because Observer classes must ascertain what changed without help from the Subject. "

like image 64
Ray Tayek Avatar answered Oct 09 '22 13:10

Ray Tayek


The second parameter to update() is of type Object, so you can pass anything appropriate. As you note, the approach is rather general. In contrast, a class that maintains an EventListenerList can get a degree of run-time type safety when used as specified.

like image 26
trashgod Avatar answered Oct 09 '22 13:10

trashgod