Show in graph

Software → Software Design

DIP

High-level policy should depend on abstractions rather than low-level implementation details.

Motivation

DIP solves the problem of important business logic depending directly on low-level details. If core policy knows about databases, email vendors, or payment SDKs, changing infrastructure becomes risky.

Where it fits

DIP belongs to SOLID and software design. It is closely related to dependency injection, clean architecture, and ports/adapters style boundaries.

Code example
Bad example
Python
class CheckoutService:  def checkout(self, order):      payment = StripePaymentGateway()      repository = PostgresOrderRepository()      payment.charge(order.total)      repository.save(order)
Good example
Python
class CheckoutService:  def __init__(self, payment_gateway, order_repository):      self.payment_gateway = payment_gateway      self.order_repository = order_repository  def checkout(self, order):      self.payment_gateway.charge(order.total)      self.order_repository.save(order)

Common mistakes

  • Confusing DIP with a dependency injection framework.
  • Creating abstractions for things that are unlikely to vary.