Introduction
The Strategy Pattern is a behavioral design pattern that makes it possible to choose an algorithm's implementation at runtime. It defines a family of algorithms, encapsulates them all independently, and establishes interoperability between them. The design lets the algorithm change without relying on the clients using it.
Motivation
In situations where there are multiple ways to execute a particular operation, the Strategy Pattern becomes quite useful to help choose among these based on certain dynamic conditions. It allows defining a family of algorithms and making them interchangeable without altering the client code.
Implementation
Considering an e-commerce store is building a system that can accept multiple modes of payment, the use case remains the same, which is to pay a particular amount. The choice is between two possible modes, as shown in the example:
1.Pay with a credit card.
2.Pay with cryptocurrency.
Depending on the option selected by the customer, which is the condition, the strategy pattern comes into play to choose between these conditions. So, for the e-commerce store, it can use either card payment or crypto payment
package strategy; public interface PaymentStrategy { void pay(int amount); } public class CreditCardPayment implements PaymentStrategy { private String cardNumber; private String name; public CreditCardPayment(String cardNumber, String name) { this.cardNumber = cardNumber; this.name = name; } @Override public void pay(int amount) { System.out.println("Paid " + amount + " using the credit card: " + cardNumber); } } public class CryptoCurrencyPayment implements PaymentStrategy { private String walletAddress; public CryptoCurrencyPayment(String walletAddress) { this.walletAddress = walletAddress; } @Override public void pay(int amount) { System.out.println("Paid " + amount + " using cryptocurrency to the wallet: " + walletAddress); } } package strategy; public class ShoppingCart { private PaymentStrategy paymentStrategy; public void setPaymentStrategy(PaymentStrategy paymentStrategy) { this.paymentStrategy = paymentStrategy; } public void checkout(int amount) { paymentStrategy.pay(amount); } } ShoppingCart amazonCart = new ShoppingCart(); // Customer chooses to pay using credit card amazonCart.setPaymentStrategy(new CreditCardPayment("1234-5678-9876-5432", "John Doe")); amazonCart.checkout(500); // Customer chooses to pay using cryptocurrency amazonCart.setPaymentStrategy(new CryptoCurrencyPayment("1A2b3C4d5E6F")); amazonCart.checkout(300); Advantages:
- Allows choosing from a family of algorithms dynamically.
- Encapsulates the algorithm details from the client that uses it, making the system more modular and easier to maintain.
- Promotes the reuse of the strategy implementations across different contexts.
Disadvantages:
- This can increase the number of objects in the system.
- The client must be aware of the possible strategies to choose the appropriate one.
Usecase
An e-commerce application where the payment method can be chosen at runtime is a typical use case for the Strategy Pattern. The application can offer various payment strategies such as credit card, or cryptocurrency, and the user can choose any of these to make a payment.
Conclusion
The Strategy Pattern offers a way to manage algorithm implementations more efficiently, promoting flexibility and maintainability. It allows the easy addition of new strategies and modification of existing ones without affecting the client code, making the system more adaptable to changes.
