@edmond_brakus
The Strategy pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. This pattern enables you to select an algorithm at runtime and vary it independently from the client that uses it. Here's an example of how you can implement the Strategy pattern in PHP:
First, define an interface that all strategy classes will implement:
1 2 3 |
interface PaymentMethod { public function pay($amount); } |
Next, create concrete strategy classes that implement the PaymentMethod
interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class CreditCardPayment implements PaymentMethod { public function pay($amount) { echo "Paying with Credit Card: $" . $amount . " "; // Logic to process payment with a credit card } } class PayPalPayment implements PaymentMethod { public function pay($amount) { echo "Paying with PayPal: $" . $amount . " "; // Logic to process payment with PayPal } } |
Then, create a context class that will use the strategy objects:
1 2 3 4 5 6 7 8 9 10 11 |
class Order { private $paymentMethod; public function __construct(PaymentMethod $paymentMethod) { $this->paymentMethod = $paymentMethod; } public function process($amount) { $this->paymentMethod->pay($amount); } } |
Finally, create client code that uses the context object and passes the desired strategy object:
1 2 3 4 5 6 7 |
$creditCardPayment = new CreditCardPayment(); $order = new Order($creditCardPayment); $order->process(100); $paypalPayment = new PayPalPayment(); $order = new Order($paypalPayment); $order->process(50); |
In this example, the Order
class is the context class that uses the PaymentMethod
interface to process payments. The CreditCardPayment
and PayPalPayment
classes are concrete strategy classes that implement the PaymentMethod
interface. The client code creates an instance of the context object (Order
) and passes the desired strategy object (CreditCardPayment
or PayPalPayment
) to it. The process
method of the Order
class calls the pay
method of the selected strategy object.
By using the Strategy pattern, you can easily swap out one payment method for another without changing the code that uses it. This makes your code more flexible and easier to maintain.
@edmond_brakus
The Strategy pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. The main goal of this pattern is to separate the implementation details of an algorithm from the client code that uses it.
In PHP, you can implement the Strategy pattern by following these steps:
Step 1: Create an interface for the strategies
First, create an interface that defines the contract for all the strategies:
1 2 3 |
interface PaymentStrategy { public function pay($amount); } |
This interface defines a single method pay()
that takes the amount to be paid as a parameter.
Step 2: Implement the strategies
Next, create concrete classes that implement the PaymentStrategy
interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class CreditCardStrategy implements PaymentStrategy { private $creditCardNumber; private $expirationDate; private $cvv; public function __construct($creditCardNumber, $expirationDate, $cvv) { $this->creditCardNumber = $creditCardNumber; $this->expirationDate = $expirationDate; $this->cvv = $cvv; } public function pay($amount) { // Code to process payment via credit card } } class PayPalStrategy implements PaymentStrategy { private $email; private $password; public function __construct($email, $password) { $this->email = $email; $this->password = $password; } public function pay($amount) { // Code to process payment via PayPal } } |
Each concrete strategy implements the pay()
method according to its own logic. In this example, we have two strategies: CreditCardStrategy
and PayPalStrategy
.
Step 3: Create the context
Next, create the context class that uses the strategies:
1 2 3 4 5 6 7 8 9 10 11 |
class PaymentContext { private $strategy; public function __construct(PaymentStrategy $strategy) { $this->strategy = $strategy; } public function processPayment($amount) { $this->strategy->pay($amount); } } |
The PaymentContext
class has a reference to a PaymentStrategy
object, which is set through the constructor. It also has a method processPayment()
that delegates the payment processing to the current strategy.
Step 4: Use the context to perform payments
Finally, you can use the PaymentContext
class to perform payments:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Create a CreditCardStrategy object and pass it to the PaymentContext $creditCardStrategy = new CreditCardStrategy('1234 5678 9012 3456', '12/23', '123'); $paymentContext = new PaymentContext($creditCardStrategy); // Process the payment using the current strategy $paymentContext->processPayment(100); // Change the strategy to PayPal $payPalStrategy = new PayPalStrategy('[email protected]', 'password'); $paymentContext->setStrategy($payPalStrategy); // Process another payment using the new strategy $paymentContext->processPayment(50); |
In this example, we first create a CreditCardStrategy
object and pass it to the PaymentContext
constructor. Then, we use the processPayment()
method to process a payment of $100 using the credit card strategy.
Next, we change the current strategy to PayPalStrategy
by creating a new object and calling the setStrategy()
method on the PaymentContext
object. Finally, we use the processPayment()
method again to process another payment of $50 using the PayPal strategy.
This demonstrates the flexibility of the Strategy pattern, as the client code can switch between different strategies at runtime without