Cómo realizar diferentes operaciones dentro de la actualización del observador () en Java?

StackOverflow https://stackoverflow.com/questions/2946917

Pregunta

Yo sólo empezó a jugar con Observable, Observer y del método update() y no puedo entender lo que debería hacer cuando diferentes acciones llaman notifyObservers().

Me refiero a mi clase Observable tiene algunos métodos diferentes que setChanged() llamada y notifyObservers() al final. Dependiendo del método llamado, una parte de la interfaz de usuario (Swing) necesita ser actualizado. Sin embargo, sólo hay un método update() implementado en la clase Observer.

I, aunque algo de pasar al método notifyObservers() y luego puedo comprobar el argumento de update() pero no se siente como una buena manera de hacerlo. Incluso si lo hiciera, lo que debería pasar? Una cadena con una breve descripción de la acción / método? Un int, como un código de acción / método? Algo más?

¿Cuál es la mejor manera de manejar esta situación?

¿Fue útil?

Solución

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top