DIP – hiding irrelevant details
In this section, we will learn how the DIP allows us to split code into separate components that can change independently of each other. We will then see how this naturally leads to the OCP part of SOLID.
Dependency inversion (DI) means that we write code to depend on abstractions, not details. The opposite of this is having two code blocks, one that depends on the detailed implementation of the other. Changes to one block will cause changes to another. To see what this problem looks like in practice, let’s review a counter-example. The following code snippet begins where we left off with the Shapes
class after applying SRP to it:
package shapes; import java.util.ArrayList; import java.util.List; public class Shapes { private final List<Shape> allShapes = new ArrayList<>(); public void add(Shape s) { allShapes.add(s); ...