Detailed examples of dependency inversion principle in Java
This article mainly introduces the definition of Java dependency inversion principle and the solution to the problem. Friends who are interested should take a look
Definition: High-level modules should not depend on low-level modules , both should depend on their abstractions; abstractions should not depend on details; details should depend on abstractions.
Origin of the problem: Class A directly depends on class B. If you want to change class A to depend on class C, you must modify the code of class A. In this scenario, class A is generally a high-level module responsible for complex business logic; classes B and C are low-level modules responsible for basic atomic operations; if class A is modified, it will bring unnecessary risks to the program.
Solution: Modify class A to depend on interface I, class B and class C each implement interface I, class A indirectly contacts class B or class C through interface I, then It will greatly reduce the chance of modifying Category A.
The dependency inversion principle is based on the fact that abstract things are much more stable than the variability of details. An architecture built on abstraction is much more stable than an architecture built on details. In Java, abstraction refers to interfaces or abstract classes, and details are specific implementation classes. The purpose of using interfaces or abstract classes is to formulate specifications and contracts without involving any specific operations, leaving the task of showing the details to Their implementation class to complete.
The core idea of the dependency inversion principle is interface-oriented programming. We still use an example to illustrate how interface-oriented programming is better than implementation-oriented programming. The scene is like this. A mother tells a story to her child. As long as she is given a book, she can tell the story to her child according to the book. The code is as follows:
class Book{ public String getContent(){ return "很久很久以前有一个阿拉伯的故事……"; } } class Mother{ public void narrate(Book book){ System.out.println("妈妈开始讲故事"); System.out.println(book.getContent()); } } public class Client{ public static void main(String[] args){ Mother mother = new Mother(); mother.narrate(new Book()); } }
Running results:
Mom starts telling stories
A long time ago There is an Arab story...
It is running well. If one day, the demand becomes like this: instead of giving a book, give a newspaper, and let the mother tell the story in the newspaper. The newspaper The code of , actually you have to modify Mother to read it. What if I need to switch to a magazine in the future? What about replacing it with a web page? You have to constantly modify the Mother, which is obviously not a good design. The reason is that the coupling between Mother and Book is too high, and the coupling between them must be reduced.
We introduce an abstract interface IReader. Readings, as long as they have words, are readings:
class Newspaper{ public String getContent(){ return "林书豪38+7领导尼克斯击败湖人……"; } }
Run results:
Mom starts telling stories
Mom starts telling the story
Jeremy Lin helped the Knicks defeat the Hawks with 17+9...
Modified like this Finally, no matter how the Client class is extended in the future, there is no need to modify the Mother class. This is just a simple example. In actual situations, the Mother class representing the high-level module will be responsible for completing the main business logic. Once it needs to be modified, the risk of introducing errors is extremely high. Therefore, following the dependency inversion principle can reduce the coupling between classes, improve the stability of the system, and reduce the risks caused by modifying the program.
The adoption of the dependency inversion principle brings great convenience to multi-person parallel development. For example, in the above example, when the Mother class and the Book class were directly coupled, the Mother class must wait until the Book class coding is completed. Coding can be done because the Mother class depends on the Book class. The modified programs can be started at the same time without affecting each other, because the Mother and Book classes have nothing to do with each other. The more people involved in collaborative development and the larger the project, the more significant it is to adopt the dependency-causing principle. The now popular TDD development model is the most successful application of the dependency inversion principle.
There are three ways to transfer dependencies. The method used in the above example is interface transfer. There are also two transfer methods: constructor method transfer and setter method transfer. I believe those who have used the Spring framework, You must be familiar with the way dependencies are passed.
Low-level modules must have abstract classes or interfaces, or both.
- The declared type of a variable should be an abstract class or interface as much as possible.
- Follow the Liskov substitution principle when using inheritance.
- The core of the dependency inversion principle is that we program for interfaces. If we understand interface-oriented programming, we also understand dependency inversion.
The above is the detailed content of Detailed examples of dependency inversion principle in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
