Strategy Design Pattern

Do you Know The Strategy Design Pattern?

What is Strategy Design Pattern?

Do you use Strategies?

We all have dealt with strategies in certain moments of our life. In more technical words strategy is a set of planned actions designed to achieve a specific goal. Simply, you try out many strategies when a final exam is close by. You try to collect photo copied notes from friends and study all of them within a week, you try to go to any knowledge transferring discussions for missed lessons, you try to watch some videos on YouTube and learn the subject and many more creative strategies when the exams are getting closer. From one method or another, you were able to get good marks to pass the exam. Likewise, strategy design pattern provides multiple solutions for a recurring problem in the software world.

If you are not sure about design patterns please read Introduction to Design Patterns.

 Objects are behaving

As a norm in software design and development world, isolated components are called as objects. To build a successful system we need to have many objects with the ability to communicate and co-work with each other. But in most complex systems objects’ interactions cause many issues. Behavioral design patterns provide solutions to this object communication and interaction problems.

Define Strategy Pattern

Strategy design pattern is identified as a behavioral design pattern since it directly controls the object behaviors. This pattern is used to handle the software algorithms, manage interactions and responsibilities among objects to achieve a specific task. This is the more formal definition from the GOF team,

Strategy pattern defines a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

How Strategy Pattern Works

This pattern is most suitable when we have multiple solutions to one problem. In a more technical way there are multiple algorithms to achieve a specific task. Each algorithm is independent of one another and enclosed with own implantation. But all of those algorithms is capable of resolving that specific problem likewise anyone can replace one algorithm from another depending on their requirement. Due to that feature, each algorithm is independent of the client who uses it. Algorithm content and implementation are decoupled from the client who uses it and the client can select any upon runtime based on individual parameters and conditions. Below figure describes the overall mechanism of the strategy pattern.

Strategy Design Pattern Overview
Strategy Design Pattern Overview

What is Algorithm in Strategy Pattern?

The algorithm is an individual strategy. In a context of strategy design pattern, normally there is a template strategy as an abstract strategy in a common interface. Each algorithm should implement that common interface which is enforced by an abstract strategy. Each individual strategy should have a unique functionality that can be implemented as a concrete strategy.

When to use the Strategy Pattern?

  • When there are multiple solutions or ‘n’ number of algorithms for a certain problem and we need all of those to be available so that it is possible to make a choice at runtime
  • When you have different variants of one algorithm to achieve any function, each variant can enclose in a strategy
  • When there are multiple related classes which only differs from their behaviors, strategy pattern can be used to dynamically select the required behavior
  • When the objects should have easy interaction without getting tightly coupled with dependencies
  • When there are more private internal data to hide from outside
  • If you need to avoid heavy conditional statements those each behavior can be encapsulated in a strategy

List Class use Strategy Pattern?

Yes. You can see from the below diagram how the List class has used the strategy pattern. Let’s assume that you want to sort some numbers and you decide to use List class to achieve this.

List class uses the strategy pattern via SortingStrategy interface. There are 3 candidate sorting classes as Bubble Sort, Selection Sort and Insertion Sort which contain different sorting mechanisms.

  • Bubble Sort: Repeatedly compare neighbour pairs and swap if necessary
  • Selection Sort: Repeatedly pick the smallest element to append to the result
  • Insertion Sort: Repeatedly add new element to the sorted result

All 3 sorting algorithms will implement the Sorting Strategy interface. The sorting algorithm to use with our List class will be decided at the runtime depending on the requirement and conditions dynamically based on client’s selection.

List Object Strategy
List Object Strategy

Take the implementation of the collection. If the list has a sorting algorithm built into it – you cannot change the sorting algorithm. But when strategy pattern is injected, the sorting algorithm can be used independently while keeping the List implementation stable.

Real World Example

Strategy pattern usage can be seen in many real world scenarios as well. Let’s take an item purchasing at a shopping mall.

Basic Steps to Perform

  1. Customer comes to the cashier
  2. System accepts customer details
  3. System calculates bill amount
  4. System apply discount based on day of the week (This is where the strategy pattern comes into the picture)
    1. Monday – 10 %
    2. Friday – 50 %
  5. System output the total bill for the customer

System has to select which discount option assign to the bill based on the day of the week

  • Problem – System has to apply discounts to bills
  • Solution – There are 3 options for discounts based on days – system has to select one and calculate the bill amount

High-Level Class Diagram for the Shopping Mall Scenario

Above figure contains the code samples for the main interface and implementing concrete classes. The rest of the class implementations are described later.

  • Context – includes client or decision logic to select the correct discount based on the day


Class Diagram ShoppingMall
Class Diagram ShoppingMall

 Context Class

This class bridges a client with the strategy keeping a reference to the DiscountStrategy object. Hence, the ShoppingMallContext class will be initialized with a DiscountStrategy object in order to forward the client’s request to the strategy.



Client Class

This class is the main user of the functionality or the particular algorithm. It requests the exact strategy based on certain requirements and conditions. Client class should first invoke the Context class in order to grab the decided strategy at runtime.


Client Class
Client Class


Advantages of Strategy Pattern

  • This gives a greater flexibility and reuse
  • It is possible to eliminate large conditional statements using this pattern
  • Since this is dynamic we don’t need to find alternatives to painful hard coding
  • Easier to maintain the different behaviors since they are encapsulated in separate classes
  • Everyone needs not be aware of internal algorithms when adding new features
  • There are wide variety of selections for same behavior


Challenges of Strategy Pattern

The Strategy Design Pattern is a powerful design pattern that allows for flexible and extendable code. However, it does have some limitations:

  1. Increased complexity: Implementing the strategy pattern can add complexity to your codebase, especially if you have many different strategies to implement. This complexity can make it harder to understand and maintain the code.
  2. Additional classes: The strategy pattern requires the creation of additional classes for the strategies, the context and the interfaces, which can make the codebase larger and harder to navigate.
  3. Performance: In some cases, the performance cost of creating and switching between different strategy objects can be high.
  4. Limited to similar operations: The strategy pattern is best suited to situations where the different algorithms perform similar operations but with different implementations. If the operations are very different, it may make more sense to use a different design pattern.
  5. Dependency: The context class is dependent on the strategy objects, it can make it harder to test the context class if the strategy objects have complex dependencies or if they’re heavy in terms of instantiation.
  6. It doesn’t solve some problems: The strategy pattern is intended to be used to solve problems that involve different algorithms or behaviors that need to be used in different parts of a program, but it doesn’t solve other types of problems.

That being said, it’s important to weigh the costs and benefits of using the strategy pattern in any particular case. It’s a powerful pattern that is widely used when it’s applied in the correct context.

It’s also important to be mindful of the design patterns used and their limitations, to ensure the codebase is maintainable, readable and efficient.

Source Code:

You can find the source code used in this tutorial on github :

10 thoughts on “Strategy Design Pattern”

  1. ShoppingMallContext constructor should receive the strategy, I understand it’s a simple mistake, please rectify.
    A very good and well explained article though.
    Thank you.

    • Thanks for pointing out the mistake.Really glad that you liked the article. It is readers who complete the article, by pointing out the mistakes, typos. Thanks

    • Strategy is a behavioral pattern and Builder is a creational pattern. You can use them together. Like Strategy can be used to pick up an Algorithm and then builder can be create objects for those algorithms

  2. Nice read. Really helps to brush up the concepts that I know but somehow don’t remember the vocabularies. Good examples.

  3. I think the creation of too many objects isn’t probably a concern for modern languages. I would really like to know if java can’t handle creation of objects?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: