I am currently trying to figure out SOLID. So the Dependency Inversion Principle means that any two classes should communicate via interfaces, not directly. Example: If class A
has a method, that expects a pointer to an object of type class B
, then this method should actually expect an object of type abstract base class of B
. This is helps for Open/Close as well.
Provided that I understood that correctly, my question would be is it a good practice to apply this to all class interactions or should I try to think in terms of layers?
The reason I am skeptical is because we are paying some price for following this principle. Say, I need to implement feature Z
. After analysis, I conclude that feature Z
consists of functionality A
, B
and C
. I create a facade class Z
, that, through interfaces, uses classes A
, B
and C
. I begin coding the implementation and at some point I realize that task Z
actually consists of functionality A
, B
and D
. Now I need to scrap the C
interface, the C
class prototype and write separate D
interface and class. Without interfaces, only the class would've needed to be replaced.
In other words, to change something, I need to change 1. the caller 2. the interface 3. the declaration 4. the implementation. In a python directly coupled implementation, I would need to change only the implementation.