View RSS Feed

My Java Tips

The Strategy Design Pattern in Java

Rate this Entry
by , 11-20-2011 at 06:25 PM (2103 Views)
The strategy design pattern is one of the most fundamental design pattern. The strategy pattern is a design pattern with the help which algorithms can be selected in a real time. The Strategy pattern is behavioral design pattern. The strategy pattern is very useful when it is needed to swap the algorithms used in an application at runtime.


The strategy pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable. The strategy design pattern allows the algorithms vary independent of each other from clients that implement them. With the help of strategy pattern a programmer can develop software as a collection of loosely coupled interchangeable units. As a result of which the software developed becomes highly extensible, maintainable, and reusable.

Though it looks like that Strategy design pattern and Bidge Design patterns are same, they are different. While Strategy design pattern is behavioral design pattern the Bidge Design pattern is structural design pattern. In the Strategy design pattern coupling between the context and the strategies is tighter where as in the bridge design pattern the coupling between the abstraction and the implementation is not as tight.


As per the Strategy pattern the behavior of a class should be encapsulated using interfaces instead of inheriting it.

How to implement the Strategy pattern

1. Create the strategy interface.
2. Implement the algorithm using the strategy interface
3. Configure the Context class and maintain a reference to a Strategy object
4. In the Context class, implement public get and set methods for the Strategy object

Example: StrategyPatternDemo.java

Java Code:
class StrategyPatternDemo {

  public static void main(String[] args) {
    Context context;

    context = new Context(new Strategy1());
    context.execute();

    context = new Context(new Strategy2());
    context.execute();

    context = new Context(new Strategy3());
    context.execute();
  }
}

  interface MyStrategyInterface {
    void execute();
  }

  class Strategy1 implements MyStrategyInterface {
    public void execute() {
      System.out.println( "Called Strategy1.execute()" );
    }
  }

  class Strategy2 implements MyStrategyInterface  {
    public void execute() {
      System.out.println( "Called Strategy2.execute()" );
    }
  }

  class Strategy3 implements MyStrategyInterface {
    public void execute() {
      System.out.println( "Called Strategy3.execute()" );
    }
  }

  class Context {
    MyStrategyInterface strategy;

    public Context(MyStrategyInterface strategy) {
      this.strategy = strategy;
    }

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

Submit "The Strategy Design Pattern in Java" to Facebook Submit "The Strategy Design Pattern in Java" to Digg Submit "The Strategy Design Pattern in Java" to del.icio.us Submit "The Strategy Design Pattern in Java" to StumbleUpon Submit "The Strategy Design Pattern in Java" to Google

Comments