Professional Documents
Culture Documents
History
A
developed by Xerox PARC for Smalltalk-80. Used by the Application Kit system in NeXTstep. Used by the Cocoa APIs for Apples OS X. Recommended structural framework pattern in J2EE.
I
Observer Pattern
Defines
a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Used to decouple the subject from the observer, since the subject needs little information to notify the observer. Can result in excessive notifications.
Copyright 2001 DeLorme 28 November 2001
Observer
+update(Observable, Object)
AccountView
+update(Observable, Object)
BankAccount
+widthdraw(double) : long +deposit(double) : long +getBalance() : double
Copyright 2001 DeLorme 28 November 2001
SummaryView
+update(Observable, Object)
Transactions Happen!
Controller
deposit() setChanged() notifyObservers() update() getBalance() update() getBalance()
BankAccount
AccountView
SummaryView
Observer Rocks!
The
Observer pattern allowed the BankAccount class to notify multiple views without minimal information. Observers can register themselves with their Subjects. No strings attached!
Transactions
Architecture Diagram
Model
business logic Get State Update Event Change View Set
State
View
model representation
User Actions
Controller
user interaction
Advantages
Separation
Problems
Problems
of translation:
Business logic bleeds into the Controller. Observer pattern is non-obvious for EJBs. See EJBObserver by Greg Comeau.
Problems
of the pattern:
Excessive coupling between the Model and View and the Model and Controller. Frozen interfaces are hard to manage!
Copyright 2001 DeLorme 28 November 2001