Concept Flow - Strategy pattern
Client code
Choose strategy
Set strategy object
Call strategy method
Strategy executes algorithm
Return result to client
The client picks a strategy object, sets it, then calls its method to run the chosen algorithm.
<?php interface Strategy { public function doOperation(int $a, int $b): int; } class AddStrategy implements Strategy { public function doOperation(int $a, int $b): int { return $a + $b; } } class Context { private Strategy $strategy; public function __construct(Strategy $strategy) { $this->strategy = $strategy; } public function executeStrategy(int $a, int $b): int { return $this->strategy->doOperation($a, $b); } } $addStrategy = new AddStrategy(); $context = new Context($addStrategy); echo $context->executeStrategy(5, 3); // Outputs 8 ?>
| Step | Action | Object/Variable | Value/Result | Explanation |
|---|---|---|---|---|
| 1 | Create AddStrategy object | $addStrategy | AddStrategy instance | AddStrategy object created to perform addition |
| 2 | Create Context with AddStrategy | $context | Context instance with AddStrategy | Context stores the strategy object |
| 3 | Call executeStrategy(5, 3) | executeStrategy | Calls AddStrategy->doOperation(5,3) | Context delegates operation to strategy |
| 4 | AddStrategy->doOperation(5,3) | doOperation | 8 | AddStrategy adds 5 + 3 and returns 8 |
| 5 | executeStrategy returns | executeStrategy | 8 | Context returns the result from strategy |
| 6 | echo output | Output | 8 | Program prints 8 to the screen |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| $addStrategy | null | AddStrategy instance | AddStrategy instance | AddStrategy instance | AddStrategy instance | AddStrategy instance |
| $context | null | null | Context instance with AddStrategy | Context instance with AddStrategy | Context instance with AddStrategy | Context instance with AddStrategy |
| Result | null | null | null | null | 8 | 8 |
Strategy pattern lets you choose an algorithm at runtime. Define a Strategy interface with a method. Create concrete Strategy classes implementing the interface. Context holds a Strategy object and calls its method. Change strategy to change behavior without changing Context.
What is the main purpose of the Strategy pattern in PHP?
Which of the following is the correct way to define a strategy interface in PHP?
?interface, and methods inside must be declared without a body and with visibility (usually public).What will be the output of the following PHP code using the Strategy pattern?
interface Strategy {
public function execute();
}
class AddStrategy implements Strategy {
public function execute() {
return 5 + 3;
}
}
class MultiplyStrategy implements Strategy {
public function execute() {
return 5 * 3;
}
}
class Context {
private Strategy $strategy;
public function __construct(Strategy $strategy) {
$this->strategy = $strategy;
}
public function executeStrategy() {
return $this->strategy->execute();
}
}
$context = new Context(new MultiplyStrategy());
echo $context->executeStrategy();
new MultiplyStrategy(), so calling executeStrategy() returns 15.Identify the error in the following PHP code implementing the Strategy pattern:
interface Strategy {
public function execute();
}
class ConcreteStrategy implements Strategy {
public function execute() {
echo "Running strategy";
}
}
class Context {
private $strategy;
public function __construct(Strategy $strategy) {
$this->strategy = $strategy;
}
public function executeStrategy() {
$this->strategy->execute;
}
}
$context = new Context(new ConcreteStrategy());
$context->executeStrategy();
executeStrategy(), the code calls $this->strategy->execute; without parentheses, so the method is not actually called.$this->strategy->execute();.You want to add a new strategy SubtractStrategy to the existing PHP Strategy pattern code. Which of the following is the best way to implement it?
interface Strategy {
public function execute(int $a, int $b): int;
}
class AddStrategy implements Strategy {
public function execute(int $a, int $b): int {
return $a + $b;
}
}
// Your code here
execute(int $a, int $b): int with type hints and return type.Pass the test and this pattern moves unknown