DIP: you should depend on what?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

DIP: you should depend on what?

Explanation:
Dependency Inversion Principle tells you to have high-level modules depend on abstractions rather than concrete details. In practice, design components to rely on interfaces or abstract types that specify what they need from collaborators, not on the concrete classes that implement that behavior. This decouples the high-level logic from specific implementations, making it easy to swap in different behavior, test with mocks or fakes, and evolve the code without touching the core logic. If you depend on a concrete class, changes there ripple through the rest of the code; if you depend on an abstraction, you can substitute different implementations without altering the high-level module. You achieve this through dependency injection or using factories to supply the abstraction at runtime, which keeps the design flexible. This is why the idea is to depend on abstractions, not concretions. Relying on concrete implementations binds you to a specific behavior, reducing adaptability. Global state or platform-specific APIs also undermine modularity: global state hurts testability and predictability, while platform-specific code ties you to a particular environment instead of abstract behavior.

Dependency Inversion Principle tells you to have high-level modules depend on abstractions rather than concrete details. In practice, design components to rely on interfaces or abstract types that specify what they need from collaborators, not on the concrete classes that implement that behavior. This decouples the high-level logic from specific implementations, making it easy to swap in different behavior, test with mocks or fakes, and evolve the code without touching the core logic. If you depend on a concrete class, changes there ripple through the rest of the code; if you depend on an abstraction, you can substitute different implementations without altering the high-level module. You achieve this through dependency injection or using factories to supply the abstraction at runtime, which keeps the design flexible.

This is why the idea is to depend on abstractions, not concretions. Relying on concrete implementations binds you to a specific behavior, reducing adaptability. Global state or platform-specific APIs also undermine modularity: global state hurts testability and predictability, while platform-specific code ties you to a particular environment instead of abstract behavior.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy