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.

Classic implementation of the Strategy Design Pattern

The strategy interface that must be implemented by all strategies.

[sourcecode language=”java” toolbar=”false”]
public interface Strategy {

public void execute();

}
[/sourcecode]

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

[sourcecode language=”java” toolbar=”false”]
public class StrategyA implements Strategy {
@Override
public void execute(){
System.out.print("Executing strategy A");
}
}
[/sourcecode]

[sourcecode language=”java” toolbar=”false”]
public class StrategyB implements Strategy {
@Override
public void execute() {
System.out.print("Executing strategy B");
}
}
[/sourcecode]

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

[sourcecode language=”java” toolbar=”false”]
public class Context {

private Strategy strategy;

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

public void executeStrategy(){
this.strategy.execute();
}
}
[/sourcecode]

An example of using the Strategy.

[sourcecode language=”java” toolbar=”false”]
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();
}
}
[/sourcecode]

Enum implementation of the Strategy Design Pattern

Now lets 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.

[sourcecode language=”java” toolbar=”false”]
public enum Strategy implements 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();
}
[/sourcecode]

We use this implementation as follows:

[sourcecode language=”java” toolbar=”false”]
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();
}
}
[/sourcecode]

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.

[sourcecode language=”java” toolbar=”false”]
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();
}
}
[/sourcecode]

Open In Codenvy

To save time you can view this code directly in your browser by using codenvy’s cloud IDE. Click this link to view this code now.

Links

 

 

Leave a Reply

%d bloggers like this: