Using the Strategy Pattern in PHP to Send Notifications
An overview of the Strategy design pattern demonstrates how to encapsulate notification-sending algorithms in independent PHP classes, allowing runtime selection among email, SMS, or FCM implementations without modifying client code, and highlights benefits such as flexibility, reusability, and testability.
Strategy pattern is a behavioral design pattern that encapsulates algorithms in separate classes, allowing the client to vary behavior without changing its own code. In other words, it lets the client choose which algorithm to use at runtime.
The pattern is often used for interchangeable behaviors such as different notification channels (email, SMS, FCM), enabling easy switching between them.
Example
The following PHP code demonstrates using the Strategy pattern to send notifications:
<code>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 sent from: Mail
$fcm = new FCM();
(new SendAnnouncementFCM())->setSendable($fcm)->notify(); // Notification sent from: FCM
$sms = new SMS();
(new SendOtpSMS())->setSendable($sms)->notify(); // Notification sent from: SMS</code>In this example, a Sendable interface defines a single send() method. Three concrete implementations— Mail , FCM , and SMS —provide different ways to send notifications.
The Notification class acts as the client, holding a Sendable reference and delegating the notify() call to the selected strategy’s send() method.
To send a notification, create a Notification object (or a subclass) and inject the desired Sendable implementation. For example, to send a verification email:
<code>$email = new Mail();
(new SendVerificationEmail())->setSendable($email)->notify(); // Notification sent from: Mail</code>This creates a SendVerificationEmail object, sets its sendable property to a Mail instance, and calls notify() , which invokes Mail::send() to dispatch the email.
Benefits of Using the Strategy Pattern
The Strategy pattern offers several advantages:
Flexibility: Algorithms are encapsulated in independent classes, allowing dynamic changes at runtime.
Reusability: Encapsulated algorithms can be reused across different contexts, reducing duplication.
Testability: Each algorithm can be tested in isolation, improving overall code reliability.
Overall, the Strategy pattern provides a powerful way to implement interchangeable algorithms with high flexibility, reusability, and testability, helping developers write more elegant and maintainable code.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.