DESIGN PATTERNS: Strategy Pattern

1
 
 
DESIGN PATTERNS :
DESIGN PATTERNS :
Strategy Pattern
Strategy Pattern
 
 
By
RAVI P. PATKI
RAVI P. PATKI
Associate Professor (IT)
Associate Professor (IT)
Hope Foundation’s
Hope Foundation’s
International Institute of Information Technology
International Institute of Information Technology
2
Agenda
Agenda
Introduction
Introduction
Problem in Software Design
Problem in Software Design
 Definition of Strategy Pattern
 Definition of Strategy Pattern
 Solution to Problem in terms of
 Solution to Problem in terms of
Strategy Pattern
Strategy Pattern
 Implementation of Strategy Pattern
 Implementation of Strategy Pattern
Advantages
Advantages
Disadvantages
Disadvantages
Applications
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 - 
 ; Email - 
info@isquareit.edu.inwww.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
Behavioral Patterns
T
y
p
e
s
 
o
f
 
B
e
h
a
v
i
o
r
a
l
 
P
a
t
t
e
r
n
s
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
S
t
r
a
t
e
g
y
,
O
b
s
e
r
v
e
r
,
S
t
a
t
e
,
A
d
a
p
t
o
r
.
Patterns Explored
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
I
n
 
S
o
f
t
w
a
r
e
 
E
n
g
i
n
e
e
r
i
n
g
,
 
t
h
e
 
s
t
r
a
t
e
g
y
 
p
a
t
t
e
r
n
 
(
a
l
s
o
 
k
n
o
w
n
 
a
s
 
t
h
e
 
p
o
l
i
c
y
p
a
t
t
e
r
n
)
 
i
s
 
a
 
s
o
f
t
w
a
r
e
 
d
e
s
i
g
n
 
p
a
t
t
e
r
n
 
t
h
a
t
 
e
n
a
b
l
e
s
 
a
n
 
a
l
g
o
r
i
t
h
m
'
s
 
b
e
h
a
v
i
o
r
 
t
o
b
e
 
s
e
l
e
c
t
e
d
 
a
t
 
r
u
n
t
i
m
e
.
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.
E
x
a
m
p
l
e
 
:
F
o
r
 
i
n
s
t
a
n
c
e
,
 
a
 
c
l
a
s
s
 
t
h
a
t
 
p
e
r
f
o
r
m
s
 
v
a
l
i
d
a
t
i
o
n
 
o
n
 
i
n
c
o
m
i
n
g
 
d
a
t
a
 
m
a
y
 
u
s
e
 
a
s
t
r
a
t
e
g
y
 
p
a
t
t
e
r
n
 
t
o
 
s
e
l
e
c
t
 
a
 
v
a
l
i
d
a
t
i
o
n
 
a
l
g
o
r
i
t
h
m
 
b
a
s
e
d
 
o
n
 
t
h
e
 
t
y
p
e
 
o
f
 
d
a
t
a
,
t
h
e
 
s
o
u
r
c
e
 
o
f
 
t
h
e
 
d
a
t
a
,
 
u
s
e
r
 
c
h
o
i
c
e
,
 
o
r
 
o
t
h
e
r
 
d
i
s
c
r
i
m
i
n
a
t
i
n
g
 
f
a
c
t
o
r
s
.
T
h
e
s
e
 
f
a
c
t
o
r
s
 
a
r
e
 
n
o
t
 
k
n
o
w
n
 
f
o
r
 
e
a
c
h
 
c
a
s
e
 
u
n
t
i
l
 
r
u
n
-
t
i
m
e
,
 
a
n
d
 
m
a
y
 
r
e
q
u
i
r
e
r
a
d
i
c
a
l
l
y
 
d
i
f
f
e
r
e
n
t
 
v
a
l
i
d
a
t
i
o
n
 
t
o
 
b
e
 
p
e
r
f
o
r
m
e
d
.
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
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
Introduction
A Strategy defines a set of algorithms that can be used interchangeably.
E
x
a
m
p
l
e
 
:
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
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?
S
u
p
p
o
s
e
 
i
n
i
t
i
a
l
l
y
 
y
o
u
 
u
s
e
 
i
n
h
e
r
i
t
a
n
c
e
 
a
n
d
 
a
b
s
t
r
a
c
t
o
u
t
 
t
h
e
 
c
o
m
m
o
n
 
f
e
a
t
u
r
e
s
 
i
n
 
a
 
F
i
g
h
t
e
r
 
c
l
a
s
s
 
a
n
d
 
l
e
t
o
t
h
e
r
 
c
h
a
r
a
c
t
e
r
s
 
s
u
b
c
l
a
s
s
 
F
i
g
h
t
e
r
 
c
l
a
s
s
.
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
Problem
F
i
g
h
t
e
r
 
c
l
a
s
s
 
w
i
l
l
 
w
e
 
h
a
v
e
 
d
e
f
a
u
l
t
 
i
m
p
l
e
m
e
n
t
a
t
i
o
n
 
o
f
 
n
o
r
m
a
l
 
a
c
t
i
o
n
s
.
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
W
h
a
t
 
a
r
e
 
t
h
e
 
p
r
o
b
l
e
m
s
 
w
i
t
h
 
a
b
o
v
e
 
d
e
s
i
g
n
?
Problem
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
Problem
I
t
s
 
m
u
c
h
 
c
l
e
a
n
e
r
.
 
W
e
 
t
o
o
k
 
o
u
t
 
s
o
m
e
 
a
c
t
i
o
n
s
 
(
w
h
i
c
h
 
s
o
m
e
 
c
h
a
r
a
c
t
e
r
s
 
m
i
g
h
t
 
n
o
t
p
e
r
f
o
r
m
)
 
o
u
t
 
o
f
 
F
i
g
h
t
e
r
 
c
l
a
s
s
 
a
n
d
 
m
a
d
e
 
i
n
t
e
r
f
a
c
e
s
 
f
o
r
 
t
h
e
m
.
 
T
h
a
t
 
w
a
y
 
o
n
l
y
c
h
a
r
a
c
t
e
r
s
 
t
h
a
t
 
a
r
e
 
s
u
p
p
o
s
e
d
 
t
o
 
j
u
m
p
 
w
i
l
l
 
i
m
p
l
e
m
e
n
t
 
t
h
e
 
J
u
m
p
B
e
h
a
v
i
o
r
.
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
W
h
a
t
 
a
r
e
 
t
h
e
 
p
r
o
b
l
e
m
s
 
w
i
t
h
 
a
b
o
v
e
 
d
e
s
i
g
n
?
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.
H
o
w
 
c
a
n
 
w
e
 
a
v
o
i
d
 
t
h
i
s
?
W
h
a
t
 
i
f
 
w
e
 
m
a
d
e
 
J
u
m
p
B
e
h
a
v
i
o
r
 
a
n
d
 
R
o
l
l
B
e
h
a
v
i
o
r
 
c
l
a
s
s
e
s
 
i
n
s
t
e
a
d
 
o
f
i
n
t
e
r
f
a
c
e
?
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
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
Definition : Strategy Pattern
I
n
 
c
o
m
p
u
t
e
r
 
p
r
o
g
r
a
m
m
i
n
g
,
 
t
h
e
 
s
t
r
a
t
e
g
y
 
p
a
t
t
e
r
n
 
(
a
l
s
o
 
k
n
o
w
n
 
a
s
t
h
e
 
p
o
l
i
c
y
 
p
a
t
t
e
r
n
)
 
i
s
 
a
 
s
o
f
t
w
a
r
e
 
d
e
s
i
g
n
 
p
a
t
t
e
r
n
 
t
h
a
t
 
e
n
a
b
l
e
s
a
n
 
a
l
g
o
r
i
t
h
m
s
 
b
e
h
a
v
i
o
r
 
t
o
 
b
e
 
s
e
l
e
c
t
e
d
 
a
t
 
r
u
n
t
i
m
e
.
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:
D
e
f
i
n
e
s
 
a
 
s
e
t
 
o
f
 
e
n
c
a
p
s
u
l
a
t
e
d
 
a
l
g
o
r
i
t
h
m
s
 
t
h
a
t
 
c
a
n
 
b
e
s
w
a
p
p
e
d
 
t
o
 
c
a
r
r
y
 
o
u
t
 
a
 
s
p
e
c
i
f
i
c
 
b
e
h
a
v
i
o
u
r
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.
I
n
 
t
h
e
 
a
b
o
v
e
 
d
i
a
g
r
a
m
 
C
o
n
t
e
x
t
 
i
s
 
c
o
m
p
o
s
e
d
 
o
f
 
a
 
S
t
r
a
t
e
g
y
.
 
I
n
s
t
e
a
d
 
o
f
i
m
p
l
e
m
e
n
t
i
n
g
 
a
 
b
e
h
a
v
i
o
r
 
t
h
e
 
C
o
n
t
e
x
t
 
d
e
l
e
g
a
t
e
s
 
i
t
 
t
o
 
S
t
r
a
t
e
g
y
.
The context could be anything that would require changing behaviours
T
h
e
 
S
t
r
a
t
e
g
y
 
i
s
 
s
i
m
p
l
y
 
i
m
p
l
e
m
e
n
t
e
d
 
a
s
 
a
n
 
i
n
t
e
r
f
a
c
e
,
 
s
o
 
t
h
a
t
 
w
e
 
c
a
n
 
s
w
a
p
C
o
n
c
r
e
t
e
S
t
r
a
t
e
g
y
s
 
i
n
 
a
n
d
 
o
u
t
 
w
i
t
h
o
u
t
 
e
f
f
e
c
t
i
n
g
 
o
u
r
 
C
o
n
t
e
x
t
.
Definition : Strategy Pattern
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
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
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.
T
o
 
s
e
p
a
r
a
t
e
 
t
h
e
s
e
 
b
e
h
a
v
i
o
r
s
 
w
e
 
w
i
l
l
 
p
u
l
l
 
b
o
t
h
 
m
e
t
h
o
d
s
 
o
u
t
 
o
f
 
F
i
g
h
t
e
r
 
c
l
a
s
s
 
a
n
d
c
r
e
a
t
e
 
a
 
n
e
w
 
s
e
t
 
o
f
 
c
l
a
s
s
e
s
 
t
o
 
r
e
p
r
e
s
e
n
t
 
e
a
c
h
 
b
e
h
a
v
i
o
r
.
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
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
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
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
A
d
v
a
n
t
a
g
e
s
:
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
D
i
s
a
d
v
a
n
t
a
g
e
s
:
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:
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
Implementation
S
t
e
p
s
 
t
o
 
i
m
p
l
e
m
e
n
t
a
t
i
o
n
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.
S
trategyPatternDemo
, our demo class, will use 
Context
 and strategy
objects to demonstrate change in Context behaviour based on strategy
it deploys or uses
Implementation
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
Implementation
S
t
e
p
 
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
Implementation
S
t
e
p
 
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
Implementation
S
t
e
p
 
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
S
t
e
p
 
4
Use the 
Context
 to see change in behaviour when it changes its 
Strategy
.
Implementation
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
Implementation
S
t
e
p
 
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
W
h
e
r
e
 
W
o
u
l
d
 
I
 
U
s
e
 
T
h
i
s
 
P
a
t
t
e
r
n
?
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
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
R
E
F
E
R
E
N
C
E
 
:
 
w
w
w
.
t
u
t
o
r
i
a
l
p
o
i
n
t
.
c
o
m
29
THANK YOU
THANK YOU
For further information please feel free to contact
For further information please feel free to contact
Dr. Ravi Patki
Dr. Ravi Patki
ravip@isquareit.edu.in
ravip@isquareit.edu.in
Department of Information Technology,
Department of Information Technology,
Hope Foundation’s International Institute of Information Technology, I²IT
Hope Foundation’s International Institute of Information Technology, I²IT
P-14, Rajiv Gandhi Infotech Park, Hinjawadi, MIDC Phase I
P-14, Rajiv Gandhi Infotech Park, Hinjawadi, MIDC Phase I
Pune – 411057
Pune – 411057
Tel +91 20 22933441
Tel +91 20 22933441
www.isquareit.edu.in
www.isquareit.edu.in
; 
; 
info@isquareit.edu.in
info@isquareit.edu.in
Slide Note
Embed
Share

Software engineering design pattern focusing on algorithm behavior selection at runtime, allowing interchangeable algorithms within a family, enhancing flexibility and reusability across systems.

  • Strategy Pattern
  • Software Design
  • Behavioral Patterns
  • Implementation
  • Advantages

Uploaded on Feb 15, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. DESIGN PATTERNS : Strategy Pattern By RAVI P. PATKI Associate Professor (IT) Hope Foundation s International Institute of Information Technology 1

  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 2 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. Behavioral Patterns 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. Types of Behavioral Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Multiple Dispatch Observer State Strategy Template Method Visitor 3 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. Patterns Explored 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. 4 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. Introduction 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. 5 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. 6 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. 7 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: 8 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. Problem What are the problems with above design? 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 9 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. Problem Take a look at the following design: 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. 10 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. Problem 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. 11 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 12 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. Definition : Strategy Pattern 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. 13 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 14 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. 15 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. Solution 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. 16 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. Solution After reworking the final class diagram would be: 17 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. Solution After betterly reworking the final class diagram would be: 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. 18 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. 19 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: 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. 20 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. 21 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. Implementation 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 22 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 23 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. 24 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. 25 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. Implementation Step 4 Use the Context to see change in behaviour when it changes its Strategy. 26 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. 27 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. Applications 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. REFERENCE : www.tutorialpoint.com 28 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

  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 29

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#