Consider the following PHP code implementing a simple Strategy pattern for sorting an array. What will be the output when running this code?
<?php interface SortStrategy { public function sort(array $data): array; } class BubbleSort implements SortStrategy { public function sort(array $data): array { $n = count($data); for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n - $i - 1; $j++) { if ($data[$j] > $data[$j + 1]) { $temp = $data[$j]; $data[$j] = $data[$j + 1]; $data[$j + 1] = $temp; } } } return $data; } } class ReverseSort implements SortStrategy { public function sort(array $data): array { rsort($data); return $data; } } class Sorter { private SortStrategy $strategy; public function __construct(SortStrategy $strategy) { $this->strategy = $strategy; } public function setStrategy(SortStrategy $strategy): void { $this->strategy = $strategy; } public function sort(array $data): array { return $this->strategy->sort($data); } } $data = [3, 1, 4, 1, 5]; $sorter = new Sorter(new BubbleSort()); $result1 = $sorter->sort($data); $sorter->setStrategy(new ReverseSort()); $result2 = $sorter->sort($data); print_r($result1); print_r($result2); ?>
Think about what each strategy does to the array and the order of operations.
The BubbleSort strategy sorts the array in ascending order, so the first print_r shows the sorted array ascending. The ReverseSort strategy sorts the array in descending order, so the second print_r shows the array sorted descending.
Which of the following best explains the main benefit of using the Strategy pattern in PHP?
Think about flexibility and how the pattern helps with changing behavior.
The Strategy pattern lets you swap algorithms at runtime by encapsulating them in separate classes and using composition. This avoids modifying the main object and promotes flexibility.
Examine the following PHP code snippet using the Strategy pattern. What error will it produce when run?
<?php interface PaymentStrategy { public function pay(float $amount): string; } class CreditCardPayment implements PaymentStrategy { public function pay(float $amount): string { return "Paid $amount using credit card."; } } class PaypalPayment implements PaymentStrategy { public function pay(float $amount): string { return "Paid $amount using PayPal."; } } class PaymentContext { private PaymentStrategy $strategy; public function __construct(PaymentStrategy $strategy) { $this->strategy = $strategy; } public function setStrategy(PaymentStrategy $strategy): void { $this->strategy = $strategy; } public function pay(float $amount): string { return $this->strategy->pay($amount); } } $context = new PaymentContext(new CreditCardPayment()); echo $context->pay(100); $context->setStrategy(new PaypalPayment()); echo $context->pay(50); $context->setStrategy(new class implements PaymentStrategy { public function pay(float $amount): string { return "Paid $amount using Bitcoin."; } }); echo $context->pay(25); ?>
Check the anonymous class passed to setStrategy and its relation to the interface.
The anonymous class implements the PaymentStrategy interface, so no error occurs and the output is as expected.
Given the interface LoggerStrategy with method log(string $message): void, which option correctly implements this interface?
Check method signatures and interface implementation rules.
Option B correctly implements the interface method with matching name, parameter type, and return type. Option B changes parameter type to int, Option B changes return type, C does not implement interface.
Analyze the following PHP code using the Strategy pattern. How many distinct strategy classes are used to perform operations?
<?php interface OperationStrategy { public function execute(int $a, int $b): int; } class AddOperation implements OperationStrategy { public function execute(int $a, int $b): int { return $a + $b; } } class MultiplyOperation implements OperationStrategy { public function execute(int $a, int $b): int { return $a * $b; } } class Calculator { private OperationStrategy $strategy; public function __construct(OperationStrategy $strategy) { $this->strategy = $strategy; } public function setStrategy(OperationStrategy $strategy): void { $this->strategy = $strategy; } public function calculate(int $a, int $b): int { return $this->strategy->execute($a, $b); } } $calc = new Calculator(new AddOperation()); $sum = $calc->calculate(5, 3); $calc->setStrategy(new MultiplyOperation()); $product = $calc->calculate(5, 3); $calc->setStrategy(new class implements OperationStrategy { public function execute(int $a, int $b): int { return $a - $b; } }); $difference = $calc->calculate(5, 3);
Count all classes implementing the OperationStrategy interface, including anonymous classes.
There are three distinct strategy implementations: AddOperation, MultiplyOperation, and an anonymous class implementing subtraction.