Skip to main content

What is Dependency inversion Principle?

Dependency inversion Principle

The dependency inversion principle (DIP) is one of the principles of SOLID. What is SOLID? SOLID is a five design pattern to make object-oriented designs more understandable, flexible, and maintainable.

To understand dependency inversion principle, we must understand what is inversion of control and dependency injection.

Inversion of control

What is control? And What is Inversion? We need to find the answer. So first, we are going to break down the control and inversion.

What is control? Do you think about that while you are writing the code or designing the code? The control is a flow that determines in what order the code is executed. If you wrote code like the one below, the code must be executed from top to bottom.

a = 10 b = 20 print(a + b) // 20

You can expect the result and directly figure out how to be executed this code. This is because you're controlling the flow of the sequence of codes.

class MessengerTest {
  public static boolean doTest() {
    // do something
  }

  public static void main(String[] args) {
    if (doTest()) {
        System.out.println("Test Success");
    } else {
        System.out.println("Test Failed");
    }
  }
}

The above code is the same and works as well. How do you do that if you have to write a test code like this form? You may abstract this class like the code below.

public abstract class TestCase {
    public void run() {
        if (doTest()) {
            System.out.println("Test Success");
        } else {
            System.out.println("Test Failed");
        }
    }

    public abstract boolean doTest();
}

public class TestApplication {
    private static final List<TestCase> testCases = new ArrayList<>();
    
    public static void register(Testcase testCase) {
        testCases.add(testCase);
    }

    public static final void main(String[] args) {
        for (TestCase testCase: testCases) {
            testCase.run();
        }
    }
}

If you abstract this class like the one above, then a client can make the test case easily by simply adding code.

public class MessengerTest extends TestCase { @overried public boolean doTest() { // do something... } } TestApplication.register(new MessengerTest());

Now, the Inversion of Control is reversed. That means you can not understand how the code works. It is important. You do not consider the way of being executed your codes. You just add your code to the Frameworks by following the framework's rules. Now, we move on to the dependency injection.

Dependency injection

Dependency injection is simple. It just injects the dependency of a class into the class from outside. 

class Money {
    private final int amount;
    private final Calculator calculator;

    public Money(int amount, Calculator calculator) {
        this.amount = amount;
        this.calculator = calculator;
    }

    public Money exchange(Country country) {
       return this.calculator.exchange(this.amount, country);
    }
}

Money class has chosen to be injected dependencies from the outside.  That means we can inject any Calculator classes into the Money Class without any modified Money class.

public static void main(String[] args) {
    Calculator calculator = new KoreanCalculator();
    Calculator calculator2 = new EuropeCalculator();

    Money money = Money(100, calculator);
    Money money2 = Money(100, calculator2);
}

For now, you may understand what dependency injection and Inversion of control are. So, we are going to find out what is Dependency Inversion Principle.

Dependency Inversion Principle

As mentioned before, Dependency Injection Principle is one of the principles of SOLID. The meaning of the world is that If there is a High-level layer and lower-level layers, both layers should depend on abstractions that expose the behavior needed by higher-level layers.

[Image From WikiPedia: https://en.wikipedia.org/wiki/Dependency_inversion_principle]

If you understand what Inversion of Control and Dependency injections are, then you can implement this pattern easily.


Comments

Popular posts from this blog

What does remark means and When I use it?

Remark I get this question a lot when I use it in daily conversation. It's subtle to explain what is different between "say" and "remark" .  When you say something as an opinion The word "Remark" is usually used to say about your opinion or say something being used that is related to the current situation. Tom remarked "There is no private data. We type something, search for something, and all of it is sent to Google." against Google News.  Do you understand what the word remark is? If you don't understand it, I'll show examples more.  Tommy always remarks "Recycling Campaign" when we are throwing away something.