How to perform different operations in update() of observer in Java?

I just started using observable, observer and its update () method. When different actions call notifyobservers (), I can't understand what I should do

I mean, my Observable class has several different ways to call setChanged () and notifyObservers () finally. Some parts of the UI (swing) need to be updated according to the called method 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 parameters on update (), it is not a good method Even if it does, what should I pass? A string containing a short description of the action / method? An int, like an action / method code? What else?

What is the best way to deal with this situation?

Solution

Usually, when you receive update (), you should update everything from observable If this is not possible, you can pass the prompt to notifyobservers()

The group said that one of the consequences of the observer model is:

"Unexpected updates. Because observers do not understand each other's existence, they may turn a blind eye to the final cost of changing the subject. Seemingly harmless operations on the subject may lead to cascading updates of observers and their dependent objects. In addition, dependencies without clearly defined or maintained standards usually lead to false updates, which is difficult to trace

This problem is exacerbated by the fact that the simple update protocol does not provide details about the changes that occur in the subject Without additional protocols to help observers detect changes, they may be forced to try to infer changes "They also stated:

"Avoid observer specific update protocols: push and pull models. The implementation of observer mode usually allows the topic to broadcast additional information about changes. The topic passes this information to the update as a parameter. The amount of information may vary greatly

At one extreme, we call it the push model, in which the subject sends detailed information about the change to the observer, whether they like it or not The other extreme is the pull model; Apart from the minimal notification, the subject only sends anything, and the observer will then explicitly ask for details

Pull model emphasizes the subject's ignorance of its observer, while push model assumes that the subject knows the needs of the observer The push model may reduce the reusability of observers, because the subject class makes assumptions about the Observer class, which may not always be the case On the other hand, the pull model may be inefficient because the Observer class must determine what has changed without topic help “

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>