Strategy example:
# Strategies common interface, Strategies:
class PaymentInterface:
def pay(self, amount):
pass
class CardPayment:
def pay(self, amount):
print(f"Paying {amount} with card.")
class PayPalPayment:
def pay(self, amount):
print(f"Paying {amount} via PayPal.")
class BankTransferPayment:
def pay(self, amount):
print(f"Paying {amount} via bank transfer.")
# Context
class PaymentProcessor:
def __init__(self, strategy):
self.strategy = strategy
def set_strategy(self, strategy):
self.strategy = strategy
def process_payment(self, amount):
self.strategy.pay(amount)
# Usage
processor = PaymentProcessor(CardPayment())
processor.process_payment(100)
processor.set_strategy(PayPalPayment())
processor.process_payment(200)