next up previous contents
Next: Snapshot Pattern: Persistent Objects Up: Patterns Previous: Object Pool Pattern: Reusable   Contents

Observer Pattern: Update Notification [60]



$\bullet$ Problem: How to maintain consistency between multiple observers of an observable object's state information?

$\bullet$ Solution: Observer Pattern

$\bullet$ Outline: Consider an Observable Supplier object with Number, Name, Status, City. Two Observer Frames both display the information about this particular Supplier:


\begin{picture}(340,120)(30,660)\thicklines
\put( 30,660){\framebox (130,120)...
...5,675){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm SUBMIT}}}
\end{picture}



$\bullet$ A user modifies two fields in the first Frame, and pushes SUBMIT

$\bullet$ The goal is to have the other Frame(s) automatically refresh their own displays:




\begin{picture}(340,120)(30,660)\thicklines
\put( 30,660){\framebox (130,120)...
...){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm City: Paris}}}
\end{picture}



Observer Pattern: UML [61]




\begin{picture}(330,200)(35,620)\thicklines
\put(255,720){\framebox (110,100)...
...ut(245,650){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm *}}}
\end{picture}

$\bullet$ Supplier inherits from Java's Observable class

$\bullet$ ObsFrame implements Java's Observer interface

$\bullet$ Many ObsFrames observe the one Supplier

$\bullet$ Each ObsFrame registers itself with the Supplier via addObserver(this)

$\bullet$ addObserver() puts the Observer into the Vector of all Observers

$\bullet$ If a ObsFrame does a SUBMIT, then it mutates the Supplier

$\bullet$ Supplier calls notifyObservers()

$\bullet$ notifyObservers() iterates through the Vector, and calls each Observers's update()

$\bullet$ update() is the automatic update (callback) which allows the ObsFrame to refresh the display

$\bullet$ Refresh can be performed by using the Supplier's accessor methods

$\bullet$ SEE CASE STUDY


next up previous contents
Next: Snapshot Pattern: Persistent Objects Up: Patterns Previous: Object Pool Pattern: Reusable   Contents
Ted Billard 2006-09-26