Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Inversion Principle: High Level and Low Level module example

I was going through the following link to understand what high-level and low-level modules mean in the context of Dependency Inversion Principle.

As per the explanation given there, is the following code snippet a good/appropriate example?

public class HighLevel
{
    private IAbstraction _abstraction;

    public HighLevel(IAbstraction abstraction)
    {
        _abstraction = abstraction;
    }

    public void Act()
    {
        _abstraction.DoSomething();
    }

}

public interface IAbstraction
{
    void DoSomething();
}

public class LowLevel: IAbstraction
{
    public void DoSomething()
    {
        //Do something
    }
}
like image 298
Sang Suantak Avatar asked Apr 25 '17 06:04

Sang Suantak


People also ask

What are high level modules and low-level modules?

High-level modules contain the important policy decisions and business models of an application – The identity of the application. Low-level modules contain detailed implementations of individual mechanisms needed to realize the policy.

What is an example of dependency inversion principle?

In our example, CustomerBusinessLogic depends on the DataAccess class, so CustomerBusinessLogic is a high-level module and DataAccess is a low-level module. So, as per the first rule of DIP, CustomerBusinessLogic should not depend on the concrete DataAccess class, instead both classes should depend on abstraction.

What is high level module in dependency inversion?

Definition of the Dependency Inversion Principle The general idea of this principle is as simple as it is important: High-level modules, which provide complex logic, should be easily reusable and unaffected by changes in low-level modules, which provide utility features.

What is high level module in Java?

High level module is the interface / abstraction that will be consumed directly by the presentation layer. Low level on the other hand are bunch of small modules (subsystems) help the high level do their work. Example below is the high level module.


1 Answers

To make a long answer short: yes, this is an example of a Dependency Inversion Principle

like image 142
dvberkel Avatar answered Sep 23 '22 20:09

dvberkel