Problem

A standard value of software development has been "maximize cohesion and minimize coupling". Over time, objects become more entwined and less flexible to new implementations.

Solution

Define a family of interchangeable encapsulated algorithms. The Strategy pattern allows the algorithm to vary based on the clients that use it.

Related Patterns

Discussion

Clients should prefer interfaces rather than direct connections with peer objects. An interface can represent an abstract base class or the method signatures to be expected.

Examples

Different modes of transportation to an arbitrary goal are an example of a Strategy. Any of the different modes will get a traveler to the destination, which can be applied based on tradeoffs between convenience, speed, and cost.

Code

The Comparator.sort() method in the Java utils is a great example of the Strategy pattern. By implementing the Comparator in our class, we can override the compare() method and then make our own compare algorithm based on the compareMode set during initialization.

public class PersonComparator implements Comparator<Person>{
  public static final int COMPARE_BY_FIRST_NAME = 0;
  public static final int COMPARE_BY_LAST_NAME = 1;
  public static final int COMPARE_BY_AGE = 2;
  private int compareMode = 1;

  public PersonComparator(){}
  public PersonComparator(int compareMode){
    this.compareMode = compareMode;
  }

  @Override
  public int compare(Person p1, Person p2){
    switch(compareMode){
    case COMPARE_BY_FIRST_NAME:
      return p1.firstName.compareTo(p2.firstName);
    case COMPARE_BY_LAST_NAME:
      return p1.lastName.compareTo(p2.lastName);
    default:
      return p1.age.compareTo(p2.age);
    }
  }
}
class Person{
  public String firstName;
  public String lastName;
  public int age;
  public Person(String first, String last, int newAge){
    firstName = first;
    lastName = last;
    age = newAge;
  }
}