Overview
This article is part of the Learning Design Patterns with PHP Advent Calendar 2018.
This time, I will write about the Strategy pattern.
What is the Strategy Pattern?
The Strategy pattern is a pattern that makes it easy to switch algorithms. By defining different processes in separate classes, not only can you dynamically select processes, but you can also reduce conditional branches. It is one of the patterns faithful to the OCP (open/closed principle).
Implementation Example
Let's look at a simple example of implementing the Strategy pattern.
<?php
class Context
{
private $notification;
public function __construct(NotificationInterface $notification)
{
$this->notification = $notification;
}
public function execute()
{
return $this->notification->notify();
}
}
interface NotificationInterface
{
public function notify();
}
class SlackNotification implements NotificationInterface
{
public function __construct($message)
{
$this->message = $message;
}
public function notify()
{
echo $this->message . '- sent by Slack';
}
}
class EmailNotification implements NotificationInterface
{
public function __construct($message)
{
$this->message = $message;
}
public function notify()
{
echo $this->message . '- sent by Email';
}
}
$message = "Hello World!";
$slack = new SlackNotification($message);
$context = new Context($slack);
$context->execute(); // Hello World - sent by Slack
$email = new EmailNotification($message);
$context = new Context($email);
$context->execute(); // Hello World - sent by Email
By making the implementation of the Context class dependent on the interface, the "strategy" can be switched from the client side. Isn't this a common implementation?
Since behaviors are separated, it should follow the OCP. For extensions, you just need to add an implementation of NotificationInterface, and for modifications, you just need to modify the implementation of NotificationInterface.
Thoughts
I realized that sometimes I use GoF without even knowing it, so I thought it wouldn't hurt to remember it properly.
References
- Wikipedia - Strategy Pattern
- Do You PHP Hatena - [doyouphp][phpdp] Introduction to Design Patterns with PHP - Switching Strategies
- isseium's blog - Design Patterns with PHP
Strategy Pattern - DesignPatternsPHP - Strategy
- Quick Corporation's Web Service Development Blog - Let's Consider the Strategy Pattern with PHP