Strategy Pattern

STRATEGY

This pattern belongs to the group of behavioral patterns. In other words, those that deal with the structure of processes and activities in a broader perspective. It is characterized by the fact that it encapsulates the process. It divides it into smaller modules that contain elements of the problem solution. The strategy manages these modules and allows them to be used interchangeably depending on the context.

The great advantage of this pattern is flexibility. If a new situation arises to be handled, a new algorithm. Just add a new class and put the appropriate interface on it. So it is a relatively very small job.

Let’s try to understand the essence of this pattern using the example of a washing machine program. 

So let’s create a basic interface for our program:

public interface ToDoList {
public void completeTheTask();
}

Next, we will create real classes needed to support our program, which will be implementations of our interface:

public class PouringWater implements ToDoList{

public void completeTheTask(){
System.out.println("I pour water into the drum");
}
}
public class WaterHeating implements ToDoList{

public void completeTheTask(){
System.out.println("I heat the water to the right temperature");
}
}
public class MainWashCycle implements ToDoList{

public void completeTheTask(){
System.out.println("The main wash is underway"); }
}
public class Rinsing implements ToDoList{
public void completeTheTask(){
System.out.println("Rinsing the laundry");
}
}
public class PreSpin implements ToDoList{
public void completeTheTask(){
System.out.println("Pre-spin cycle");
}
}
public class FinalCentrifugation implements ToDoList{
public void completeTheTask(){
System.out.println("Final spin of the laundry");
}
}

Now, of course, we will need a washing machine to complete the various tasks:

public class WashingMachine {

ToDoList task;
public WashingMachine(ToDoList newTask){
task = newTask;
}
public void doIt(){
task.completeTheTask();
}
public void switchTask(ToDoList newTask){
task = newTask;
}
}

So let’s test the following sequence of events:

public class Test {

public static void main(String[] args){
WashingMachine washing = new WashingMachine(new PouringWater());
washing.doIt();
washing.switchTask(new WaterHeating());
washing.doIt();
washing.switchTask(new MainWashCycle());
washing.doIt();
washing.switchTask(new Rinsing());
washing.doIt();
washing.switchTask(new PreSpin());
washing.doIt();
washing.switchTask(new Rinsing());
washing.doIt();
washing.switchTask(new FinalCentrifugation());
washing.doIt();
}
}

Below is the result of these operations:

I pour water into the drum
I heat the water to the right temperature
The main wash is underway
Rinsing the laundry
Pre-spin cycle
Rinsing the laundry
Final spin of the laundry

Summary

I hope that the presentation of the above example will help you understand what the Strategy pattern is and I am convinced that you will be able to implement it without any problems when the need arises.

Leave a Comment

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