Strategy Design Pattern

The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing clients to switch algorithms dynamically without altering the code structure.

Table of Contents

  1. Characteristics of the Strategy Design Pattern
  2. Components of the Strategy Design Pattern
  3. Communication between the Components
  4. Implementation

Characteristics of the Strategy Design Pattern

The Strategy Design Pattern exhibits several key characteristics that make it distinctive and effective for managing algorithm variations in software systems:

Components of the Strategy Design Pattern

Components of the Strategy Design Pattern

Context

The Context is a class or object that holds a reference to a strategy object and delegates the task to it.

It acts as the interface between the client and the strategy, providing a unified way to execute the task without knowing the details of how it’s done. The Context maintains a reference to a strategy object and calls its methods to perform the task, allowing for interchangeable strategies to be used.

Strategy Interface

The Strategy Interface is an interface or abstract class that defines a set of methods that all concrete strategies must implement.

It serves as a contract, ensuring that all strategies adhere to the same set of rules and can be used interchangeably by the Context. By defining a common interface, the Strategy Interface allows for decoupling between the Context and the concrete strategies, promoting flexibility and modularity in the design.

Concrete Strategies

Concrete Strategies are the various implementations of the Strategy Interface. Each concrete strategy provides a specific algorithm or behavior for performing the task defined by the Strategy Interface.

Concrete strategies encapsulate the details of their respective algorithms and provide a method for executing the task. They are interchangeable and can be selected and configured by the client based on the requirements of the task.

Client

The Client is responsible for selecting and configuring the appropriate strategy and providing it to the Context.

It knows the requirements of the task and decides which strategy to use based on those requirements. The client creates an instance of the desired concrete strategy and passes it to the Context, enabling the Context to use the selected strategy to perform the task.

Communication between the Components

In the Strategy Design Pattern, communication between the components occurs in a structured and decoupled manner. Here’s how the components interact with each other:

Implementation

UML Strategy Pattern

SortingContext

public class SortingContext {
    private SortingStrategy sortingStrategy;

    public SortingContext(SortingStrategy sortingStrategy) {
        this.sortingStrategy = sortingStrategy;
    }

    public void setSortingStrategy(SortingStrategy sortingStrategy) {
        this.sortingStrategy = sortingStrategy;
    }

    public void performSort(int[] array) {
        sortingStrategy.sort(array);
    }
}

SortingStrategy (Interface)

public interface SortingStrategy {
    void sort(int[] array);
}

Concrete Strategies

// BubbleSortStrategy
public class BubbleSortStrategy implements SortingStrategy {
    @Override
    public void sort(int[] array) {
        // Implement Bubble Sort algorithm
        System.out.println("Sorting using Bubble Sort");
    }
}

// MergeSortStrategy
public class MergeSortStrategy implements SortingStrategy {
    @Override
    public void sort(int[] array) {
        // Implement Merge Sort algorithm
        System.out.println("Sorting using Merge Sort");
    }
}

// QuickSortStrategy
public class QuickSortStrategy implements SortingStrategy {
    @Override
    public void sort(int[] array) {
        // Implement Quick Sort algorithm
        System.out.println("Sorting using Quick Sort");
    }
}