Strategy Design Pattern (simple example implementation in PHP)
Tags: php, designpatterns, strategypattern
Published: 2021-02-07 21:00:28

---
title: Strategy Design Pattern (simple example implementation in PHP)
published: true
description: Strategy Design Pattern (simple example implementation in PHP)
tags: PHP, designpatterns, strategypattern
//cover_image: https://direct_url_to_image.jpg
---
**NOTE - Please everything written down here is extracted from books which I read years ago and actually jotted down in a notebook (to be honest I don't remember the names of these books), I realized that keeping this information inside a notebook is not the best of options, come on what if something happened to my notebook? Also, I wanted this to be available to everyone who might have been wanting to understand the Strategy Design Pattern as well -:)**

If you have not read my previous post on [Factory Design Pattern](https://dev.to/osaigbovoemmanuel1/factory-design-pattern-simple-example-implementation-in-php-2780), please do well to check it out.

The strategy design pattern is a type of behavioural pattern. Behavioural patterns are used to address how an application runs. As a comparison, the factory pattern can change the object type on the fly.

The Strategy design pattern comes in handy when we have multiple chunks of code performing similar operations. It defines an encapsulated and interchangeable family of algorithms(an algorithm just being a process or set of code used to solve a problem).

The strategy design pattern is most useful in situations where you have classes that may be similar, but not related, and differ only in their specific behaviour. For example, say you need a filtering system for strings, Different filters might include 
- Stripping out HTML
- Crossing out swear words
- Catching character combinations that can be used to send spam through contacts form and the like.

The only quality that these three approaches have in common is that they are all applied to strings, other than that there are no commonalities that might suggest they would make sense as derived subclasses from a base class (factories).

Furthermore, these filters might be applied differently on the fly. For example, an application switch might indicate whether or not HTML or swear words are allowed in the text. This, then, is a good candidate for the strategy pattern.

- First, start by defining an interface that dictates the needed functionality:

```php
interface Filter {
     public function filter($str);
}
```

- Specific filters types then implement their specific versions of the interface's methods:

```php
class HTMLFilter implements Filter {
    public function filter($str) {
       //strip out the HTML
       return $str;
    } 
}
```
```php
class SwearFilter implements Filter {
   public function filter($str) {
     //cross out swear words
     return $str;
   }
}
```
- Finally, another class would be written that could use any filter:

```php
class FormData {
   private $data;

   public function __construct($input) {
     $this->data = $input;
   }

   public function process(Filter $type)
   {
      return $this->data = $type->filter($this->data);
   }
}
```

In the Strategy design pattern, an interface is implemented by more concrete classes, which in turn are used by a specific object (the context, FormData in this case).
In that code, the process() method expects to receive an object of type Filter through which the data would then be run.

```php
$form = new FormData($someUserInput)
{
    if(//no HTML is allowed) {
      $form->process(new HTMLFilter);  
    }
    
   if(//no swear words allowed) { 
      $form->process(new SwearFilter);
   }
} 
```