Hot Posts

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.

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

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();
}

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();
    }
}

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: