Top Banner
By SmartBoard Team
28

Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Jul 03, 2015

Download

Technology

Welcome message from author
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
Page 1: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

By SmartBoard Team

Page 2: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Agenda

Scenario & Writing UML Discuss with each team’s UML What if… BMVV design Strategy pattern Applied with BMVV design More exercise..

Page 3: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Scenario Act yourself as Car company owner

Car component will consist of Engine Type

Fuel used

Nitrous (Additional boost)

Page 4: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Scenario (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…

Page 5: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Writing UML ~10-15 min

Take your time…

Page 6: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Time’s up….

Let’s see your UML diagram

Page 7: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

What if..

More engine type is added More fuel type is added More nitrous type is added Some car can’t use some engine No more benzene ??

Can you design support this changes?

Page 8: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Let’s record your problem

Page 9: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

BMVV Car company designB

cd Car

Car

# engine: String# fuel: String# boost: String- accel: double

+ setBoost(String) : void+ setEngine(String) : void+ BurnFuel(double) : double+ getFuelName() : String+ getEngineName() : String+ getBoostName() : String# getBurnFuel(double) : double+ performBoost(double) : double

JazzCar

+ JazzCar()PorcheCar

+ PorcheCar()

TankCar

+ TankCar()

Page 10: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

BMVV Car company design #2B

cd Car

Car

# engine: String# fuel: String# boost: String- accel: double

+ setBoost(String) : void+ setEngine(String) : void+ BurnFuel(double) : double+ getFuelName() : String+ getEngineName() : String+ getBoostName() : String# getBurnFuel(double) : double+ performBoost(double) : double

JazzCar

+ JazzCar()PorcheCar

+ PorcheCar()

TankCar

+ TankCar()

protected double getBurnFuel(double distance){if(engine.equalsIgnoreCase("1500CC") && fuel.equalsIgnoreCase("Benzene")){ double consume = distance*0.054; consume = consume*18/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")){ ……. }…………..…….…

Page 11: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Problems 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(“4500CC”)&& fuel.equalsIgnoreCase(“SuperGasohol”)){ …… }

else if(engine.equalsIgnoreCase(“1900CC”)&& fuel.equalsIgnoreCase(“SuperGasohol”)){ …… }

Page 12: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Unable to encapsulates data

According to this design, if you company need to outsource your work…

Confidential

$$$$$

Page 13: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Strategy 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

Page 14: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Communication between a station and an unite

Every message will be encrypted.

Thai military communication network

Page 15: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Old cipher machine model

Machine

Conditions2. decryptA3. decryptB4. decryptC5. None

(RTA,2)

SUB1500 lines

Break encapsulation rule

Page 16: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

New cipher machine model

Machine

(RTA, object)

SUB

<interface>

decryptB

None

decryptA

Object.decrypt(“RTA”)

Alternative subclass

Family of algorithms

Eliminate conditions

A choice of implementation

s

Page 17: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

New 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

Page 18: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Consequences

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.

Page 19: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Another example : Sort Assume that, you need to write a sort program There’re many kinds of sort.. Strategy is what we group the many algorithms

that do the same things and make it interchangeable at run-time

Bubble Sort

Selection SortDescending

Sort

HeapSort

QuickSort

AscendingSort

SortStrategies

Input #1

Result #1

Input #2

Result #2

Runtime

Page 20: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Another example : Sort #2

Change the algorithm at runtime

Page 21: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Apply strategy with BMVV design Indicates the problem..

I

cd Car

Car

# engine: String# fuel: String# boost: String- accel: double

+ setBoost(String) : void+ setEngine(String) : void+ BurnFuel(double) : double+ getFuelName() : String+ getEngineName() : String+ getBoostName() : String# getBurnFuel(double) : double+ performBoost(double) : double

JazzCar

+ JazzCar()PorcheCar

+ PorcheCar()

TankCar

+ TankCar()

Hard to maintain

Move it to the new class…

Page 22: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Apply strategy with BMVV design Re-designing

Need getBurnFuel()from different factor

So as Nitrous

Page 23: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Apply strategy with BMVV design Overall design

Ocd carPattern

Car

# Boost: Nitrous# engine: Engine- Accel: double

+ performBoost(double) : double+ «property set» setBoost(Nitrous) : void+ setEngine(Engine) : void+ BurnFuel(double) : double+ Run() : void+ Fuel() : void

«interface»

Engine

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine1500CCBenzene

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine1500CCDiesel

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine1500CCGasohol

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine1900CCBenzene

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine1900CCDiesel

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine1900CCGasohol

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine2500CCBenzene

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine2500CCDiesel

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Engine2500CCGasohol

+ getBurnFuel(double) : double+ getEngineName() : String+ getFuelName() : String

Garage

+ main(String[]) : void

JazzCar

+ JazzCar()+ Fuel() : void

«interface»

Nitrous

+ getBurnFuel(double) : double+ getFuelName() : String

NitrousBenzene

+ getBurnFuel(double) : double+ getFuelName() : String

NitrousDiesel

+ getBurnFuel(double) : double+ getFuelName() : String

NitrousGasohol

+ getBurnFuel(double) : double+ getFuelName() : String

NoBoost

+ getBurnFuel(double) : double+ getFuelName() : String

PorcheCar

+ PorcheCar()+ Fuel() : void

TankCar

+ TankCar()+ Fuel() : void

#engine #Boost

If 4500cc engine is added?Engine4500ccBenzene Engine4500ccDieselEngine4500ccGasohol

If no more benzene?

Easier when doing the maintenance !!!

Page 24: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Apply strategy with BMVV design How it works at runtime

Jazz is changing its engine

It will call BurnFuel(double) according to its engine

Page 25: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

An action adventure game

Page 26: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Solutions of exercise

Page 27: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

Q/A

Page 28: Ku bangkok sw-eng-dp-02_smart_board_strategypattern

References www.viewimages.com www.thailandstrategy.com http://www.maxituning.pl/archiwum/0412/pics/technik

a/nos2.jpg www.mysticcobra.net/system/ http://jpfamily.net/Coolpix/NHRA/slides/ http://www.robinstewart.com/products/graphics/gs_ic

on.gif www.freewarebox.com/screenshot_2476_clock.html http://tv.pcworld.hu/apix/0612/Data%20thief.png http://www.offthemarkcartoons.com/cartoons/