The Dependency Inversion Principle
The concept of avoiding concreate
classes isn't new. Robert C. Martin defined this idea in The C++ Report in May 1996 in an article titled The Dependency Inversion Principle. It is the D in his SOLID design principles. The principle has two parts:
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend on details. Details should depend on abstractions.
While this may seem like a mouthful, the concept is actually very easy. Imagine we have a StageManager
class that is responsible for initializing, updating, and shutting down all of the stages in our game. In this case, our StageManager
is our high-level modules, and the stages are the low-level modules. The StageManager
will control the creation and behavior of our low-level module, the stages. This principle says that our StageManager
code shouldn't depend on derived stage classes, but should instead depend on an abstract stage class. To...