Jan Turoň
Jan Turoň

Reputation: 32912

Understanding MVC using Observer in Java

I study Java MVC from this example.

If the View is Observer and the Model Observable, why the Observer.update method accepts Observable in the first parameter? If the View worked with the Model (relying on some of its methods), it wouldn't be reusable. Shouldn't be the second parameter the only thing that the View should work with?

Upvotes: 1

Views: 584

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

why the Observer.update method accepts Observable in the first parameter?

The observable is what initiates the update call, and it passes a reference to itself in this call so that the observer knows without doubt who is making this call. Imagine what would happen if the observer is observing several observables at once. This type of parameter passing will help eliminate confusion as to the source of this call. It's really little different from a passing a reference to the calling object in a Swing ChangeEvent object or an ActionEvent object.

Upvotes: 1

Related Questions