The strategy pattern is a behavioral design pattern that enables an algorithm’s behavior to be selected at runtime without causing tight coupling. The strategy pattern defines a family of algorithms, encapsulates each algorithm, and makes algorithms interchangeable. Strategy pattern lets the algorithm vary independently from clients that use it.

For more information go to http://en.wikipedia.org/wiki/Strategy_pattern

The definition in original Gang of Four book is given below

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Class Diagram

Strategy Pattern in Java

Example

Suppose we can have slightly different temperatures at different time(i.e., morning, noon, afternoon, evening etc.) in a day. So we do not know what would be the temperature at different time, until we reach to morning or evening, that is, run-time. This is the whole point of Strategy pattern.

Implementation

Define a interface DayTemperature, which has one method displayTemperature()

package com.roytuts.designpattern.strategy;

public interface DayTemperature {

  void displayTemperature(String temp);

}

Now we have several kinds of day time.

package com.roytuts.designpattern.strategy;

public class MorningTemperature implements DayTemperature {

  @Override
  public void displayTemperature(String temp) {
    System.out.println("Today's temperature at morning : " + temp);
  }

}
package com.roytuts.designpattern.strategy;

public class NoonTemperature implements DayTemperature {

  @Override
  public void displayTemperature(String temp) {
    System.out.println("Today's temperature at noon : " + temp);
  }

}
package com.roytuts.designpattern.strategy;

public class AfterNoonTemperature implements DayTemperature {

  @Override
  public void displayTemperature(String temp) {
    System.out.println("Today's temperature at afternoon : " + temp);
  }

}
package com.roytuts.designpattern.strategy;

public class EveningTemperature implements DayTemperature {

  @Override
  public void displayTemperature(String temp) {
    System.out.println("Today's temperature at evening : " + temp);
  }

}
package com.roytuts.designpattern.strategy;

public class NightTemperature implements DayTemperature {

  @Override
  public void displayTemperature(String temp) {
    System.out.println("Today's temperature at night : " + temp);
  }

}

Define a day time in when temperature gets changed.

package com.roytuts.designpattern.strategy;

public class DayTime {

  private DayTemperature strategy;

  public DayTime(DayTemperature strategy) {
    this.strategy = strategy;
  }

  public DayTemperature getStrategy() {
    return strategy;
  }

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

  public void showTemperature(String temp) {
    strategy.displayTemperature(temp);
  }

}

Run the above test class and see the below output in the console

Today's temperature at morning : 10 Degree Celsius
Today's temperature at noon : 15 Degree Celsius
Today's temperature at afternoon : 13 Degree Celsius
Today's temperature at evening : 11 Degree Celsius
Today's temperature at night : 9 Degree Celsius

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *