Understanding Java's Observer Design Pattern in MVC, Study notes of Computer Science

An overview of java's observer class and its observable interface, which are essential components of the observer design pattern used in the model-view-controller (mvc) design pattern. The relationship between observer, observable, and mvc, and provides an example of how to implement the observer pattern using java. Students will learn how to set up the code to have the controller let users click a button that sends a message to the model, which in turn notifies the observer(s) of the state change.

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-q3p
koofers-user-q3p 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 335 Section 2- and 3-Nov
Read this page and discuss Java's Observer class and its Observable interface.
Begin the exercise in pairs (or solo)
Model-View-Controller (MVC) is a classic design pattern often used by applications that need the ability
to maintain multiple views of the same data. The MVC pattern hinges on a clean separation of objects into
one of three categories — models for maintaining data, views for displaying all or a portion of the data,
and controllers for handling events that affect the model or view(s).
Because of this separation, multiple views and controllers can interface with the same model. Even new
types of views and controllers that never existed before can interface with a model without forcing a
change in the model design. The MVC abstraction can be graphically represented as follows.
Events typically cause a controller to change a model, or view, or both. Whenever a controller changes a
model’s data or properties, all dependent views are automatically updated. Similarly, whenever a
controller changes a view, for example, by revealing areas that were previously hidden, the view gets data
from the underlying model to refresh itself.
Observer/Observable
The observer pattern usually plays a part in MVC. Java provides support with the Observer class and
Observable interface. The design allows one Observable to notify all registered observables
that a change of state has occurred.
implements
Observable
Vector my_observers
addObserver
(Observer)
notifyObservers
()
-counter: int
+increment()
+currentCount(): int
for each o in my_observers
o.update(this)
<<interface>>
Observer
update(Observable)
ViewOne
1
*
1..*
ViewTwo
update
JPanel
paintComponent
update
JPanel
stateChanged
pf3
pf4

Partial preview of the text

Download Understanding Java's Observer Design Pattern in MVC and more Study notes Computer Science in PDF only on Docsity!

C Sc 335 Section 2- and 3-Nov

 Read this page and discuss Java's Observer class and its Observable interface.

 Begin the exercise in pairs (or solo)

Model-View-Controller (MVC) is a classic design pattern often used by applications that need the ability

to maintain multiple views of the same data. The MVC pattern hinges on a clean separation of objects into

one of three categories — models for maintaining data, views for displaying all or a portion of the data,

and controllers for handling events that affect the model or view(s).

Because of this separation, multiple views and controllers can interface with the same model. Even new

types of views and controllers that never existed before can interface with a model without forcing a

change in the model design. The MVC abstraction can be graphically represented as follows.

Events typically cause a controller to change a model, or view, or both. Whenever a controller changes a

model’s data or properties, all dependent views are automatically updated. Similarly, whenever a

controller changes a view, for example, by revealing areas that were previously hidden, the view gets data

from the underlying model to refresh itself.

Observer/Observable

The observer pattern usually plays a part in MVC. Java provides support with the Observer class and

Observable interface. The design allows one Observable to notify all registered observables

that a change of state has occurred.

implements

Observable

Vector my_observers

addObserver (Observer)

notifyObservers

SimpleModelFromThePast

- counter: int

+increment()

+currentCount(): int

for each o in my_observers o.update(this)

<>

Observer

update(Observable)

ViewOne

ViewTwo

update JPanel paintComponent update JPanel

stateChanged

Exercise. Using the uml diagram and screenshots below, in teams of 2, set up the code to have the

controller let users click the button that sends a message to the model, which in turn notifies the observer(s) of

the state change. The model can send stateChanged and notifyObserver message from anywhere when it

thinks observers should know the state has changed. The view updates itself by showing an oval in the current color

of the mode that changes according to its own rules (these are given in the changeColor method).

// 3. Implement class View // 4. If time permits, Implement class ViewTwo to show the toString version // of the model's current color in a Jlabel on a JPanel // 5. What would you have to do for this second view to be swapped at runtime