Abstract Factory Pattern
from abc import ABC, abstractmethod
# Interface representing any payment gateway
class PaymentGateway(ABC):
@abstractmethod
def process_payment(self, amount):
pass
# Concrete implementation: Razorpay
class RazorpayGateway(PaymentGateway):
def process_payment(self, amount):
print(f"Processing INR payment via Razorpay: {amount}")
# Concrete implementation: PayU
class PayUGateway(PaymentGateway):
def process_payment(self, amount):
print(f"Processing INR payment via PayU: {amount}")
# Interface representing invoice generation
class Invoice(ABC):
@abstractmethod
def generate_invoice(self):
pass
# Concrete invoice implementation for India
class GSTInvoice(Invoice):
def generate_invoice(self):
print("Generating GST Invoice for India.")
# CheckoutService that directly handles object creation (bad practice)
class CheckoutService:
def __init__(self, gateway_type):
# Constructor accepts a string to determine which gateway to use
self.gateway_type = gateway_type
# Checkout process hardcodes logic for gateway and invoice creation
def check_out(self, amount):
# Hardcoded decision logic
if self.gateway_type == "razorpay":
payment_gateway = RazorpayGateway()
else:
payment_gateway = PayUGateway()
# Process payment using selected gateway
payment_gateway.process_payment(amount)
# Always uses GSTInvoice, even though more types may exist later
invoice = GSTInvoice()
invoice.generate_invoice()
# Main method
if __name__ == "__main__":
# Example: Using Razorpay
razorpay_service = CheckoutService("razorpay")
razorpay_service.check_out(1500.00)

Last updated