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
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
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.terminal
1
2
3
publicinterface PaymentStrategy {
void pay(double amount);
}
➜~java
UTF-83 lines● ready
Credit Card Payment
java.terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
publicinterface CreditCardPayment {
privateString name;
privateString cardNumber;
privateString cvv;
privateString expiryDate;
public CreditCardPayment(String name, String cardNumber, String cvv, String expiryDate){
this.name = name;
this.cardNumber = cardNumber;
this.cvv = cvv;
this.expiryDate = expiryDate;
}
@Override
publicvoid pay(double amount){
System.out.println("Processing $" + amount + " payment with Credit Card");
}
}
➜~java
UTF-818 lines● ready
GooglePay Payment
java.terminal
1
2
3
4
5
6
7
8
9
10
11
12
publicinterface GooglePayPayment {
privateString pin;
public GooglePayPayment(String pin){
this.pin = pin;
}
@Override
publicvoid pay(double amount){
System.out.println("Processing $" + amount + " payment with Google Pay");
}
}
➜~java
UTF-812 lines● ready
PayPal Payment
java.terminal
1
2
3
4
5
6
7
8
9
10
11
12
publicinterface PayPalPayment {
privateString pin;
public PayPalPayment(String pin){
this.pin = pin;
}
@Override
publicvoid pay(double amount){
System.out.println("Processing $" + amount + " payment with PayPal");
}
}
➜~java
UTF-812 lines● ready
PaymentProcessor
java.terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%% This will have the context where the different strategies will be injected based on the input %%
publicinterface PaymentProcessor {
private PaymentStrategy strategy;
public PaymentProcessor(PaymentStrategy strategy){
this.strategy = strategy;
}
publicvoid setStrategy(PaymentStrategy strategy){
this.strategy = strategy;
}
publicvoid processPayment(double amount){
strategy.pay(amount);
}
}
➜~java
UTF-816 lines● ready
Main Application Code
java.terminal
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
publicclass Main {
publicstaticvoid 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){
case1:
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;
case2:
System.out.print("Enter your pin: ");
String pin = sc.nextLine();
strategy = new GooglePayPayment();
break;
case3:
System.out.print("Enter your pin: ");
String pin = sc.nextLine();
strategy = new PayPalPayment();
break;
}
PaymentProcessor processor = new PaymentProcessor(