This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CS 247: Software Engineering Principles
Design Patterns (Observer, MVC)
Reading: Freeman, Robson, Bates, Sierra, Head First Design Patterns, O'Reilly Media, Inc. 2004
Ch 2 Observer PatternCh 12 Model-View-Controller
MVC example among gtkmm examples provided in project 2
U Waterloo CS247 (Spring 2015) — p.1/31
Problem: Synchronized Views
U Waterloo CS247 (Spring 2015) — p.2/31
a b c a
b c Views
a = 50% b = 30% c = 20%
Model a b c
50 30 20
Multiple applicationseditor: main canvas, thumbprints, editor paletteinternet games: distributed players see all updates
Solution #1: Coupled Design
U Waterloo CS247 (Spring 2015) — p.3/31
update(state)Percentage
update(state)BarGraph
update(state)PieChart
+change()#notifyViews()
Data
percent
bargraph
piechart
void Data:notifyViews() {percent_->update( state );bargraph_->update( state );piechart_->update( state );
}
Solution #2: Aggregation of Abstract Views
U Waterloo CS247 (Spring 2015) — p.4/31
• Data refers to collection of abstract views• Notifying views means iterating through collection
*1+update(state)
View
update(state)Percentage
update(state)BarGraph
update(state)PieChart
+change()#notify()
Data
Improved Design
U Waterloo CS247 (Spring 2015) — p.5/31
In addition, let's provide methods for adding and removing displays from the collection.
Might provide methods for getting and setting state information (more later).
getState ()setState ()
- subjectStateConcreteSubject
Problem: maintaining consistency among related objects
Solution: Subject sends updates to collection of abstract ObserversSubject maintains collection of subscribed observers, and sends notifications to all when its state changes.
update(state)- objectStateConcreteObserver
Observer Pattern in Action
U Waterloo CS247 (Spring 2015) — p.7/31
:Data :Percentage :BarGraph :PieChart
editstate changes
update(state)update(state)
update(state)
Each Observer is triggered by the same abstract operation
update viewupdate view
update view
notify()
1
2
3
4
A Different Design: Push vs. Pull
U Waterloo CS247 (Spring 2015) — p.8/31
s:Subject :Observer :Observer :Observeredit
state changes
update( )
update( )
update( )
accessor(...)
accessor(...)
accessor(...)
each Observer is triggered by the same abstract operation
each Observer uses Subject's accessors to retrieve state information
• In previous design, the Subject pushes state information to Observers. • An alternative design would have each Observer request the specific
information it needs, on notification of a change. • The decision to push or pull update information depends on the
frequency of operations and the complexity of the data.
notify()
Example
U Waterloo CS247 (Spring 2015) — p.9/31
Consider the example of an automobile and its onboard status computer. The computer monitors the all of the vital data about the status of the vehicle. The computer receives this informa8on from the onboard sensors, and then relays it to the OnStar, Low Jack, Fleet Tracker, and gauge cluster and dash board systems. These systems then interpret the data and abstract it for each of their own par8cular uses. For instance, the onboard status computer reports informa8on about the status of the headlights on the vehicle (ON or OFF), this informa8on would be irrelevant to a low jack system that is responsible for tracking the vehicle should it be stolen. All of the individual systems have unique uses for the data about the vehicle, but all of the data collec8vely comes from the same source.
int m_speed;double m_position;bool m_locked;bool m_lights;int m_fuelLevel;bool m_windows; bool m_wipers; int m_engineTemp; long m_odometer; long m_vin; bool m_isOn; int m_systemTime; // would use a time object here, but for demonstration sake..double m_economy; // fuel points/km
m_isOn = true;m_engineTemp = 200;m_position += (double)distance;m_fuelLevel -= (distance * m_economy); // arbitrary constantm_odometer += abs(distance); // distance is a vector, odometers only go
up, position can go up and downm_speed = speed;m_lights = atNight;m_wipers = inRain;m_windows = windowsOpen;notify();
}
All manipulators include call to notify observers.
Concrete Subject
U Waterloo CS247 (Spring 2015) — p.12/31
Each concrete observer overrides Observer update() method, to retrieve and display the vehicle information it cares about. //****** Dashboard.cpp *******void Dashboard::update ( Vehicle* s ){