Strategy Pattern in Java

Concept: A creational pattern. The Strategy Pattern is essentially a way to provide multiple ways to perform some sort of similar action. I've used the Strategy Pattern in projects to handle actions spanning from social network login authorization to payment processing authorization.

The concept is that we know we want to do something, such as an authorization (login/process payment), but the algorithm by which this takes place (Facebook vs. Twitter / Credit Card vs. Checking Account) needs to be decoupled and managed at an object level from the calling code.

Pros: Allows for incorporating new behavior in a loosely coupled way within the context entity.

Cons: The client/calling code must be aware of the various strategies that are available.

Example: In the following example, we exhibit how the Strategy Pattern can be used to distinguish the amount of calories that can be burned via different exercises at a given gym (Swimming vs Jogging). In the future, we could add other ways to exercise at our gym (perhaps Cycling?). Given the same 30 minutes of exercise time, we can see that we burn a different amount of calories from each activity.

We accomplish the Strategy Pattern by:

  • Creating the Strategy Interface ExerciseStrategy and defining the shared activity type. In this case, it's the method exercise().
  • We create Concrete Strategies SwimmingStrategy and JoggingStrategy
  • We create a Context named ExerciseContext which has a simple executeStrategy() method that is called when we want to literally execute the strategy
  • Last, in our main() function, all we need to do is tell the Context what Strategy we want to use and it performs the actions in a clean, elegant manner.

ExerciseStrategy.java

package com.justinbellamy.strategyexample;

public interface ExerciseStrategy {  
    public int exercise(int minutes);
}

SwimmingExample.java

package com.justinbellamy.strategyexample;

public class SwimmingStrategy implements ExerciseStrategy {  
    public int exercise(int minutes) {
        return (minutes * 7);
    }
}

JoggingExample.java

package com.justinbellamy.strategyexample;

public class JoggingStrategy implements ExerciseStrategy {  
    public int exercise(int minutes) {
        return (minutes * 11);
    }
}

ExerciseContext.java

package com.justinbellamy.strategyexample;

public class ExerciseContext {  
    private ExerciseStrategy strategy;

    public ExerciseContext(ExerciseStrategy strategy) {
        this.strategy = strategy;
    }

    public int executeStrategy(int minutes) {
        return strategy.exercise(minutes);
    }
}

StrategyExample.java

package com.justinbellamy.strategyexample;

public class StrategyExample {

    public static void main(String[] args) {
        ExerciseContext context = new ExerciseContext(new JoggingStrategy());
        System.out.println("30 minutes of running burns about " + context.executeStrategy(30) + " calories.");

        context = new ExerciseContext(new SwimmingStrategy());
        System.out.println("30 minutes of swimming burns about " + context.executeStrategy(30) + " calories.");
    }
}

Source Code on GitHub: https://github.com/justinbellamy/StrategyExampleJava