Problem Statement
When code depends directly on concrete classes, changing implementations requires modifying all dependent code. This tight coupling makes the system fragile and hard to extend or test.
The diagram shows the client depending only on the interface, while multiple implementations can vary independently.
### Before: Program to implementation (tight coupling) class PaymentProcessor: def pay(self, amount): print(f"Processing payment of {amount} using Stripe") class OrderService: def __init__(self): self.processor = PaymentProcessor() def checkout(self, amount): self.processor.pay(amount) order = OrderService() order.checkout(100) ### After: Program to interface (loose coupling) from abc import ABC, abstractmethod class PaymentProcessorInterface(ABC): @abstractmethod def pay(self, amount): pass class StripeProcessor(PaymentProcessorInterface): def pay(self, amount): print(f"Processing payment of {amount} using Stripe") class PaypalProcessor(PaymentProcessorInterface): def pay(self, amount): print(f"Processing payment of {amount} using PayPal") class OrderService: def __init__(self, processor: PaymentProcessorInterface): self.processor = processor def checkout(self, amount): self.processor.pay(amount) stripe = StripeProcessor() order = OrderService(stripe) order.checkout(100) paypal = PaypalProcessor() order2 = OrderService(paypal) order2.checkout(200)
interface Vehicle { void drive(); }
class Car implements Vehicle { public void drive() { print("Car driving"); } }
class Bike implements Vehicle { public void drive() { print("Bike driving"); } }
Vehicle v = new Car();
v.drive();interface Printer { void print(); }
class LaserPrinter implements Printer { void print() { System.out.println("Laser printing"); } }
Printer p = new LaserPrinter();
p.print();Pass the test and this pattern moves unknown