SlideShare a Scribd company logo
1 of 29
1
DESIGN PATTERNS :
Strategy Pattern
By
RAVI P. PATKI
Associate Professor (IT)
Hope Foundation’s
International Institute of Information Technology
2
Agenda
 Introduction
 Problem in Software Design
 Definition of Strategy Pattern
 Solution to Problem in terms of
Strategy Pattern
 Implementation of Strategy Pattern
 Advantages
 Disadvantages
 Applications
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
3
 In software engineering, behavioral design patterns are design patterns
that identify common communication patterns between objects and
realize these patterns.
 By doing so, these patterns increase flexibility in carrying out this
communication.
 These design patterns are specifically concerned with communication
between objects.
Behavioral Patterns
 Types of Behavioral Patterns
 Chain of Responsibility
 Command
 Interpreter
 Iterator
 Mediator
 Memento
 Multiple Dispatch
 Observer
 State
 Strategy
 Template Method
 Visitor
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
4
We’ll explore a small subset of the given patterns
in detail, going over the concept, the
problem/context pair, solution, and a little about
implementation
Attempt to focus on patterns that aren’t obvious or
common sense
Patterns to explore
Strategy,
Observer,
State,
Adaptor.
Patterns Explored
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
5
 In Software Engineering, 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.
 Strategy lets the algorithm vary independently from clients that use it.
 Example :
 For instance, a class that performs validation on incoming data may use a
strategy pattern to select a validation algorithm based on the type of data,
the source of the data, user choice, or other discriminating factors.
 These factors are not known for each case until run-time, and may require
radically different validation to be performed.
 The validation strategies, encapsulated separately from the validating object,
may be used by other validating objects in different areas of the system (or
even different systems) without code duplication.
Introduction
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
6
Introduction
 A Strategy defines a set of algorithms that can be used interchangeably.
Example :
 Modes of transportation to an airport is an example of a Strategy.
 Several options exist such as driving one's own car, taking a taxi, an airport shuttle,
a city bus, or a limousine service.
 For some airports, subways and helicopters are also available as a mode of
transportation to the airport.
 Any of these modes of transportation will get a traveler to the airport, and they can
be used interchangeably.
 The traveler must chose the Strategy based on trade-offs between cost,
convenience, and time.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
7
Problem
As always we will learn this pattern by defining a
problem and using strategy pattern to solve it.
Suppose we are building a game “Street Fighter”.
For simplicity assume that a character may have four
moves that is kick, punch, roll and jump.
Every character has kick and punch moves, but roll
and jump are optional.
How would you model your classes?
Suppose initially you use inheritance and abstract
out the common features in a Fighter class and let
other characters subclass Fighter class.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
8
Problem
Fighter class will we have default implementation of normal actions.
Any character with specialized move can override that action in its subclass.
Class diagram would be as follows:
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
9
What are the problems with above design?
Problem
What if a character doesn’t perform jump move?
It still inherits the jump behavior from super class.
Although you can override jump to do nothing in that case
but you may have to do so for many existing classes and
take care of that for future classes too.
This would also make maintenance difficult. So we can’t
use inheritance here.
 this is very bad design option
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
10
Take a look at the following design:
Problem
It’s much cleaner. We took out some actions (which some characters might not
perform) out of Fighter class and made interfaces for them. That way only
characters that are supposed to jump will implement the JumpBehavior.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
11
 What are the problems with above design?
 The main problem with the above design is code reuse.
 Since there is no default implementation of jump and roll behavior we may
have code duplicity.
 You may have to rewrite the same jump behavior over and over in many
subclasses.
 How can we avoid this?
 What if we made JumpBehavior and RollBehavior classes instead of
interface?
 Well then we would have to use multiple inheritance that is not supported
in many languages due to many problems associated with it.
Here strategy pattern comes to our rescue.
We will learn what the strategy pattern is and then apply it to solve our
problem.
Problem
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
12
Definition : Strategy Pattern
 “In computer programming, 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.”
 The Strategy pattern is known as a behavioural pattern - it's used to
manage algorithms, relationships and responsibilities between
objects.
 The definition of Strategy provided in the original Gang of Four book
on Design Patterns states:
“Defines a set of encapsulated algorithms that can be
swapped to carry out a specific behaviour”
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
13
Now, let's take a look at the diagram definition of the Strategy pattern.
 Here we rely on composition instead of inheritance for reuse.
 In the above diagram Context is composed of a Strategy. Instead of
implementing a behavior the Context delegates it to Strategy.
 The context could be anything that would require changing behaviours
 The Strategy is simply implemented as an interface, so that we can swap
ConcreteStrategys in and out without effecting our Context.
Definition : Strategy Pattern
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
14
Definition : Strategy Pattern
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
15
Solution
we apply Strategy Pattern to the Fighter Problem and discuss implementation.
The first step is to identify the behaviors that may vary across different classes
in future and separate them from the rest.
For our example let them be kick and jump behaviors.
To separate these behaviors we will pull both methods out of Fighter class and
create a new set of classes to represent each behavior.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
16
The Fighter class will now delegate its kick and jump behavior instead of using kick
and jump methods defined in the Fighter class or its subclass.
Solution
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
17
After reworking the final class diagram would be:
Solution
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
18
After betterly reworking the final class diagram would be:
Solution
Comparing our design to the definition of strategy pattern encapsulated kick and
jump behaviors are two families of algorithms. And these algorithms are
interchangeable as evident in implementation.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
19
Advantages
 Advantages:
1. A family of algorithms can be defined as a class hierarchy and
can be used interchangeably to alter application behavior
without changing its architecture.
2. By encapsulating the algorithm separately, new algorithms
complying with the same interface can be easily introduced.
3. The application can switch strategies at run-time.
4. Strategy enables the clients to choose the required algorithm,
without using a “switch” statement or a series of “if-else”
statements.
5. Data structures used for implementing the algorithm are
completely encapsulated in Strategy classes. Therefore, the
implementation of an algorithm can be changed without affecting
the Context class.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
20
 Disadvantages:
1. The application must be aware of all the strategies to
select the right one for the right situation.
2. Context and the Strategy classes normally
communicate through the interface specified by the
abstract Strategy base class. Strategy base class
must expose interface for all the required
behaviours, which some concrete Strategy classes
might not implement.
3. In most cases, the application configures the Context
with the required Strategy object. Therefore, the
application needs to create and maintain two objects
in place of one.
Disadvantages:
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
21
Implementation
Steps to implementation
Identify an algorithm (i.e. a behavior) that the
client would prefer to access through a "flex
point".
Specify the signature for that algorithm in an
interface.
Bury the alternative implementation details in
derived classes.
Clients of the algorithm couple themselves to the
interface.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
22
 We are going to create a Strategy interface defining an action and
concrete strategy classes implementing the Strategy interface.
 Context is a class which uses a Strategy.
 StrategyPatternDemo, our demo class, will use Context and strategy
objects to demonstrate change in Context behaviour based on strategy
it deploys or uses
Implementation
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
23
Implementation
Step 1
Create an interface.
Strategy.java
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
24
Implementation
Step 2
Create concrete classes implementing the same interface.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
25
Implementation
Step 3
Create Context Class.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
26
Step 4
Use the Context to see change in behaviour when it changes its Strategy.
Implementation
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
27
Implementation
Step 5
Verify the output.
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
28
Where Would I Use This Pattern?
 The Strategy pattern is to be used where you want to
choose the algorithm to use at runtime.
 A good use of the Strategy pattern would be saving files
in different formats, running various sorting algorithms,
or file compression.
 The Strategy pattern provides a way to define a family
of algorithms, encapsulate each one as an object, and
make them interchangeable.
Applications
Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi
Infotech Park, Hinjawadi, Pune - 411 057
Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
REFERENCE : www.tutorialpoint.com
29
THANK YOU
For further information please feel free to contact
Dr. Ravi Patki
ravip@isquareit.edu.in
Department of Information Technology,
Hope Foundation’s International Institute of Information Technology, I²IT
P-14, Rajiv Gandhi Infotech Park, Hinjawadi, MIDC Phase I
Pune – 411057
Tel +91 20 22933441
www.isquareit.edu.in; info@isquareit.edu.in

More Related Content

What's hot

Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
 
Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design PatternAsif Tayef
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern11prasoon
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
 
The State Design Pattern
The State Design PatternThe State Design Pattern
The State Design PatternJosh Candish
 
Creational pattern
Creational patternCreational pattern
Creational patternHimanshu
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade PatternMudasir Qazi
 
Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design PatternGanesh Kolhe
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton PatternMudasir Qazi
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design PatternShahriar Hyder
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural PatternsSameh Deabes
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsMichael Heron
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.pptVMahesh5
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 

What's hot (20)

Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
Class diagrams
Class diagramsClass diagrams
Class diagrams
 
Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
The State Design Pattern
The State Design PatternThe State Design Pattern
The State Design Pattern
 
Creational pattern
Creational patternCreational pattern
Creational pattern
 
Facade Pattern
Facade PatternFacade Pattern
Facade Pattern
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade Pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design Pattern
 
Class Diagram
Class DiagramClass Diagram
Class Diagram
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design Patterns
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
UML
UMLUML
UML
 

Similar to Strategy Pattern Design Explained in 40 Characters

IRJET- Career Counselling Chatbot
IRJET-  	  Career Counselling ChatbotIRJET-  	  Career Counselling Chatbot
IRJET- Career Counselling ChatbotIRJET Journal
 
Android Based E-Learning Application Class-E
Android Based E-Learning Application Class-EAndroid Based E-Learning Application Class-E
Android Based E-Learning Application Class-EIRJET Journal
 
IRJET- NEEV: An Education Informational Chatbot
IRJET-  	  NEEV: An Education Informational ChatbotIRJET-  	  NEEV: An Education Informational Chatbot
IRJET- NEEV: An Education Informational ChatbotIRJET Journal
 
IRJET- Social Network Message Credibility: An Agent-based Approach
IRJET- Social Network Message Credibility: An Agent-based ApproachIRJET- Social Network Message Credibility: An Agent-based Approach
IRJET- Social Network Message Credibility: An Agent-based ApproachIRJET Journal
 
IRJET - Social Network Message Credibility: An Agent-based Approach
IRJET -  	  Social Network Message Credibility: An Agent-based ApproachIRJET -  	  Social Network Message Credibility: An Agent-based Approach
IRJET - Social Network Message Credibility: An Agent-based ApproachIRJET Journal
 
Emotion Recognition By Textual Tweets Using Machine Learning
Emotion Recognition By Textual Tweets Using Machine LearningEmotion Recognition By Textual Tweets Using Machine Learning
Emotion Recognition By Textual Tweets Using Machine LearningIRJET Journal
 
City i-Tick: The android based mobile application for students’ attendance at...
City i-Tick: The android based mobile application for students’ attendance at...City i-Tick: The android based mobile application for students’ attendance at...
City i-Tick: The android based mobile application for students’ attendance at...journalBEEI
 
IRJET- Placement Portal and Prediction System
IRJET- Placement Portal and Prediction SystemIRJET- Placement Portal and Prediction System
IRJET- Placement Portal and Prediction SystemIRJET Journal
 

Similar to Strategy Pattern Design Explained in 40 Characters (20)

State Pattern: Introduction & Implementation
State Pattern: Introduction  & Implementation State Pattern: Introduction  & Implementation
State Pattern: Introduction & Implementation
 
Introduction To Design Pattern
Introduction To Design PatternIntroduction To Design Pattern
Introduction To Design Pattern
 
Usability Heuristics - Principles & Examples
Usability Heuristics - Principles & ExamplesUsability Heuristics - Principles & Examples
Usability Heuristics - Principles & Examples
 
What is DevOps?
What is DevOps?What is DevOps?
What is DevOps?
 
IRJET- Career Counselling Chatbot
IRJET-  	  Career Counselling ChatbotIRJET-  	  Career Counselling Chatbot
IRJET- Career Counselling Chatbot
 
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
 
Android Based E-Learning Application Class-E
Android Based E-Learning Application Class-EAndroid Based E-Learning Application Class-E
Android Based E-Learning Application Class-E
 
IRJET- NEEV: An Education Informational Chatbot
IRJET-  	  NEEV: An Education Informational ChatbotIRJET-  	  NEEV: An Education Informational Chatbot
IRJET- NEEV: An Education Informational Chatbot
 
Supervised Learning in Cybersecurity
Supervised Learning in CybersecuritySupervised Learning in Cybersecurity
Supervised Learning in Cybersecurity
 
IRJET- Social Network Message Credibility: An Agent-based Approach
IRJET- Social Network Message Credibility: An Agent-based ApproachIRJET- Social Network Message Credibility: An Agent-based Approach
IRJET- Social Network Message Credibility: An Agent-based Approach
 
IRJET - Social Network Message Credibility: An Agent-based Approach
IRJET -  	  Social Network Message Credibility: An Agent-based ApproachIRJET -  	  Social Network Message Credibility: An Agent-based Approach
IRJET - Social Network Message Credibility: An Agent-based Approach
 
Emotion Recognition By Textual Tweets Using Machine Learning
Emotion Recognition By Textual Tweets Using Machine LearningEmotion Recognition By Textual Tweets Using Machine Learning
Emotion Recognition By Textual Tweets Using Machine Learning
 
DAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & ApplicationDAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & Application
 
City i-Tick: The android based mobile application for students’ attendance at...
City i-Tick: The android based mobile application for students’ attendance at...City i-Tick: The android based mobile application for students’ attendance at...
City i-Tick: The android based mobile application for students’ attendance at...
 
Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)
 
Machine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-offMachine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-off
 
IRJET- Placement Portal and Prediction System
IRJET- Placement Portal and Prediction SystemIRJET- Placement Portal and Prediction System
IRJET- Placement Portal and Prediction System
 
Sagar BODAGE's CV
Sagar BODAGE's CVSagar BODAGE's CV
Sagar BODAGE's CV
 
resume_latest-format
resume_latest-formatresume_latest-format
resume_latest-format
 
Machine learning
Machine learningMachine learning
Machine learning
 

More from International Institute of Information Technology (I²IT)

More from International Institute of Information Technology (I²IT) (20)

Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Understanding Natural Language Processing
Understanding Natural Language ProcessingUnderstanding Natural Language Processing
Understanding Natural Language Processing
 
What Is Smart Computing?
What Is Smart Computing?What Is Smart Computing?
What Is Smart Computing?
 
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
 
Writing Skills: Importance of Writing Skills
Writing Skills: Importance of Writing SkillsWriting Skills: Importance of Writing Skills
Writing Skills: Importance of Writing Skills
 
Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself
 
Servlet: A Server-side Technology
Servlet: A Server-side TechnologyServlet: A Server-side Technology
Servlet: A Server-side Technology
 
What Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It WorksWhat Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It Works
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Hypothesis-Testing
Hypothesis-TestingHypothesis-Testing
Hypothesis-Testing
 
Data Science, Big Data, Data Analytics
Data Science, Big Data, Data AnalyticsData Science, Big Data, Data Analytics
Data Science, Big Data, Data Analytics
 
Types of Artificial Intelligence
Types of Artificial Intelligence Types of Artificial Intelligence
Types of Artificial Intelligence
 
Sentiment Analysis in Machine Learning
Sentiment Analysis in  Machine LearningSentiment Analysis in  Machine Learning
Sentiment Analysis in Machine Learning
 
What Is Cloud Computing?
What Is Cloud Computing?What Is Cloud Computing?
What Is Cloud Computing?
 
Importance of Theory of Computations
Importance of Theory of ComputationsImportance of Theory of Computations
Importance of Theory of Computations
 
Java as Object Oriented Programming Language
Java as Object Oriented Programming LanguageJava as Object Oriented Programming Language
Java as Object Oriented Programming Language
 
What Is High Performance-Computing?
What Is High Performance-Computing?What Is High Performance-Computing?
What Is High Performance-Computing?
 
Data Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BIData Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BI
 
AVL Tree Explained
AVL Tree ExplainedAVL Tree Explained
AVL Tree Explained
 
Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19
 

Recently uploaded

VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Roomishabajaj13
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Roomdivyansh0kumar0
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Roomdivyansh0kumar0
 
Call Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on Delivery
Call Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on DeliveryCall Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on Delivery
Call Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on Deliverybabeytanya
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...akbard9823
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Roomgirls4nights
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607dollysharma2066
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一Fs
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一Fs
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITMgdsc13
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girladitipandeya
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Lucknow
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一Fs
 

Recently uploaded (20)

VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
 
Call Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on Delivery
Call Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on DeliveryCall Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on Delivery
Call Girls In Mumbai Central Mumbai ❤️ 9920874524 👈 Cash on Delivery
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
Sushant Golf City / best call girls in Lucknow | Service-oriented sexy call g...
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
 
sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-
sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-
sasti delhi Call Girls in munirka 🔝 9953056974 🔝 escort Service-
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
 
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
 
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
定制(AUT毕业证书)新西兰奥克兰理工大学毕业证成绩单原版一比一
 
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Ishita 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Ishita 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
定制(UAL学位证)英国伦敦艺术大学毕业证成绩单原版一比一
 
Git and Github workshop GDSC MLRITM
Git and Github  workshop GDSC MLRITMGit and Github  workshop GDSC MLRITM
Git and Github workshop GDSC MLRITM
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
 
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja VipCall Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
Call Girls Service Adil Nagar 7001305949 Need escorts Service Pooja Vip
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girls
 
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
定制(Lincoln毕业证书)新西兰林肯大学毕业证成绩单原版一比一
 

Strategy Pattern Design Explained in 40 Characters

  • 1. 1 DESIGN PATTERNS : Strategy Pattern By RAVI P. PATKI Associate Professor (IT) Hope Foundation’s International Institute of Information Technology
  • 2. 2 Agenda  Introduction  Problem in Software Design  Definition of Strategy Pattern  Solution to Problem in terms of Strategy Pattern  Implementation of Strategy Pattern  Advantages  Disadvantages  Applications Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 3. 3  In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns.  By doing so, these patterns increase flexibility in carrying out this communication.  These design patterns are specifically concerned with communication between objects. Behavioral Patterns  Types of Behavioral Patterns  Chain of Responsibility  Command  Interpreter  Iterator  Mediator  Memento  Multiple Dispatch  Observer  State  Strategy  Template Method  Visitor Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 4. 4 We’ll explore a small subset of the given patterns in detail, going over the concept, the problem/context pair, solution, and a little about implementation Attempt to focus on patterns that aren’t obvious or common sense Patterns to explore Strategy, Observer, State, Adaptor. Patterns Explored Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 5. 5  In Software Engineering, 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.  Strategy lets the algorithm vary independently from clients that use it.  Example :  For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, or other discriminating factors.  These factors are not known for each case until run-time, and may require radically different validation to be performed.  The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication. Introduction Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 6. 6 Introduction  A Strategy defines a set of algorithms that can be used interchangeably. Example :  Modes of transportation to an airport is an example of a Strategy.  Several options exist such as driving one's own car, taking a taxi, an airport shuttle, a city bus, or a limousine service.  For some airports, subways and helicopters are also available as a mode of transportation to the airport.  Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably.  The traveler must chose the Strategy based on trade-offs between cost, convenience, and time. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 7. 7 Problem As always we will learn this pattern by defining a problem and using strategy pattern to solve it. Suppose we are building a game “Street Fighter”. For simplicity assume that a character may have four moves that is kick, punch, roll and jump. Every character has kick and punch moves, but roll and jump are optional. How would you model your classes? Suppose initially you use inheritance and abstract out the common features in a Fighter class and let other characters subclass Fighter class. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 8. 8 Problem Fighter class will we have default implementation of normal actions. Any character with specialized move can override that action in its subclass. Class diagram would be as follows: Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 9. 9 What are the problems with above design? Problem What if a character doesn’t perform jump move? It still inherits the jump behavior from super class. Although you can override jump to do nothing in that case but you may have to do so for many existing classes and take care of that for future classes too. This would also make maintenance difficult. So we can’t use inheritance here.  this is very bad design option Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 10. 10 Take a look at the following design: Problem It’s much cleaner. We took out some actions (which some characters might not perform) out of Fighter class and made interfaces for them. That way only characters that are supposed to jump will implement the JumpBehavior. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 11. 11  What are the problems with above design?  The main problem with the above design is code reuse.  Since there is no default implementation of jump and roll behavior we may have code duplicity.  You may have to rewrite the same jump behavior over and over in many subclasses.  How can we avoid this?  What if we made JumpBehavior and RollBehavior classes instead of interface?  Well then we would have to use multiple inheritance that is not supported in many languages due to many problems associated with it. Here strategy pattern comes to our rescue. We will learn what the strategy pattern is and then apply it to solve our problem. Problem Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 12. 12 Definition : Strategy Pattern  “In computer programming, 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.”  The Strategy pattern is known as a behavioural pattern - it's used to manage algorithms, relationships and responsibilities between objects.  The definition of Strategy provided in the original Gang of Four book on Design Patterns states: “Defines a set of encapsulated algorithms that can be swapped to carry out a specific behaviour” Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 13. 13 Now, let's take a look at the diagram definition of the Strategy pattern.  Here we rely on composition instead of inheritance for reuse.  In the above diagram Context is composed of a Strategy. Instead of implementing a behavior the Context delegates it to Strategy.  The context could be anything that would require changing behaviours  The Strategy is simply implemented as an interface, so that we can swap ConcreteStrategys in and out without effecting our Context. Definition : Strategy Pattern Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 14. 14 Definition : Strategy Pattern Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 15. 15 Solution we apply Strategy Pattern to the Fighter Problem and discuss implementation. The first step is to identify the behaviors that may vary across different classes in future and separate them from the rest. For our example let them be kick and jump behaviors. To separate these behaviors we will pull both methods out of Fighter class and create a new set of classes to represent each behavior. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 16. 16 The Fighter class will now delegate its kick and jump behavior instead of using kick and jump methods defined in the Fighter class or its subclass. Solution Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 17. 17 After reworking the final class diagram would be: Solution Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 18. 18 After betterly reworking the final class diagram would be: Solution Comparing our design to the definition of strategy pattern encapsulated kick and jump behaviors are two families of algorithms. And these algorithms are interchangeable as evident in implementation. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 19. 19 Advantages  Advantages: 1. A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture. 2. By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced. 3. The application can switch strategies at run-time. 4. Strategy enables the clients to choose the required algorithm, without using a “switch” statement or a series of “if-else” statements. 5. Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 20. 20  Disadvantages: 1. The application must be aware of all the strategies to select the right one for the right situation. 2. Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviours, which some concrete Strategy classes might not implement. 3. In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one. Disadvantages: Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 21. 21 Implementation Steps to implementation Identify an algorithm (i.e. a behavior) that the client would prefer to access through a "flex point". Specify the signature for that algorithm in an interface. Bury the alternative implementation details in derived classes. Clients of the algorithm couple themselves to the interface. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 22. 22  We are going to create a Strategy interface defining an action and concrete strategy classes implementing the Strategy interface.  Context is a class which uses a Strategy.  StrategyPatternDemo, our demo class, will use Context and strategy objects to demonstrate change in Context behaviour based on strategy it deploys or uses Implementation Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 23. 23 Implementation Step 1 Create an interface. Strategy.java Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 24. 24 Implementation Step 2 Create concrete classes implementing the same interface. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 25. 25 Implementation Step 3 Create Context Class. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 26. 26 Step 4 Use the Context to see change in behaviour when it changes its Strategy. Implementation Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 27. 27 Implementation Step 5 Verify the output. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in
  • 28. 28 Where Would I Use This Pattern?  The Strategy pattern is to be used where you want to choose the algorithm to use at runtime.  A good use of the Strategy pattern would be saving files in different formats, running various sorting algorithms, or file compression.  The Strategy pattern provides a way to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. Applications Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in REFERENCE : www.tutorialpoint.com
  • 29. 29 THANK YOU For further information please feel free to contact Dr. Ravi Patki ravip@isquareit.edu.in Department of Information Technology, Hope Foundation’s International Institute of Information Technology, I²IT P-14, Rajiv Gandhi Infotech Park, Hinjawadi, MIDC Phase I Pune – 411057 Tel +91 20 22933441 www.isquareit.edu.in; info@isquareit.edu.in