Which statement best describes the Dependency Inversion Principle?
Think about which direction dependencies should point to reduce tight coupling.
The Dependency Inversion Principle states that both high-level and low-level modules should depend on abstractions (interfaces or abstract classes), not on concrete implementations. This reduces coupling and increases flexibility.
You are designing a payment system where the high-level module processes payments and the low-level modules handle different payment gateways (e.g., PayPal, Stripe). How should you apply the Dependency Inversion Principle?
Consider how to decouple the payment processor from specific payment gateway implementations.
By depending on an abstraction that all payment gateways implement, the payment processor can work with any gateway without changing its code, following DIP.
You have a notification system with modules for email, SMS, and push notifications. To scale and add new notification types easily, which design aligns best with the Dependency Inversion Principle?
Think about how to add new notification types without modifying the sender module.
By having the sender depend on an abstraction, new notification types can be added by implementing the abstraction without changing the sender, supporting scalability and DIP.
What is a common tradeoff when applying the Dependency Inversion Principle in system design?
Consider the impact of adding abstractions on system complexity.
While DIP improves flexibility and reduces coupling, it often introduces more interfaces and abstractions, which can increase the system's complexity and learning curve.
Consider a high-level module OrderProcessor that directly creates and uses a concrete class MySQLDatabase for data storage. Which problem does this violate according to the Dependency Inversion Principle?
Think about which direction dependencies should point and what modules should depend on.
OrderProcessor depending directly on a concrete class like MySQLDatabase creates tight coupling and violates DIP. Instead, it should depend on an abstraction that MySQLDatabase implements.