Strategy (Design Pattern)

Strategy Design Pattern

The Strategy Design Pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. This pattern enables the algorithm to vary independently from the clients that use it. It’s particularly useful for scenarios where you need to select an algorithm at runtime.

Key Benefits:

  1. Flexibility: Swap algorithms without altering the client code.
  2. Maintainability: Isolate and localize the code that implements specific behaviors.
  3. Extensibility: Add new strategies without modifying existing code.

Components:

  • Context: The class that uses a Strategy to perform a specific task.
  • Strategy: An interface common to all supported algorithms.
  • Concrete Strategies: Classes that implement the Strategy interface.

Example:

Let's say you are building a navigation app that can use different routing algorithms like ShortestPath and ScenicRoute.

Step-by-Step Implementation:

  1. Define the Strategy Interface:

    public interface RouteStrategy {
        void buildRoute(String start, String end);
    }
    
  2. Implement Concrete Strategies:

    public class ShortestPath implements RouteStrategy {
        @Override
        public void buildRoute(String start, String end) {
            System.out.println("Building the shortest route from " + start + " to " + end);
        }
    }
    
    public class ScenicRoute implements RouteStrategy {
        @Override
        public void buildRoute(String start, String end) {
            System.out.println("Building the scenic route from " + start + " to " + end);
        }
    }
    
  3. Create the Context Class:

    public class Navigator {
        private RouteStrategy strategy;
    
        public void setStrategy(RouteStrategy strategy) {
            this.strategy = strategy;
        }
    
        public void buildRoute(String start, String end) {
            strategy.buildRoute(start, end);
        }
    }
    
  4. Use the Strategy in the Client:

    public class Client {
        public static void main(String[] args) {
            Navigator navigator = new Navigator();
    
            // Use the shortest path strategy
            navigator.setStrategy(new ShortestPath());
            navigator.buildRoute("Home", "Office");
    
            // Switch to the scenic route strategy
            navigator.setStrategy(new ScenicRoute());
            navigator.buildRoute("Home", "Office");
        }
    }
    

UML Diagram:

classDiagram Navigator --> RouteStrategy RouteStrategy <|.. ShortestPath RouteStrategy <|.. ScenicRoute class Navigator { - RouteStrategy strategy + setStrategy(RouteStrategy strategy) + buildRoute(String start, String end) } class RouteStrategy { <<interface>> + buildRoute(String start, String end) } class ShortestPath { + buildRoute(String start, String end) } class ScenicRoute { + buildRoute(String start, String end) }

Summary:

The Strategy Pattern is like having a toolbox of algorithms. When you need to perform a task, you just pick the right tool (algorithm) without changing the toolbox (client code). This pattern promotes cleaner, more modular, and maintainable code, making your applications easier to extend and adapt.


Backlinks