Low Level DesignAug 19, 20263 min read

Strategy Design Pattern

Deep dive into the Strategy Design Pattern with real-world examples

So many a times what happens is that we go on to a platform and then we are trying to do a payment. Then we get a multiple options like okay there is a Google Pay, credit card, debit card, Paypal option, right? The page looks something like this.

Payment Methods
Payment Methods
So, here what the Strategy Design Pattern says that we will only have one function which is pay() and internally we will have the options to choose between the different payment options. It is a behavioural design pattern that allows you to choose different algorithms or behaviours at runtime.
This is how the interface would look like:
Strategy Pattern
Strategy Pattern

The thing here is if we don't use the Strategy Pattern, we will have to create all the functions before hand with concrete algorithms. This will cause a problem called Class Explosion as we will have created many classes here.

This is a clean code example for it

Payment Strategy Class
java
public interface PaymentStrategy {
	void pay(double amount);
}
Credit Card Payment
java
public interface CreditCardPayment {
	private String name;
	private String cardNumber;
	private String cvv;
	private String expiryDate;
	
	public CreditCardPayment(String name, String cardNumber, String cvv, String expiryDate){
		this.name = name;
		this.cardNumber = cardNumber;
		this.cvv = cvv;
		this.expiryDate = expiryDate;
	}
	
	@Override
	public void pay(double amount){
		System.out.println("Processing $" + amount + " payment with Credit Card");
	}
}
GooglePay Payment
java
public interface GooglePayPayment {
	private String pin;
	
	public GooglePayPayment(String pin){
		this.pin = pin;
	}
	
	@Override
	public void pay(double amount){
		System.out.println("Processing $" + amount + " payment with Google Pay");
	}
}
PayPal Payment
java
public interface PayPalPayment {
	private String pin;
	
	public PayPalPayment(String pin){
		this.pin = pin;
	}
	
	@Override
	public void pay(double amount){
		System.out.println("Processing $" + amount + " payment with PayPal");
	}
}
PaymentProcessor
java
%% This will have the context where the different strategies will be injected based on the input %%
public interface PaymentProcessor {
	private PaymentStrategy strategy;
	
	public PaymentProcessor(PaymentStrategy strategy){
		this.strategy = strategy;
	}
	
	public void setStrategy(PaymentStrategy strategy){
		this.strategy = strategy;
	}
	
	public void processPayment(double amount){
		strategy.pay(amount);
	}
}
Main Application Code
java
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		double amount = 100;
		
		System.out.println("Select Payment Method");
		System.out.println("1. Credit Card");
		System.out.println("2. Google Pay");
		System.out.println("3. PayPal");
		int choice = sc.nextInt();
		sc.nextLine();
		
		PaymentStrategy strategy = null;
		
		switch(choice){
			case 1:
				System.out.print("Enter your name: ");
				String name = sc.nextLine();
				System.out.print("Enter your card number: ");
				String card = sc.nextLine();
				System.out.print("Enter your CVV: ");
				String cvv = sc.nextLine();
				System.out.print("Enter your name: ");
				strategy = new CreditCardPayment();
				break;
				
			case 2:
				System.out.print("Enter your pin: ");
				String pin = sc.nextLine();
				strategy = new GooglePayPayment();
				break;
				
			case 3:
				System.out.print("Enter your pin: ");
				String pin = sc.nextLine();
				strategy = new PayPalPayment();
				break;
		
		}
		
		PaymentProcessor processor = new PaymentProcessor(
		strategy);
		processor.processPayment(amount);
		
		sc.close();
	}
}