Assignment 3
Assignment 3
Software Engineering
Design Patterns
Examples
Peter Bunus
Dept of Computer and Information Science
Linköping University, Sweden
[email protected]
pelab
pelab
Decorator
Pattern
Beverage
The cost() method is
-description abstract; subclasses need to
+getDescription() define their implementation
+cost()
Inheritance doesn’t worked very well for us. What we should do?
Hi Jamie. One of my
guys have problem with
coffee classes. Could
you please help him out.
1. Take the DarkRoast object
2. Decorate it with a Mocha
object
3. Decorate it with the Whip
object
4. Call the cost() method and
relay on delegation to add to the
condiment cost.
1
class CondimentDecorator : public Beverage{
+getDescription()()
public:
HouseBlend Expresso CondimentDecorator(){};
virtual string getDescription()=0;
+cost() +cost() };
Milk Soy
Decaf DarkRoast -beverage : Beverage -beverage : Beverage
+cost() +cost()
+cost() +cost() +getDescription() +getDescription()
A Whipped Dark
Roast with double
Mocha
expresso
void main(){
cout << "Testing the Coffe Shop application" << endl;
0.99
double Whip::cost(){
0.99+0.9+0.9 0.99+0.9 return beverage->cost() + 0.76;
}
double Mocha::cost(){
0.99+0.9+0.9 + 0.76 = 3.55 return beverage->cost() + 0.9;
}
double DarkRoast::cost(){
return 0.99;
}
TDDB84 Design Patterns Peter Bunus 15
pelab
The Decorator Pattern
The Mediator
Flight 34
Flight 456
The Mediator defines an object that controls how a set of objects interact.
The pilots of the planes approaching or departing the terminal area communicate with
the tower, rather than explicitly communicating with one another.
The constraints on who can take off or land are enforced by the tower.
the tower does not control the whole flight. It exists only to enforce constraints in the
terminal area.
TDDB84 Design Patterns Peter Bunus 23
pelab
The Mediator – Another Example
Bob lives in the HouseOfFuture where everthing is automated:
When Bob hits the snooze button of the alarm the coffee maker starts
brewing coffee
No coffee in weekends
.......
onEvent(){
onEvent(){ checkCalendar();
checkCalendar(); checkAlarm();
checkSprinkler(); //do more stuff
startCoffee(); }
//do more stuff
}
onEvent(){ onEvent(){
checkDayOfTheWeek(); checkCalendar();
doShower(); checkShower();
doCoffee(); checkTemperature
doAlarm(); //do more stuff
//do more stuff }
}
TDDB84 Design Patterns Peter Bunus 24
pelab
The Mediator in Action
With a Mediator added to the system all the appliance objects can be
greatly simplified
They tell the mediator when their state changes
It’s such a relief,
They respond to requests from the Mediator not having to
figure out that
Alarm clock picky
rules
if(alarmEvent)(){
checkCalendar();
checkShower();
checkTemp();
//do more stuff
}
if(weekend){
checkWeather();
}
if(trashDay){
resetAlarm();
}
«interface» «interface»
Mediator Collegue
+broadcasEvent() +handleEvent()
+changed()
ConcreteMediator
* ConcreteColleague1 ConcreteCollague2
+broadcastEvent()
+handleEvent() +handleEvent()
+changed() +changed()
* * *
Mediator
defines an interface for communicating with Colleague objects
ConcreteMediator
implements cooperative behavior by coordinating Colleague objects
knows and maintains its colleagues
Colleague classes (Participant)
each Colleague class knows its Mediator object (has an instance of the
mediator)
each colleague communicates with its mediator whenever it would have
otherwise communicated with another colleague
TDDB84 Design Patterns Peter Bunus 27
pelab
Yet Another Example
Robbery in
progress. I
need backup
Officer down,
officer
down!!! We
have
casualties
Changing the system behavior means just sub classing the mediator. Other
objects can be used as is.
Since the mediator and its colleagues are only tied together by a loose
coupling, both the mediator and colleague classes can be varied and
reused independent of each other.
Since all the interaction between the colleagues are bundled into the
mediator, it has the potential of making the mediator class very complex and
monolithically hard to maintain.
System-Architecture OO Architecture
Application-Architecture Subsystem
Macro-Architecture Frameworks
Micro-Architecture Design-Patterns
Objects OO Programming