Dependency inversion (DIP) 00:00:00 Make a prediction You will draw which package owns the payment contract and prove checkout can handle a decline without constructing a bank client. Checkout builds a specific bank SDK object inside its charge method. Can you test a declined payment without talking to that SDK or replacing hidden global state? Only awkwardly. Put the charge contract in checkout's vocabulary and inject an implementation. A fake can decline predictably while production uses the adapter. 00:00:31 The travelling theatre A touring theatre brings a lantern with a standard mounting ring. The stage defines the ring position; one town supplies an oil lamp, another an electric one. The play does not change its script for each town. The performance is policy and the stage connector is the port it requires. The locally wired implementation conforms to that port, so source dependencies point toward the policy. The dependency inversion principle (DIP) has two sentences. High-level modules should not depend on low-level modules: both should depend on abstractions. And abstractions should not depend on details: details should depend on abstractions. Policy lives long and tools change. When a rule creates its own database client, a new client is an edit in the rule. With many rules, it is an edit in all of them. 00:01:29 Keep the business rule in charge Checkout needs the outcome of a charge, not the fields of one bank's response. Define the charge operation in checkout's vocabulary. The bank adapter implements that port and translates the response. The application composition step passes the adapter into checkout. For a test, inject a declining implementation. Checkout keeps the order unpaid without a network call; the direction of the source-code dependency is toward the domain contract. Inject a gateway that declines a ten-dollar charge. The order should stay unpaid. Replace it with a gateway that approves the same charge and assert the new paid state. If the domain code switches on a vendor-specific error number, the business rule still depends on a detail of that vendor. 00:02:17 Apply it to a clock Subscription renewal depends on today's date. Reading the system clock inside every branch makes a midnight case hard to reproduce. Supply a clock that the renewal service asks for time. Production uses the real clock, while a test supplies the exact expiration instant. Freeze the clock one instant before midnight, record the eligibility result, then advance to midnight and repeat. An injected clock makes both answers reproducible without moving the machine clock. If the rule secretly calls system time, the test no longer controls the decision. Passing an SDK object into a constructor still couples the policy to that SDK if vendor types and error codes leak through the boundary. When you depend on a stable small value rather than a varying service, injecting a basic value can be simpler than inventing an interface. JDBC. Java code calls interfaces in java.sql. Every database vendor ships a class that implements Driver. The same calls work over different drivers; the SQL text may still differ. Go database/sql. The package holds the generic calls and must be used with a driver. Package database/sql/driver defines the interfaces that drivers implement. 00:03:38 Try an interview follow-up When would you reject a new interface in review? I would ask which change or test the interface makes easier. If it simply repeats a private class, has one implementation and crosses no meaningful boundary, I would postpone it. A published contract is different: external users are harder to migrate, so the cost of deciding late can be much higher. A receipt vendor changes its API. Which classes must change? Let the order service express a small send-receipt contract in business terms. Put the vendor translation in an adapter and supply that adapter at startup. Test an order with a recording sender that confirms the order number and amount. 00:04:23 What to take with you Point source code arrows at the policy. The policy owns a small port, adapters implement it, and the composition root joins them. The run-time call still goes from policy to tool. Injection is how objects receive things. Inversion is who owns the interface. You can inject a concrete class and have no inversion at all.