The Strategy pattern helps you change how a program works by swapping different methods easily. It keeps your code clean and flexible.
0
0
Strategy pattern in PHP
Introduction
When you want to choose between different ways to do a task while the program runs.
When you want to avoid big if-else or switch statements deciding what to do.
When you want to add new ways to do something without changing existing code.
When different parts of your program need to do the same job in different ways.
When you want to test different behaviors separately.
Syntax
PHP
<?php interface Strategy { public function execute(string $data): string; } class ConcreteStrategyA implements Strategy { public function execute(string $data): string { return strtoupper($data); } } class ConcreteStrategyB implements Strategy { public function execute(string $data): string { return strtolower($data); } } class Context { private Strategy $strategy; public function __construct(Strategy $strategy) { $this->strategy = $strategy; } public function setStrategy(Strategy $strategy): void { $this->strategy = $strategy; } public function doAction(string $data): string { return $this->strategy->execute($data); } }
The Strategy interface defines a method all strategies must implement.
The Context class uses a strategy object to perform its task.
Examples
This example shows two strategies: one to make text uppercase, another lowercase. The context uses the uppercase strategy first.
PHP
<?php interface Strategy { public function execute(string $data): string; } class UpperCaseStrategy implements Strategy { public function execute(string $data): string { return strtoupper($data); } } class LowerCaseStrategy implements Strategy { public function execute(string $data): string { return strtolower($data); } } class Context { private Strategy $strategy; public function __construct(Strategy $strategy) { $this->strategy = $strategy; } public function doAction(string $data): string { return $this->strategy->execute($data); } } $context = new Context(new UpperCaseStrategy()); echo $context->doAction("Hello World");
Here, the context switches to the lowercase strategy and processes the same text differently.
PHP
<?php $context = new Context(new LowerCaseStrategy()); echo $context->doAction("Hello World");
Sample Program
This program shows how the context uses two different strategies to change the text. First, it makes the text uppercase, then lowercase.
PHP
<?php interface Strategy { public function execute(string $data): string; } class UpperCaseStrategy implements Strategy { public function execute(string $data): string { return strtoupper($data); } } class LowerCaseStrategy implements Strategy { public function execute(string $data): string { return strtolower($data); } } class Context { private Strategy $strategy; public function __construct(Strategy $strategy) { $this->strategy = $strategy; } public function setStrategy(Strategy $strategy): void { $this->strategy = $strategy; } public function doAction(string $data): string { return $this->strategy->execute($data); } } $context = new Context(new UpperCaseStrategy()); echo $context->doAction("Hello Strategy Pattern!") . "\n"; $context->setStrategy(new LowerCaseStrategy()); echo $context->doAction("Hello Strategy Pattern!") . "\n";
OutputSuccess
Important Notes
Each strategy class must follow the same interface so the context can use them interchangeably.
You can add new strategies without changing the context code.
Use the Strategy pattern to keep your code easy to read and maintain.
Summary
The Strategy pattern lets you swap different ways to do a task easily.
It helps avoid complex if-else statements by using separate classes.
The context class uses a strategy object to perform actions.