Strategy design pattern is used when we have multiple ways of doing a specific task and we want to choose any one of them at runtime. Best example is choosing a payment method on e-commerce website.
Scalability is a problem when we do not use design pattern. We need to modify our existing code incase we want to add a new payment method, for example NEFT payment.
5 real world Strategy Design Pattern
Payment Processing Systems: Different payment methods (credit card, PayPal, bank transfer) can be implemented as strategies.
Sorting Algorithms: Different sorting algorithms (bubble sort, merge sort, quick sort) can be implemented as strategies, allowing the system to choose the most appropriate algorithm based on the size and nature of the data.
Navigation Systems: Different routing strategies (fastest route, shortest route, avoiding toll roads) can be implemented to provide users with various navigation options.
Cache Management Systems: Different cache eviction policies (LRU, FIFO, LFU) can be implemented as strategies to manage cache contents and optimize performance based on access patterns.
Investment Strategies in Finance: Different investment strategies (growth investing, value investing, index investing) can be implemented as strategies to manage investment portfolios based on risk tolerance and financial goals.
Without Design Pattern
public class PaymentProcessor {
public void processPayment(String paymentMethod, double amount){
if(paymentMethod.equals("CreditCard")){
System.out.println("Processing credit card payment of amount: "+ amount);
} else if(paymentMethod.equals("PayPal")){
System.out.println("Processing PayPal payment of amount: " + amount);
} else if(paymentMethod.equals("GooglePay")){
System.out.println("Processing GooglePay payment of amount: "+amount);
} else{
System.out.println("Invalid Payment Method");
}
}
}
public class Main {
public static void main(String[] args) {
PaymentProcessor paymentProcessor = new PaymentProcessor();
paymentProcessor.processPayment("CreditCard", 100.00);
paymentProcessor.processPayment("PayPal", 80.00);
paymentProcessor.processPayment("GooglePay", 150.00);
}
}
With Strategy Design Pattern
public interface PaymentStrategy {
void processPayment(double amount);
}
public class CreditCardPaymentStrategy implements PaymentStrategy {
@Override
public void processPayment(double amount) {
System.out.println("Processing CreditCard Payment of amount: "+ amount);
}
}
public class GooglePayPaymentStrategy implements PaymentStrategy {
@Override
public void processPayment(double amount) {
System.out.println("Processing GooglePay payment of amount: "+ amount);
}
}
public class PayPalPaymentStrategy implements PaymentStrategy{
@Override
public void processPayment(double amount) {
System.out.println("Processing PayPal payment of amount: "+ amount);
}
}
public class User {
PaymentStrategy paymentStrategy;
public User(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStrategy;
}
public void buyNow(double amount){
paymentStrategy.processPayment(amount);
}
}
public class Main {
public static void main(String[] args) {
CreditCardPaymentStrategy creditCardPaymentStrategy = new CreditCardPaymentStrategy();
User user1= new User(creditCardPaymentStrategy);
user1.buyNow(100.00);
PayPalPaymentStrategy payPalPaymentStrategy = new PayPalPaymentStrategy();
User user2= new User(payPalPaymentStrategy);
user2.buyNow(80.00);
GooglePayPaymentStrategy googlePayPaymentStrategy = new GooglePayPaymentStrategy();
User user3= new User(googlePayPaymentStrategy);
user3.buyNow(150.00);
}
}