-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem7_adapter.py
More file actions
46 lines (30 loc) · 1.14 KB
/
problem7_adapter.py
File metadata and controls
46 lines (30 loc) · 1.14 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
"""
Problem 7: Adapter Pattern
"""
from abc import ABC, abstractmethod
class PaymentProcessor(ABC):
@abstractmethod
def process(self, amount, currency): pass
class LegacyPaymentService:
def make_payment(self, amount_in_dollars):
print(f"Legacy payment of ${amount_in_dollars}")
class LegacyPaymentAdapter(PaymentProcessor):
def __init__(self, legacy_service):
self.legacy_service = legacy_service
def process(self, amount, currency):
if currency != "USD":
amount = amount / 280 # simple conversion
self.legacy_service.make_payment(amount)
class ModernPaymentService(PaymentProcessor):
def process(self, amount, currency):
print(f"Modern payment: {amount} {currency}")
class PaymentSystem:
def __init__(self, processor: PaymentProcessor):
self.processor = processor
def pay(self, amount, currency):
self.processor.process(amount, currency)
if __name__ == "__main__":
modern = PaymentSystem(ModernPaymentService())
modern.pay(100, "USD")
legacy = PaymentSystem(LegacyPaymentAdapter(LegacyPaymentService()))
legacy.pay(28000, "PKR")