Programming: Strategy design pattern step by step

Dev Avenue
3 min readJul 11, 2021

What is the strategy design pattern?

Strategy pattern or Policy pattern is used when you have to choose between different algorithms to implement.

In a strategy pattern, you will see an interface class, classes that implements the interface, a selector code (can be class, or an abstract class), the main class / client class or your controller.

THE WHY? why do we need to implement it?

Here’s the not so object-oriented approach we can do. Most of the people can code using the MVC pattern.

Let’s say we have a controller for Payment,. Let’s call it PaymentController and the user can select which payment controller to use. If we are going to implement it we can do the code below.

class PaymentController {
public function pay($paymentId,$amount) {
if ($paymentId == 1){
$this->goToPaymentPaypalPage($amount);
}
if ($paymentId == 2 ) {
$this->goToPaymentCashOnDeliveryPage($amount);
}
}
}

The code below will work but not for the long run if we needed to implement several types of implementation in the future we would end up bloating our controller different methods and the next developer will have difficulty understanding our code.

So how are we going to implement the strategy pattern?

Step 1. list all the algorithms that you’re going to use.

Step 2. Create the interface class for those algorithms.

Step 3. Create the “context class” or the “setter class”.
this class is where you switch strategies or implementations or algorithms.
this could be a normal class or you can use an abstract class to separate concerns or for more advanced use cases.

Step 4. Create a method where you can set or inject the interface to the “setter class” or your “context class”.

Step 5. Use it in your controller or client class or where ever you want to use it.

Example.

Let’s say we want to use a different algorithms on analyzing resume.

Step 1. List all the different algorithms of implementation on analyzing a resume.

class Analyzer1 {
public function analyze() { }
}

Class Analyzer2 {
public function analyze() { }
}

Step 2. create the interface to be use in different algorithms

interface AnalyzerInterface {
public function analyze();
}

Step 3. Implement the interface into the algorithms

class Analyzer1 implements AnalyzerInterface {
public function analyze() { }
}

Class Analyzer2 implements AnalyzerInterface {
public function analyze() { }
}

Step 4. Create a method “setter class” or the “context class” where you will set the interface. As you can see we injected the AnalyzerInterface into our analyze method. and we called the analyzed method of whichever Analyzer we will inject.

class ResumeAnalyzer {

public function analyze($filename,$AnalyzerInterface $analyzer)
{
$this->analyze();
}

}

Step 5. use it in our controller. As you can see because we have an interface we can choose between analyzer1 and analyzer2 on which algorithm to use to analyze a resume. Use “strategy pattern” when you have to choose different “strategy” on run-time.

$resumeAnalyzer = new ResumeAnalyzer();
$resumeAnalyzer->analyze($filename,new Analyzer1());

We can switch between which analyzer or algorithm to use in run-time with simplicity and anticipation of the future implementations.

Other use cases of this pattern.

1. Logger — choosing between where to log: database, text, excel, etc

2. Mail — choosing between which mail provider : smtp,sparkpost,mailgun,etc

3. Duck Behaviours — Choosing different kinds of behavior a duck can do: swim(), walk(), quack(), fly(), notFly(). This example requires an abstract class that we will discuss in different tutorial.

For now, try using the strategy pattern in your code. whenever you need to choose different algorithms in run-time this is the way to go.

--

--