Understanding the Strategy Design Pattern in PHP with a Simple Example

Hashemi Rafsan
3 min readNov 11, 2023

The Strategy design pattern is a behavioral design pattern that allows an algorithm to vary independently from clients that use it. In other words, it allows you to choose which algorithm to use at runtime, rather than having to hardwire it into your code.

The Strategy pattern is often used to implement different ways of doing the same thing, such as different ways to send notifications. For example, you might want to be able to send notifications via Email, SMS, or FCM. The Strategy pattern allows you to easily switch between these different notification methods without having to modify your code.

Example:

The following PHP code shows a simple example of how to use the Strategy pattern to send notifications:

interface Sendable
{
public function send(): void;
}

class Mail implements Sendable
{
public function send(): void
{
echo "Notification send from: Mail \n";
}
}

class FCM implements Sendable
{
public function send(): void
{
echo "Notification send from: FCM \n";
}
}

class SMS implements Sendable
{
public function send(): void
{
echo "Notification send from: SMS \n";
}
}

class Notification
{
public Sendable $sendable;
public function setSendable(Sendable $sendable): self
{
$this->sendable = $sendable;
return $this;
}
public function notify()
{
return $this->sendable->send();
}
}

class SendVerificationEmail extends Notification {}
class SendAnnouncementFCM extends Notification {}
class SendOtpSMS extends Notification {}

$email = new Mail();
(new SendVerificationEmail())->setSendable($email)->notify(); // Notification send from: Mail

$fcm = new FCM();
(new SendAnnouncementFCM())->setSendable($fcm)->notify(); // Notification send from: FCM

$sms = new SMS();
(new SendOtpSMS())->setSendable($sms)->notify(); // Notification send from: SMS

In this example, we have a Sendable interface that defines a single method, send(). We then have three concrete implementations of the Sendable interface: Mail, FCM, and SMS. These concrete implementations provide different ways to send notifications.

The Notification class is the client code that uses the Strategy pattern to send notifications. The Notification class has a sendable property that stores a reference to a Sendable object. The notify() method simply calls the send() method on the sendable object.

To send a notification, we first create a Notification object and then pass it a concrete implementation of the Sendable interface. For example, to send a verification email, we would do the following:

$email = new Mail();
(new SendVerificationEmail())->setSendable($email)->notify();

This would create a new SendVerificationEmail object and set its sendable property to the Mail object. The notify() method would then call the send() method on the Mail object, which would send the verification email.

// Notification send from: Mail

Benefits of Using the Strategy Pattern:

The Strategy pattern offers a number of benefits, including:

  • Flexibility: The Strategy pattern allows you to easily change the algorithm that is used at runtime. This can be useful if you need to support multiple algorithms or if you need to change the algorithm based on the context.
  • Reusability: The Strategy pattern allows you to reuse the same algorithm in different contexts. This can help to reduce code duplication and make your code more maintainable.
  • Testability: The Strategy pattern makes it easier to test your code. You can create separate tests for each concrete implementation of the Strategy interface, which can help to ensure that your code is working correctly.

Conclusion:

The Strategy design pattern is a powerful tool that can be used to implement different ways of doing the same thing. It is a flexible, reusable, and testable pattern that can help you to write better code.

N.B: I took help to write this blog from Google Bard

You can follow me on

https://twitter.com/RafsanHashemi

https://rafsan.substack.com/

--

--