Title: Strategy Pattern
1Strategy Pattern
2Agenda
- Scenario Writing UML
- Discuss with each teams UML
- What if
- BMVV design
- Strategy pattern
- Applied with BMVV design
- More exercise..
3Scenario
- Act yourself as Car company owner
- Car component will consist of
- Engine Type
- Fuel used
- Nitrous (Additional boost)
4Scenario (2)
- From its components, we can calculate
- the fuel burn rate. So, it should include method
getBurnFuel(double distance). - It comes from Engines power Nitrous power (if
used) - Each car can used only
- One type of engine
- One type of Fuel
- One type of Nitrous
- You need some program to
- show fuel burn rate for each car
- Car components can be changed in the future.
- Every team, please write UML Class diagram to
according to the requirement above
5Writing UML 10-15 min
6Times up.
- Lets see your UML diagram
7What if..
- More engine type is added
- More fuel type is added
- More nitrous type is added
- Some car cant use some engine
- No more benzene ??
- Can you design support this changes?
8Lets record your problem
9BMVV Car company design
10BMVV Car company design 2
protected double getBurnFuel(double
distance) if(engine.equalsIgnoreCase("1500CC")
fuel.equalsIgnoreCase("Benzene"))
double consume distance0.054 consume
consume18/17 return consume else
if(engine.equalsIgnoreCase("1500CC")
fuel.equalsIgnoreCase("Diesel")) ..
else if(engine.equalsIgnoreCase("1500CC")
fuel.equalsIgnoreCase("Gasohol")) ..
else if(engine.equalsIgnoreCase("1900CC")
fuel.equalsIgnoreCase("Benzene")) .
.. .
11Problems occurs when
- If 4500cc engine are added
- No more benzene used.
- Super Gasohol is invented.
- If each case has more conditions.
else if(engine.equalsIgnoreCase(4500CC)
fuel.equalsIgnoreCase(Benzene)) else
if(engine.equalsIgnoreCase(4500CC)
fuel.equalsIgnoreCase(Diesel)) else
if(engine.equalsIgnoreCase(4500CC) Another
fuel type ) . . .
else if(engine.equalsIgnoreCase(1500CC)
fuel.equalsIgnoreCase(SuperGasohol))
else if(engine.equalsIgnoreCase(1900CC)
fuel.equalsIgnoreCase(SuperGasohol))
else if(engine.equalsIgnoreCase(4500CC)
fuel.equalsIgnoreCase(SuperGasohol))
12Unable to encapsulates data
- According to this design, if you company need to
outsource your work
Confidential
13Strategy patterns
- Define a family of algorithms, encapsulate, and
make them interchangeable. Strategy lets the
algorithm vary independently from clients that
use it.
http//en.wikipedia.org/wiki/Strategy_pattern
14Thai military communication network
Every message will be encrypted.
Communication between a station and an unite
15Old cipher machine model
Break encapsulation rule
- Conditions
- decryptA
- decryptB
- decryptC
- None
(RTA,2)
Machine
1500 lines
SUB
16New cipher machine model
Alternative subclass
Object.decrypt(RTA)
ltinterfacegt
(RTA, object)
decryptB
Machine
decryptA
Eliminate conditions
Family of algorithms
A choice of implementations
None
SUB
17New cipher machine model
- Decrypt A and B can use signature only a string.
Object.decrypt(String) - But Decrypt C need more parameters.
Object.decrypt(String, int) - So method signature should have two parameters.
Object.decrypt(String, int) - Ex . ObjectA.decrypt(RTA, Null)
ObjectB.decrypt(RTA, Null)
ObjectC.decrypt(RTA, 366)
Communication overhead
18Consequences
- Families of related algorithms.
- An alternative to subclassing.
- Strategies eliminate conditional statements.
- A choice of implementations.
- Clients must be aware of different strategies.
- Communication overhead between Strategy and
Context. - Increased number of objects.
19Another example Sort
- Assume that, you need to write a sort program
- Therere many kinds of sort..
- Strategy is what we group the many algorithms
that do the same things and make it
interchangeable at run-time
Ascending Sort
SortStrategies
Bubble Sort
Selection Sort
Descending Sort
Result 1
Result 2
Input 1
Input 2
Heap Sort
Quick Sort
20Another example Sort 2
Change the algorithm at runtime
21Apply strategy with BMVV design
Hard to maintain
Move it to the new class
22Apply strategy with BMVV design
Need getBurnFuel() from different factor
So as Nitrous
23Apply strategy with BMVV design
If 4500cc engine is added?
Engine4500ccBenzene
Engine4500ccDiesel
Engine4500ccGasohol
Easier when doing the maintenance !!!
If no more benzene?
24Apply strategy with BMVV design
It will call BurnFuel(double) according to its
engine
Jazz is changing its engine
25An action adventure game
Start
26Solutions of exercise
27Q/A
28References
- www.viewimages.com
- www.thailandstrategy.com
- http//www.maxituning.pl/archiwum/0412/pics/techni
ka/nos2.jpg - www.mysticcobra.net/system/
- http//jpfamily.net/Coolpix/NHRA/slides/
- http//www.robinstewart.com/products/graphics/gs_i
con.gif - www.freewarebox.com/screenshot_2476_clock.html
- http//tv.pcworld.hu/apix/0612/Data20thief.png
- http//www.offthemarkcartoons.com/cartoons/