Strategy Pattern

Medium

In Strategy Pattern, composition is used over inheritance. It is advised to program to abstraction than to concretions. You see that Strategy Pattern is compatible with the SOLID principles.

Inheritance vs Composition
Inheritance `is a` relationship. To avoid duplication. Have similar      functionality.
Composition `has a ` relationship

  1. Strategy Pattern.  Youtube
    While inheritance is powerful, I have learned that it doesn’t always lead to the most flexible or maintainable designs.
    It’s about using Composition instead of inheritance. It’s about understanding that, inheritance is not about code reuse. I have learned there are ways of “inheriting” behavior at runtime through composition and delegation. When I inherit behavior by subclassing, that behavior is set statically at compile time. In addition, all subclasses must inherit the same behavior. If however, I can extend an object’s behavior through composition, then I can do this dynamically at runtime. It is possible for me to add multiple new responsibilities to objects through this composition, including responsibilities that were not even thought of by the designer of the superclass. By dynamically composing objects, I can add new functionality by writing new code rather than altering existing code. Because I’m not changing existing code, the chances of introducing bugs or causing unintended side effects in pre-existing code are much reduced.
    The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Real world example

Consider the example of sorting, we implemented bubble sort but the data started to grow and bubble sort started getting very slow. In order to tackle this we implemented Quick sort. But now although the quick sort algorithm was doing better for large datasets, it was very slow for smaller datasets. In order to handle this we implemented a strategy where for small datasets, bubble sort will be used and for larger, quick sort.

In plain words

Strategy pattern allows you to switch the algorithm or strategy based upon the situation.

Wikipedia says

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioural software design pattern that enables an algorithm’s behavior to be selected at runtime.

Programmatic example

Translating our example from above. First of all we have our strategy interface and different strategy implementations

interface SortStrategy {  public function sort(array $dataset): array; } class BubbleSortStrategy implements SortStrategy {  public function sort(array $dataset): array  {  echo "Sorting using bubble sort";  // Do sorting  return $dataset;  } } class QuickSortStrategy implements SortStrategy {  public function sort(array $dataset): array  {  echo "Sorting using quick sort";  // Do sorting  return $dataset;  } }

And then we have our client that is going to use any strategy

class Sorter {  protected $sorter;  public function __construct(SortStrategy $sorter)  {  $this->sorter = $sorter;  }  public function sort(array $dataset): array  {  return $this->sorter->sort($dataset);  } }

And it can be used as

$dataset = [1, 5, 4, 3, 2, 8]; $sorter = new Sorter(new BubbleSortStrategy()); $sorter->sort($dataset); // Output : Sorting using bubble sort $sorter = new Sorter(new QuickSortStrategy()); $sorter->sort($dataset); // Output : Sorting using quick sort

One thought on “Strategy Pattern

Leave a comment