-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem4_strategy.py
More file actions
47 lines (30 loc) · 1.1 KB
/
problem4_strategy.py
File metadata and controls
47 lines (30 loc) · 1.1 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
"""
Problem 4: Strategy Pattern
"""
from abc import ABC, abstractmethod
class PaymentStrategy(ABC):
@abstractmethod
def process_payment(self, amount): pass
class CreditCardStrategy(PaymentStrategy):
def process_payment(self, amount):
print(f"Paid {amount} via Credit Card")
class PayPalStrategy(PaymentStrategy):
def process_payment(self, amount):
print(f"Paid {amount} via PayPal")
class BankTransferStrategy(PaymentStrategy):
def process_payment(self, amount):
print(f"Paid {amount} via Bank Transfer")
class PaymentProcessor:
def __init__(self, strategy: PaymentStrategy):
self.strategy = strategy
def set_strategy(self, strategy: PaymentStrategy):
self.strategy = strategy
def execute_payment(self, amount):
self.strategy.process_payment(amount)
if __name__ == "__main__":
processor = PaymentProcessor(CreditCardStrategy())
processor.execute_payment(100)
processor.set_strategy(PayPalStrategy())
processor.execute_payment(200)
processor.set_strategy(BankTransferStrategy())
processor.execute_payment(300)