Model/View/Controller
- Some programs have multiple editable views
- Example: HTML Editor
- WYSIWYG view
- structure view
- source view
- Editing one view updates the other
- Updates seem instantaneous
Model/View/Controller
Model/View/Controller
- Model: data structure, no visual representation
- Views: visual representations
- Controllers: user interaction
Model/View/Controller
- Views/controllers update model
- Model tells views that data has changed
- Views redraw themselves
Model/View/Controller
Observer Pattern
- Model notifies views when something interesting happens
- Button notifies action listeners when something interesting
happens
- Views attach themselves to model in order to be notified
- Action listeners attach themselves to button in order to be
notified
- Generalize: Observers attach themselves to subject
Observer Pattern
Context
- An object, called the subject, is source of events
- One or more observer objects want to be notified when such an
event occurs.
Solution
- Define an observer interface type. All concrete observers
implement it.
- The subject maintains a collection of observers.
- The subject supplies methods for attaching and detaching
observers.
- Whenever an event occurs, the subject notifies all observers.
Observer Pattern
Names in Observer Pattern
Name in Design Pattern
|
Actual Name (Swing buttons)
|
Subject
|
JButton
|
Observer
|
ActionListener
|
ConcreteObserver
|
the class that implements the ActionListener
interface type
|
attach()
|
addActionListener()
|
notify() |
actionPerformed()
|
Maintained by John Loomis,
last updated 25 February 2007