News Ticker

An Enum implementation of the Strategy pattern

What is the Strategy design pattern?

The Strategy design pattern is designed to provide a way of selecting from a range of interchangeable strategies.

How is it implemented?

Classic implementation requires that each Strategy implements an interface and provides a concrete implementation for an execute method. The strategy is selected and the execute method called via an interface reference.

Learn more about design patterns

Programming Foundations: Design Patterns
Java Design Patterns: Creational
Java EE: Design Patterns and Architecture
Spring: Design Patterns

Classic implementation of the Strategy design pattern

The strategy interface that must be implemented by all strategies.

public interface Strategy {
   public void execute();
}

Two classes showing the implementation of the Strategy interface and a concrete implementation of the execute method.

 public class StrategyA implements Strategy {
   @Override
   public void execute(){
     System.out.print("Executing strategy A");
   }
 }

 public class StrategyB implements Strategy {
   @Override
   public void execute() {
     System.out.print("Executing strategy B");
   }
 }

The context class selects the strategy and executes the execute method of the selected strategy.

 public class Context {

   private Strategy strategy;

   public void setStrategy(Strategy strategy){
     this.strategy = strategy;
   }

   public void executeStrategy(){
     this.strategy.execute();
   }
 }

An example of using the Strategy.

public class UseStrategy {

  public static void main(String[] args){

  Context context = new Context();

  context.setStrategy(new StrategyA());
  context.executeStrategy();

  context.setStrategy(new StrategyB());
  context.executeStrategy();
  }
}

Enum implementation of the Strategy design pattern

Now let’s look at the above example implemented as an enum.

This implementation only requires two classes: an enum class and a class that uses it.

All the magic happens in the enum where the concrete implementation of the strategy is done in the definition of each enum constant.

 
public enum Strategy {

  STRATEGY_A {
    @Override
    void execute() {
      System.out.print("Executing strategy A");
    }
  },
  
  STRATEGY_B {
    @Override
    void execute() {
      System.out.print("Executing strategy B");
    }
  };

  abstract void execute();
}

We use this implementation as follows:

public class UseStrategy {

  public static void main(String[] args) {
    UseStrategy useStrategy = new UseStrategy();
    useStrategy.perform(Strategy.STRATEGY_A);
    useStrategy.perform(Strategy.STRATEGY_B);
  }

  private void perform(Strategy strategy) {
    strategy.execute();
  }
}

EnumMap implementation of the Strategy design pattern

Alternatively, the enum can be selected from a map of Strategies where the key is the enum itself. This is shown in the following example using an EnumMap.

public class EnumMapExample {

  static EnumMap<Strategy, Strategy> lookupStrategy= new EnumMap<>(Strategy.class);

  {
    lookupStrategy.put(Strategy.STRATEGY_A, Strategy.STRATEGY_A);
    lookupStrategy.put(Strategy.STRATEGY_B, Strategy.STRATEGY_B);
  }

  public static void main(String[] args) {
    lookupStrategy.get(Strategy.valueOf("STRATEGY_A")).execute();
    lookupStrategy.get(Strategy.valueOf("STRATEGY_B")).execute();
  }
}

Criticisms of the Enum approach

No solution is perfect and will have plenty of criticism. What’s important is not to try to find that perfect solution but to understand the limitations of the solution you choose.

Here are some criticisms of the Enum approach to the Strategy Pattern:

  • One of the strengths of the classic implementation is that you don’t have any merge issues when you implement a new strategy since the strategy implementations are in separate classes.
  • You would have to know all possible strategies at compile time, and you are not able to add new strategies at runtime.
  • It violates the rule of “open for extension but closed for modification”.
  • Main applicable use would be when you need a limited amount of strategies and limited to those.

Links

2 Trackbacks / Pingbacks

  1. Strategy Pattern Implemented as an Enum Boost your career with us Professional Java EE Video Training and Tutorials
  2. Enum: How to use name() and toString() methods correctly

Leave a Reply