How to perform various operations in Observer update () in Java?

I just started playing with the Observable , Observer and its update() method, and I cannot figure out what to do when different actions call notifyObservers() .

I mean, my Observable class has several different methods, which are called setChanged() and notifyObservers() at the end. Depending on the method being called, it is necessary to update a part of the user interface (Swing). However, only one update() method is implemented in the Observer class.

Although I pass something to the notifyObservers() method and then I can check the argument for update() , it’s not a good way to do this. Even if that were the case, what did I have to go through? A line with a brief description of the action / method? Int, how is the action / method code? Something else?

What is the best way to deal with this situation?

+6
java observer-pattern observable
source share
2 answers

in the general case, you should update everything that can be observed when you receive a call to update (). if this is not practical, you can pass the notifyObservers () hint.

the bandit of the book says that one of the consequences of the observer pattern:

“Unexpected updates. Since observers do not know about each other, they may be blind to the maximum cost of changing an item. Apparently, a harmless operation on this issue can cause a cascade of updates for observers and their dependent objects. Moreover, dependency criteria that are not defined or maintained, usually lead to false updates that are difficult to track.

This problem is compounded by the fact that a simple update protocol does not contain details about what has changed in the topic. Without an additional protocol, to help observers discover what has changed, they may be forced to work to push the changes. "also in the implementation process, they say:

"Avoid specific observer update protocols: push and pull models. Implementations of the Observer pattern often provide the object with additional information about the change. The subject passes this information as an argument for updating. The amount of information can vary widely.

At some extreme, which we call the push model, the subject sends observers detailed information about the change, whether they want it or not. On the other hand, a traction model; the subject sends only the most minimal notification, and observers ask for further details.

The traction model emphasizes the subjective ignorance of its observers, while the push model assumes that objects know something about the needs of their observers. The push model can make observers less reusable because Subject classes make assumptions about Observer classes that may not always be true. On the other hand, the pull model can be inefficient because the Observer classes need to figure out what has changed without Subject's help. "

+7
source share

The second parameter update() is of type Object , so you can pass anything that is appropriate. As you noticed, the approach is quite general. In contrast, a class supporting EventListenerList can receive a certain degree of security at runtime, if one is specified.

+3
source share

All Articles