Strategy
Design Pattern
Alia Hamwi
is a behavioral 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.”
2
3
Navigation app using map:
▰ By car,
▰ By Bus,
▰ Walking.
4
Problem
Navigation app using map:
▰ By car,
▰ By Bus,
▰ Walking.
5
Solution
Pros
▰ swap algorithms used inside an object at runtime.
▰ isolate the implementation details of an algorithm
from the code that uses it.
Pros vs. Cons
6
Cons
▰ If you only have a couple of algorithms and they rarely change,
there’s no real reason to overcomplicate the program with new
classes and interfaces that come along with the pattern.
▰ Clients must be aware of the differences between strategies to be
able to select a proper one.
▰ A lot of modern programming languages lets you implement
different versions of an algorithm inside a set of anonymous
functions. Then you could use these functions exactly as you’d have
used the strategy objects, but without bloating your code with extra
classes and interfaces.
Pros vs. Cons
7
▰ you have a lot of similar classes
that only differ in the way they
execute some behavior.
▰ Use the pattern to isolate the
business logic of a class from the
implementation details of
algorithms.
8
When to use Strategy:
▰ Sorting.
▰ Validation.
▰ Games.
▰ Storing information.
▰ Outputting: We need to output X as a
plain string, but later may be a CSV,
XML, JSON, etc.
9
Real World Example:
▰ Command and Strategy may look similar because you can use both to
parameterize an object with some action. However, they have very
different intents.
▰ State can be considered as an extension of Strategy.. Strategy makes
these objects completely independent and unaware of each other.
However, State doesn’t restrict dependencies between concrete states,
letting them alter the state of the context at will.
10
Strategy with other Design Pattern
11
Code Example:
Template
Design Pattern
is a behavioral design pattern that
▰ defines the skeleton of an algorithm in the
superclass.
▰ lets subclasses override specific steps of
the algorithm without changing its
structure.
13
14
data mining application that analyzes corporate
documents:
▰ PDF,
▰ CSV,
▰ DOC.
15
Problem
data mining application
▰ PDF,
▰ CSV,
▰ DOC.
16
Solution
Pros
▰ let clients override only certain parts of a large
algorithm, making them less affected by changes
that happen to other parts of the algorithm.
▰ You can reduce the duplicate code into a
superclass.
Pros vs. Cons
17
Cons
▰ Some clients may be limited by the provided skeleton of an
algorithm.
Pros vs. Cons
18
▰ you have several classes that
contain almost identical
algorithms with some minor
differences.
▰ let clients extend only particular
steps of an algorithm, but not the
whole algorithm or its structure.
19
When to use Template:
▰ ASP.NET page life cycle: Init, Load,
Validate, Prerender, Render.
▰ Parser.
20
Real World Example:
▰ Template and strategy.
21
Template with other Design Pattern
.
22
Code Example:

Design Pattern (Strategy & Template)

  • 1.
  • 2.
    is a behavioraldesign 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.” 2
  • 3.
  • 4.
    Navigation app usingmap: ▰ By car, ▰ By Bus, ▰ Walking. 4 Problem
  • 5.
    Navigation app usingmap: ▰ By car, ▰ By Bus, ▰ Walking. 5 Solution
  • 6.
    Pros ▰ swap algorithmsused inside an object at runtime. ▰ isolate the implementation details of an algorithm from the code that uses it. Pros vs. Cons 6
  • 7.
    Cons ▰ If youonly have a couple of algorithms and they rarely change, there’s no real reason to overcomplicate the program with new classes and interfaces that come along with the pattern. ▰ Clients must be aware of the differences between strategies to be able to select a proper one. ▰ A lot of modern programming languages lets you implement different versions of an algorithm inside a set of anonymous functions. Then you could use these functions exactly as you’d have used the strategy objects, but without bloating your code with extra classes and interfaces. Pros vs. Cons 7
  • 8.
    ▰ you havea lot of similar classes that only differ in the way they execute some behavior. ▰ Use the pattern to isolate the business logic of a class from the implementation details of algorithms. 8 When to use Strategy:
  • 9.
    ▰ Sorting. ▰ Validation. ▰Games. ▰ Storing information. ▰ Outputting: We need to output X as a plain string, but later may be a CSV, XML, JSON, etc. 9 Real World Example:
  • 10.
    ▰ Command andStrategy may look similar because you can use both to parameterize an object with some action. However, they have very different intents. ▰ State can be considered as an extension of Strategy.. Strategy makes these objects completely independent and unaware of each other. However, State doesn’t restrict dependencies between concrete states, letting them alter the state of the context at will. 10 Strategy with other Design Pattern
  • 11.
  • 12.
  • 13.
    is a behavioraldesign pattern that ▰ defines the skeleton of an algorithm in the superclass. ▰ lets subclasses override specific steps of the algorithm without changing its structure. 13
  • 14.
  • 15.
    data mining applicationthat analyzes corporate documents: ▰ PDF, ▰ CSV, ▰ DOC. 15 Problem
  • 16.
    data mining application ▰PDF, ▰ CSV, ▰ DOC. 16 Solution
  • 17.
    Pros ▰ let clientsoverride only certain parts of a large algorithm, making them less affected by changes that happen to other parts of the algorithm. ▰ You can reduce the duplicate code into a superclass. Pros vs. Cons 17
  • 18.
    Cons ▰ Some clientsmay be limited by the provided skeleton of an algorithm. Pros vs. Cons 18
  • 19.
    ▰ you haveseveral classes that contain almost identical algorithms with some minor differences. ▰ let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure. 19 When to use Template:
  • 20.
    ▰ ASP.NET pagelife cycle: Init, Load, Validate, Prerender, Render. ▰ Parser. 20 Real World Example:
  • 21.
    ▰ Template andstrategy. 21 Template with other Design Pattern
  • 22.