Dependency Inversion
On This Lesson 4 topics
The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.
In this lesson
make high-level code depend on interfaces, not on concrete details.
Planned topics
- D: high-level code and low-level code both depend on an interface.
- Like the socket: the kitchen depends on the socket shape, not the appliance.
- Pass dependencies in through the constructor (dependency injection).
- Benefit: in tests, pass a fake instead of the real thing.
- Spring's dependency injection is built on this idea.
Draft outline details
Demonstration
interface PaymentProvider { Receipt charge(Money amount); }
class CheckoutService {
private final PaymentProvider payments;
CheckoutService(PaymentProvider payments) { this.payments = payments; }
}Check your understanding
Why does DI make testing easier?
Show answer
You can pass a fake in place of the real dependency.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.