-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStrategyPattern.java
More file actions
377 lines (329 loc) · 10.7 KB
/
StrategyPattern.java
File metadata and controls
377 lines (329 loc) · 10.7 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.dipdeveloper.designpatterns;
/**
* Strategy Pattern - Behavioral Pattern
*
* Problem: Different payment methods (Credit Card, UPI, Wallet, NetBanking)
* Solution: Encapsulate each payment algorithm in separate class
* Use interface to make them interchangeable
*
* Benefits:
* - Easy to add new payment methods
* - Runtime algorithm selection
* - Reduces code complexity (no if-else chains)
* - Easy to test each strategy independently
*/
// ============= Strategy Interface =============
/**
* Strategy interface - defines contract for all payment strategies
*/
public interface PaymentStrategy {
/**
* Process payment using specific strategy
* @param amount - Amount to pay
* @return true if payment successful, false otherwise
*/
boolean pay(double amount);
/**
* Get payment method name
* @return payment method name
*/
String getPaymentMethod();
}
// ============= Concrete Strategies =============
/**
* Concrete Strategy 1: Credit Card Payment
*/
public class CreditCardPayment implements PaymentStrategy {
private String cardNumber;
private String cvv;
private String expiryDate;
public CreditCardPayment(String cardNumber, String cvv, String expiryDate) {
this.cardNumber = cardNumber;
this.cvv = cvv;
this.expiryDate = expiryDate;
}
@Override
public boolean pay(double amount) {
// Validate card details
if (!isCardValid()) {
System.out.println("Invalid card details!");
return false;
}
// Simulate payment processing
System.out.println("Processing Credit Card Payment");
System.out.println("Amount: " + amount);
System.out.println("Card: ****" + cardNumber.substring(cardNumber.length() - 4));
System.out.println("Payment successful!");
return true;
}
@Override
public String getPaymentMethod() {
return "CREDIT_CARD";
}
private boolean isCardValid() {
return cardNumber != null && cvv != null && expiryDate != null;
}
}
/**
* Concrete Strategy 2: UPI Payment (BHIM, Google Pay, PhonePe)
*/
public class UPIPayment implements PaymentStrategy {
private String upiId;
private String pin;
public UPIPayment(String upiId, String pin) {
this.upiId = upiId;
this.pin = pin;
}
@Override
public boolean pay(double amount) {
// Validate UPI
if (!isUPIValid()) {
System.out.println("Invalid UPI ID or PIN!");
return false;
}
// Simulate UPI payment
System.out.println("Processing UPI Payment");
System.out.println("Amount: " + amount);
System.out.println("UPI ID: " + upiId);
System.out.println("Generating OTP...");
System.out.println("OTP verified!");
System.out.println("Payment successful!");
return true;
}
@Override
public String getPaymentMethod() {
return "UPI";
}
private boolean isUPIValid() {
return upiId != null && upiId.contains("@") && pin != null;
}
}
/**
* Concrete Strategy 3: Wallet Payment (Paytm, Amazon Pay)
*/
public class WalletPayment implements PaymentStrategy {
private double walletBalance;
private String walletId;
public WalletPayment(String walletId, double initialBalance) {
this.walletId = walletId;
this.walletBalance = initialBalance;
}
@Override
public boolean pay(double amount) {
// Check sufficient balance
if (walletBalance < amount) {
System.out.println("Insufficient wallet balance!");
System.out.println("Available: " + walletBalance + ", Required: " + amount);
return false;
}
// Deduct from wallet
walletBalance -= amount;
System.out.println("Processing Wallet Payment");
System.out.println("Amount: " + amount);
System.out.println("Wallet ID: " + walletId);
System.out.println("Remaining balance: " + walletBalance);
System.out.println("Payment successful!");
return true;
}
@Override
public String getPaymentMethod() {
return "WALLET";
}
public double getBalance() {
return walletBalance;
}
public void addBalance(double amount) {
walletBalance += amount;
}
}
/**
* Concrete Strategy 4: Net Banking Payment
*/
public class NetBankingPayment implements PaymentStrategy {
private String accountNumber;
private String bankCode;
public NetBankingPayment(String accountNumber, String bankCode) {
this.accountNumber = accountNumber;
this.bankCode = bankCode;
}
@Override
public boolean pay(double amount) {
// Validate account
if (!isAccountValid()) {
System.out.println("Invalid account details!");
return false;
}
// Simulate net banking payment
System.out.println("Processing Net Banking Payment");
System.out.println("Amount: " + amount);
System.out.println("Bank: " + bankCode);
System.out.println("Account: ****" + accountNumber.substring(accountNumber.length() - 4));
System.out.println("Redirecting to bank website...");
System.out.println("Payment successful!");
return true;
}
@Override
public String getPaymentMethod() {
return "NET_BANKING";
}
private boolean isAccountValid() {
return accountNumber != null && bankCode != null;
}
}
// ============= Context Class =============
/**
* Context: Payment Processor
* - Uses strategy to process payments
* - Can switch strategies dynamically at runtime
*/
public class PaymentProcessor {
private PaymentStrategy paymentStrategy;
private double totalAmount;
/**
* Set payment strategy (can be changed anytime)
*/
public void setPaymentStrategy(PaymentStrategy strategy) {
this.paymentStrategy = strategy;
}
/**
* Process payment using selected strategy
*/
public boolean processPayment(double amount) {
if (paymentStrategy == null) {
System.out.println("No payment strategy selected!");
return false;
}
System.out.println("\n===== Payment Processing =====");
System.out.println("Method: " + paymentStrategy.getPaymentMethod());
boolean result = paymentStrategy.pay(amount);
if (result) {
totalAmount += amount;
System.out.println("Total processed: " + totalAmount);
}
System.out.println("============================\n");
return result;
}
public double getTotalAmount() {
return totalAmount;
}
}
// ============= Demo: Using Strategy Pattern =============
/**
* Example: E-commerce checkout process
* Different users choose different payment methods
*/
public class StrategyPatternDemo {
public static void main(String[] args) {
PaymentProcessor processor = new PaymentProcessor();
// User 1: Pays with Credit Card
System.out.println("===== Customer 1: Credit Card Payment =====");
PaymentStrategy creditCard = new CreditCardPayment("1234567890123456", "123", "12/25");
processor.setPaymentStrategy(creditCard);
processor.processPayment(5000);
// User 2: Pays with UPI
System.out.println("\n===== Customer 2: UPI Payment =====");
PaymentStrategy upi = new UPIPayment("user@upi", "1234");
processor.setPaymentStrategy(upi);
processor.processPayment(3000);
// User 3: Pays with Wallet
System.out.println("\n===== Customer 3: Wallet Payment =====");
WalletPayment wallet = new WalletPayment("PAYTM123", 10000);
processor.setPaymentStrategy(wallet);
processor.processPayment(2000);
// User 4: Wallet has insufficient balance
System.out.println("\n===== Customer 4: Wallet (Insufficient Balance) =====");
WalletPayment wallet2 = new WalletPayment("PAYTM456", 500);
processor.setPaymentStrategy(wallet2);
processor.processPayment(1000); // Will fail
// User 5: Net Banking
System.out.println("\n===== Customer 5: Net Banking =====");
PaymentStrategy netBanking = new NetBankingPayment("987654321", "HDFC");
processor.setPaymentStrategy(netBanking);
processor.processPayment(4500);
System.out.println("\n===== Summary =====");
System.out.println("Total successfully processed: " + processor.getTotalAmount());
}
}
/*
* Output:
* ===== Customer 1: Credit Card Payment =====
* ===== Payment Processing =====
* Method: CREDIT_CARD
* Processing Credit Card Payment
* Amount: 5000.0
* Card: ****3456
* Payment successful!
* Total processed: 5000.0
* ============================
*
* ===== Customer 2: UPI Payment =====
* ===== Payment Processing =====
* Method: UPI
* Processing UPI Payment
* Amount: 3000.0
* UPI ID: user@upi
* Generating OTP...
* OTP verified!
* Payment successful!
* Total processed: 8000.0
* ============================
*
* ===== Customer 3: Wallet Payment =====
* ===== Payment Processing =====
* Method: WALLET
* Processing Wallet Payment
* Amount: 2000.0
* Wallet ID: PAYTM123
* Remaining balance: 8000.0
* Payment successful!
* Total processed: 10000.0
* ============================
*
* ===== Customer 4: Wallet (Insufficient Balance) =====
* ===== Payment Processing =====
* Method: WALLET
* Processing Wallet Payment
* Amount: 1000.0
* Wallet ID: PAYTM456
* Insufficient wallet balance!
* Available: 500.0, Required: 1000.0
* ============================
*
* ===== Customer 5: Net Banking =====
* ===== Payment Processing =====
* Method: NET_BANKING
* Processing Net Banking Payment
* Amount: 4500.0
* Bank: HDFC
* Account: ****4321
* Redirecting to bank website...
* Payment successful!
* Total processed: 14500.0
* ============================
*
* ===== Summary =====
* Total successfully processed: 14500.0
*/
/*
* Key Concepts:
*
* 1. ENCAPSULATION: Each payment method is encapsulated in its own class
*
* 2. INTERCHANGEABILITY: All strategies implement same interface
* Can swap strategies at runtime without changing client code
*
* 3. OPEN/CLOSED PRINCIPLE:
* - Open for extension (add new payment methods)
* - Closed for modification (existing code doesn't change)
*
* 4. NO CONDITIONAL LOGIC:
* Bad: if (type == CREDIT_CARD) {...} else if (type == UPI) {...}
* Good: strategy.pay(amount) // Works for all types
*
* 5. REAL-WORLD APPLICATIONS:
* - Payment methods (as shown above)
* - Sorting algorithms (merge sort, quick sort, bubble sort)
* - Compression algorithms (ZIP, RAR, 7Z)
* - Route finding (shortest path, fastest route, cheapest route)
* - Caching strategies (LRU, LFU, FIFO)
*/