Strategy Design Pattern

16
http://jy aasa.com Copyright 2016. Jyaasa Technologies. Strategy Pattern

Transcript of Strategy Design Pattern

Page 1: Strategy Design Pattern

http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Strategy Pattern

Page 2: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Ganesh Kunwar

Sr. Software Engineer

Jyaasa Technologies

Page 3: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

What is Strategy Pattern?

Page 4: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Page 5: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Ktm ButwalTaxi Plan Taxi

Ktm. Airport

Bhairahawa Airport

Ktm ButwalTaxi Bus

Kalanki

Ktm ButwalTaxi Bus BusKalanki

Chitwan

Strategy 1

Strategy 2

Strategy 3

Page 6: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Wikipedia Definition

the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm's behavior to be selected at runtime. The strategy pattern

● defines a family of algorithms,● encapsulates each algorithm, and● makes the algorithms interchangeable within that

family.

Page 7: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

When to Use?

Shopping Mall Example- Accept customer detail- Calculate bill amount- Apply discount based on the day of week

- Monday - High discount (10%)- Friday - Low discount (5%)

Page 8: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Where does the discount logic reside?

Inside ShoppingMall class

Or

Outside ShoppingMall class

Page 9: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Page 10: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Open closed principle

- software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification

- New discount logic may be applied in future

- Not reusable - Same discount strategy can be use in other shopping mall

Page 11: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Separate discount logic from ShoppingMall class

Page 12: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Context Strategy

Page 13: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

Page 14: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

=> grill = Grill.new(HotDog.new)=> grill.grilling # => "Grilling the hot dogs!"

=> grill.food = Hamburger.new

=> grill.grilling # => "Grilling the hamburgers!"

=> grill.food = VeggiePatty.new

=> grill.grilling # => "Grilling the veggie patties!"

Page 15: Strategy Design Pattern

Copyright 2016. Jyaasa Technologies.

http://jyaasa.com

References

- https://dockyard.com/blog/2013/07/25/design-patterns-strategy-pattern - https://en.wikipedia.org/wiki/Strategy_pattern - https://sourcemaking.com/design_patterns/strategy

Page 16: Strategy Design Pattern

Thank you!

Lets Discuss

github.com/glunwar@gkunwar1

Copyright 2015. Jyaasa Technologies. Copyright 2016. Jyaasa Technologies.

http://jyaasa.com