Skip to content

Learning the Strategy Design Pattern

Posted on:September 20, 2022 at 01:13 PM

In this blog post, we will dive into the Strategy Design Pattern, a powerful pattern that allows you to switch algorithms or behaviors at runtime, making your code more flexible and extensible. We will break down the concept, examine its advantages, and provide a simple example to help you understand how to implement it effectively.

Table of contents

Open Table of contents

Sections

Why to use the Strategy Design Pattern?

The Strategy Design Pattern is a behavioral design pattern that addresses situations where you have multiple ways to perform a specific task or handle data within your application. It offers a means to define a family of algorithms, encapsulate each one of them, and make them interchangeable.

Imagine an application that retrieves data from a database and must display it in different formats based on user preferences. Or think of a mapping application that calculates routes based on different modes of transportation (e.g., car, public transport, walking). In both scenarios, you have the same core data but need to present it differently. This is where the Strategy Design Pattern shines.

StrategyGraphicProblem

Key Elements of the Pattern

The main elements of the Strategy Design Pattern include:

Context:

The context is the component that needs to perform a specific operation. It maintains a reference to a strategy object and can switch between different strategies.

Strategy:

The strategy interface declares the method(s) common to all supported algorithms. Concrete strategies implement these methods, providing different behavior.

Advantages of Using the Strategy Design Pattern

Flexibility:

The Strategy Pattern allows you to switch between different algorithms or behaviors at runtime. This flexibility is valuable when you want to provide users with choices or accommodate various situations without altering your code.

Reusable Code:

Each strategy is encapsulated within its own class. This promotes code reuse and separates the different algorithm implementations, making them easier to maintain.

Scalability:

As your application grows, you can add new strategies without altering the existing ones. This makes the pattern particularly useful for large and complex systems.

Improved Testing:

With strategies in separate classes, you can easily test each one individually. This results in more focused and efficient testing.

Implementing the Strategy Design Pattern: A Simple Example

Let’s explore this pattern by creating a simple example where we define different strategies to format a string. We’ll use three strategies: lowercase, uppercase, and random case. Our “context” will be the Executor class, which can switch between these strategies at runtime.

Strategy Interface

public interface PrintStrategy {
    String formatString(String input);
}

Concrete Strategys

Lowercase Strategy

public class LowercaseStrategy implements PrintStrategy {
    public String formatString(String input) {
        return input.toLowerCase();
    }
}

UppercaseStrategy

public class UppercaseStrategy implements PrintStrategy {
    public String formatString(String input) {
        return input.toUpperCase();
    }
}

RandomCaseStrategy

import java.util.Random;

public class RandomCaseStrategy implements PrintStrategy {
    public String formatString(String input) {
        StringBuilder output = new StringBuilder();
        Random random = new Random();
        for (char c : input.toCharArray()) {
            if (random.nextBoolean()) {
                output.append(Character.toUpperCase(c));
            } else {
                output.append(Character.toLowerCase(c));
            }
        }
        return output.toString();
    }
}

Context (Executor)

Client Code

public class Executor {
    private PrintStrategy strategy;

    public Executor(PrintStrategy strategy) {
        this.strategy = strategy;
    }

    public void printString(String input) {
        System.out.println(strategy.formatString(input));
    }
}

Using the Strategies

public class Client {
    public static void main(String[] args) {
        PrintStrategy lowercaseStrategy = new LowercaseStrategy();
        PrintStrategy uppercaseStrategy = new UppercaseStrategy();
        PrintStrategy randomCaseStrategy = new RandomCaseStrategy();

        Executor executor = new Executor(lowercaseStrategy);
        executor.printString("Hello, Strategy Pattern!");

        executor = new Executor(uppercaseStrategy);
        executor.printString("Hello, Strategy Pattern!");

        executor = new Executor(randomCaseStrategy);
        executor.printString("Hello, Strategy Pattern!");
    }
}

This example demonstrates how we can switch between different strategies (lowercase, uppercase, and random case) to format and print a string based on our needs.

Conclusion

The Strategy Design Pattern provides an elegant way to separate algorithms or behaviors from the context in which they are used. It promotes flexibility, reusability, and maintainability in your code. By encapsulating each strategy in its own class, you can easily add, modify, or swap strategies to adapt to changing requirements, making your software more resilient and adaptable.

Happy coding! 🚀