Strategy Design Pattern
-
Upload
jyaasa-technologies -
Category
Software
-
view
121 -
download
4
Transcript of Strategy Design Pattern
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Strategy Pattern
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
Ganesh Kunwar
Sr. Software Engineer
Jyaasa Technologies
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
What is Strategy Pattern?
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
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
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.
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%)
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
Where does the discount logic reside?
Inside ShoppingMall class
Or
Outside ShoppingMall class
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
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
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
Separate discount logic from ShoppingMall class
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
Context Strategy
Copyright 2016. Jyaasa Technologies.
http://jyaasa.com
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!"
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
Thank you!
Lets Discuss
github.com/glunwar@gkunwar1
Copyright 2015. Jyaasa Technologies. Copyright 2016. Jyaasa Technologies.
http://jyaasa.com