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.


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

Related Patterns


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.


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.


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;

  public int compare(Person p1, Person p2){
      return p1.firstName.compareTo(p2.firstName);
      return p1.lastName.compareTo(p2.lastName);
      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;