SlideShare a Scribd company logo
1 of 16
Download to read offline
http://jy
aasa.comCopyright 2016. Jyaasa Technologies.
Strategy Pattern
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.com
Ganesh Kunwar
Sr. Software Engineer
Jyaasa Technologies
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.com
What is Strategy Pattern?
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.com
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.com
Ktm Butwal
Taxi Plan Taxi
Ktm.
Airport
Bhairahawa
Airport
Ktm Butwal
Taxi Bus
Kalanki
Ktm Butwal
Taxi Bus BusKalanki
Chitwan
Strategy 1
Strategy 2
Strategy 3
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.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://jy
aasa.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://jy
aasa.com
Where does the discount logic reside?
Inside ShoppingMall class
Or
Outside ShoppingMall class
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.com
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.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://jy
aasa.com
Separate discount logic from ShoppingMall class
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.com
Context Strategy
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.com
Copyright 2016. Jyaasa Technologies.
http://jy
aasa.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://jy
aasa.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

More Related Content

What's hot

What's hot (20)

Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design Pattern
 
softCours design pattern m youssfi partie 9 creation des objets abstract fact...
softCours design pattern m youssfi partie 9 creation des objets abstract fact...softCours design pattern m youssfi partie 9 creation des objets abstract fact...
softCours design pattern m youssfi partie 9 creation des objets abstract fact...
 
Traitement distribue en BIg Data - KAFKA Broker and Kafka Streams
Traitement distribue en BIg Data - KAFKA Broker and Kafka StreamsTraitement distribue en BIg Data - KAFKA Broker and Kafka Streams
Traitement distribue en BIg Data - KAFKA Broker and Kafka Streams
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Design pattern
Design patternDesign pattern
Design pattern
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Cours design pattern m youssfi partie 4 composite
Cours design pattern m youssfi partie 4 compositeCours design pattern m youssfi partie 4 composite
Cours design pattern m youssfi partie 4 composite
 
Cours design pattern m youssfi partie 5 adapter
Cours design pattern m youssfi partie 5 adapterCours design pattern m youssfi partie 5 adapter
Cours design pattern m youssfi partie 5 adapter
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
 
Software Engineering - chp4- design patterns
Software Engineering - chp4- design patternsSoftware Engineering - chp4- design patterns
Software Engineering - chp4- design patterns
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Java - programmation concurrente
Java - programmation concurrenteJava - programmation concurrente
Java - programmation concurrente
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 

Similar to Strategy Design Pattern

Similar to Strategy Design Pattern (20)

Welcome your new digital team member. meet your AI infused agile agent
Welcome your new digital team member. meet your AI infused agile agentWelcome your new digital team member. meet your AI infused agile agent
Welcome your new digital team member. meet your AI infused agile agent
 
Introduction to GCP (Google Cloud Platform)
Introduction to GCP (Google Cloud Platform)Introduction to GCP (Google Cloud Platform)
Introduction to GCP (Google Cloud Platform)
 
Advances in SAFe DevOps
Advances in SAFe DevOpsAdvances in SAFe DevOps
Advances in SAFe DevOps
 
Webinar: Select the Right Web CMS in 2016
Webinar: Select the Right Web CMS in 2016Webinar: Select the Right Web CMS in 2016
Webinar: Select the Right Web CMS in 2016
 
Smart Agile Tools 2020: AI taking it to the future state
Smart Agile Tools 2020: AI taking it to the future stateSmart Agile Tools 2020: AI taking it to the future state
Smart Agile Tools 2020: AI taking it to the future state
 
Machine Learning: Decision Trees
Machine Learning: Decision TreesMachine Learning: Decision Trees
Machine Learning: Decision Trees
 
Industrial Transformation
Industrial TransformationIndustrial Transformation
Industrial Transformation
 
Analytics Store for Hybrid Cloud
Analytics Store for Hybrid CloudAnalytics Store for Hybrid Cloud
Analytics Store for Hybrid Cloud
 
Machine learning & computer vision
Machine learning & computer visionMachine learning & computer vision
Machine learning & computer vision
 
Machine Learning in Computer Vision
Machine Learning in Computer VisionMachine Learning in Computer Vision
Machine Learning in Computer Vision
 
Meet Preston, and Explore Your Digital Twin in Virtual Reality (GPSTEC321) - ...
Meet Preston, and Explore Your Digital Twin in Virtual Reality (GPSTEC321) - ...Meet Preston, and Explore Your Digital Twin in Virtual Reality (GPSTEC321) - ...
Meet Preston, and Explore Your Digital Twin in Virtual Reality (GPSTEC321) - ...
 
Cwin16 tls-partner-sas new-open_analytics_platform
Cwin16 tls-partner-sas new-open_analytics_platformCwin16 tls-partner-sas new-open_analytics_platform
Cwin16 tls-partner-sas new-open_analytics_platform
 
What, Why and How of Governance in RPA
What, Why and How of Governance  in RPAWhat, Why and How of Governance  in RPA
What, Why and How of Governance in RPA
 
The Future of AI on AWS
The Future of AI on AWSThe Future of AI on AWS
The Future of AI on AWS
 
Just add Imagination
Just add ImaginationJust add Imagination
Just add Imagination
 
MLops workshop AWS
MLops workshop AWSMLops workshop AWS
MLops workshop AWS
 
IRJET- Supply Chain Network Design for Plant Location and Selection of Capaci...
IRJET- Supply Chain Network Design for Plant Location and Selection of Capaci...IRJET- Supply Chain Network Design for Plant Location and Selection of Capaci...
IRJET- Supply Chain Network Design for Plant Location and Selection of Capaci...
 
Microservices: The Building Blocks for a Digital Future
Microservices: The Building Blocks for a Digital FutureMicroservices: The Building Blocks for a Digital Future
Microservices: The Building Blocks for a Digital Future
 
Trends and Best Practices for Implementing SaaS for Your Business
Trends and Best Practices for Implementing SaaS for Your BusinessTrends and Best Practices for Implementing SaaS for Your Business
Trends and Best Practices for Implementing SaaS for Your Business
 
Grassroots Kanban – An Evolutionary Approach To Change
Grassroots Kanban – An Evolutionary Approach To ChangeGrassroots Kanban – An Evolutionary Approach To Change
Grassroots Kanban – An Evolutionary Approach To Change
 

More from Jyaasa Technologies

More from Jyaasa Technologies (20)

Incident management with jira
Incident management with jiraIncident management with jira
Incident management with jira
 
Extreme programming practices ( xp )
Extreme programming practices ( xp ) Extreme programming practices ( xp )
Extreme programming practices ( xp )
 
The myth of 'real javascript developer'
The myth of 'real javascript developer'The myth of 'real javascript developer'
The myth of 'real javascript developer'
 
Microservices
MicroservicesMicroservices
Microservices
 
Facade pattern in rails
Facade pattern in railsFacade pattern in rails
Facade pattern in rails
 
Scrum ceromonies
Scrum ceromoniesScrum ceromonies
Scrum ceromonies
 
An introduction to bitcoin
An introduction to bitcoinAn introduction to bitcoin
An introduction to bitcoin
 
Tor network
Tor networkTor network
Tor network
 
Collective ownership in agile teams
Collective ownership in agile teamsCollective ownership in agile teams
Collective ownership in agile teams
 
Push notification
Push notificationPush notification
Push notification
 
The Design Thinking Process
The Design Thinking ProcessThe Design Thinking Process
The Design Thinking Process
 
User story
User storyUser story
User story
 
Design sprint
Design sprintDesign sprint
Design sprint
 
Data Flow Diagram
Data Flow DiagramData Flow Diagram
Data Flow Diagram
 
OKRs and Actions Overview
OKRs and Actions OverviewOKRs and Actions Overview
OKRs and Actions Overview
 
Vue.js
Vue.jsVue.js
Vue.js
 
Active record in rails 5
Active record in rails 5Active record in rails 5
Active record in rails 5
 
Design Patern::Adaptor pattern
Design Patern::Adaptor patternDesign Patern::Adaptor pattern
Design Patern::Adaptor pattern
 
Association in rails
Association in railsAssociation in rails
Association in rails
 
Web design layout pattern
Web design layout patternWeb design layout pattern
Web design layout pattern
 

Recently uploaded

Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
drm1699
 

Recently uploaded (20)

Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
Abortion Clinic In Polokwane ](+27832195400*)[ 🏥 Safe Abortion Pills in Polok...
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
^Clinic ^%[+27788225528*Abortion Pills For Sale In soweto
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
What is a Recruitment Management Software?
What is a Recruitment Management Software?What is a Recruitment Management Software?
What is a Recruitment Management Software?
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 

Strategy Design Pattern

  • 1. http://jy aasa.comCopyright 2016. Jyaasa Technologies. Strategy Pattern
  • 2. Copyright 2016. Jyaasa Technologies. http://jy aasa.com Ganesh Kunwar Sr. Software Engineer Jyaasa Technologies
  • 3. Copyright 2016. Jyaasa Technologies. http://jy aasa.com What is Strategy Pattern?
  • 4. Copyright 2016. Jyaasa Technologies. http://jy aasa.com
  • 5. Copyright 2016. Jyaasa Technologies. http://jy aasa.com Ktm Butwal Taxi Plan Taxi Ktm. Airport Bhairahawa Airport Ktm Butwal Taxi Bus Kalanki Ktm Butwal Taxi Bus BusKalanki Chitwan Strategy 1 Strategy 2 Strategy 3
  • 6. Copyright 2016. Jyaasa Technologies. http://jy aasa.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.
  • 7. Copyright 2016. Jyaasa Technologies. http://jy aasa.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%)
  • 8. Copyright 2016. Jyaasa Technologies. http://jy aasa.com Where does the discount logic reside? Inside ShoppingMall class Or Outside ShoppingMall class
  • 9. Copyright 2016. Jyaasa Technologies. http://jy aasa.com
  • 10. Copyright 2016. Jyaasa Technologies. http://jy aasa.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
  • 11. Copyright 2016. Jyaasa Technologies. http://jy aasa.com Separate discount logic from ShoppingMall class
  • 12. Copyright 2016. Jyaasa Technologies. http://jy aasa.com Context Strategy
  • 13. Copyright 2016. Jyaasa Technologies. http://jy aasa.com
  • 14. Copyright 2016. Jyaasa Technologies. http://jy aasa.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!"
  • 15. Copyright 2016. Jyaasa Technologies. http://jy aasa.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
  • 16. Thank you! Lets Discuss github.com/glunwar @gkunwar1 Copyright 2015. Jyaasa Technologies.Copyright 2016. Jyaasa Technologies. http://jyaasa.com