Dependency Inversion Principle

What is the Dependency Inversion Principle?

Let us understand the Dependency Inversion Principle with one example

public class BackEndDeveloper {    public void writeJava() { }
}
public class FrontEndDeveloper {    public void writeJavascript() { }
}
public class Project {    private BackEndDeveloper backEndDeveloper = new BackEndDeveloper();

private FrontEndDeveloper frontEndDeveloper = new FrontEndDeveloper();

public void implement() {
backEndDeveloper.writeJava(); frontEndDeveloper.writeJavascript();
}
}
public interface Developer {
void develop();
}
public class BackEndDeveloper implements Developer {    @Override
public void develop() {
writeJava();
}
private void writeJava() { }}
public class FrontEndDeveloper implements Developer {    @Override
public void develop() {
writeJavascript();
}
public void writeJavascript() {}
}
public class Project {

private List<Developer> developers;

public Project(List<Developer> developers) {
this.developers = developers;
}

public void implement() {
developers.forEach(d->d.develop());
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Panchal Amit

Panchal Amit

📱 Android Engineer, 📝 Writer, 💻 Open Source Contributor, Techie, IoT, Interactive Projects, ☁ AWS, Google Cloud, Firebase, Python, React.