Talk about Dependency Inversion Principle

In order to obtain a good reading experience, please visit the original: Portal

First, the concept

When the Dependency Inversion Principle (Dependence Inversion Principle, DIP) code structure refers to the design, you should not rely on low-level layer module module, both of which should depend abstraction.

Abstract should not rely on the details, the details should depend on the abstract. By relying inversion can reduce the coupling between the class and class, and improve system stability, improve the readability and maintainability of the code and to reduce the risks caused by modifying the program.

Second, why

First look at an example

But the Dependency Inversion Principle is how to do it? Let's look at an example: a love of learning, "I do not have three heart" students are now learning course "design patterns" and "Java", the pseudo-code is as follows:

public class Wmyskxz {

    public void studyJavaCourse() {
        System.out.println("「我没有三颗心脏」同学正在学习「Java」课程");
    }

    public void studyDesignPatternCourse() {
        System.out.println("「我没有三颗心脏」同学正在学习「设计模式」课程");
    }
}复制代码

We call it the top to simulate:

public static void main(String[] args) {
    Wmyskxz wmyskxz = new Wmyskxz();
    wmyskxz.studyJavaCourse();
    wmyskxz.studyDesignPatternCourse();
}复制代码

One reason: the scope of effective control

Because "I do not have three heart" the students love learning, along with interest in learning the "surge", may continue learning programs AI (artificial intelligence) is. This time, because "business expansion", from the underlying implementation to high-level calls to modify the code in turn.

We need to add in Wmyskxz class studyAICourse()method, also called the need to increase high-level calls, so that, after release system, in fact, is very unstable. Obviously in this simple example, we can feel confident that we can live Hold this time the impact of changes, because all the new code , we return, they also can be a good cover live, but the actual the situation and the actual software environment is much more complex.

The ideal situation is that we have to write good code can be "the same years" , which means already covered by unit tests can not modify existing behavior can be guaranteed to remain unchanged, which means "stable" . Any impact on the modification of the code are all caused by unknown risks, no matter how simple it seems.

Two reasons: to enhance code readability and maintainability

Another point, you have not found the AI ​​actually add new courses of study, the behavior of their three classes essentially the same, almost the same if we let such behavior codes sprawl in our class inside, and soon our class will become bloated, have to wait until we realized the reconstruction of this class to alleviate this situation, it might cost has become high terrible.

Three reasons: to reduce the coupling

"Das Kapital" in such a description:

In the infancy of the commodity economy, the emergence of barter. Suppose you want to buy a iPhone, iPhone sell the boss asks you to take a pigs for him, but you do not pig, you will only programming. So you find a pig farmer, he said, to make a pig of APP to change him a pig, pig may change, he said, but starting a gold necklace in exchange for ...

So here there have been a series of object dependencies, thus causing serious coupling disaster. The best solution to this problem is to rely on the sale of twin abstract - that is, money - to be exchanged, so that the degree of coupling will be greatly reduced.

Third, how do

Our code is now directly dependent on low-level to achieve the top, and now we need to define an abstract ICourse interfaces to decouple this strong dependence (as in the example above "Das Kapital" above):

Next we can refer to pseudo-code, we'll set a course abstract ICourse interfaces:

public interface ICourse {
    void study();
}复制代码

Then write respectively, JavaCourseand DesignPatternCoursewrite a class:

public class JavaCourse implements ICourse {

    @Override
    public void study() {
        System.out.println("「我没有三颗心脏」同学正在学习「Java」课程");
    }
}

public class DesignPatternCourse implements ICourse {

    @Override
    public void study() {
        System.out.println("「我没有三颗心脏」同学正在学习「设计模式」课程");
    }
}复制代码

Then Wmyskxz class transformed into the following way:

public class Wmyskxz {

    public void study(ICourse course) {
        course.study();
    }
}复制代码

Next came our call:

public static void main(String[] args) {
    Wmyskxz wmyskxz = new Wmyskxz();
    wmyskxz.study(new JavaCourse());
    wmyskxz.study(new DesignPatternCourse());
}复制代码

This time we look at the code, either, "I do not have three heart" of how skyrocketing interest for the new course, only you need to create a class, tell it by the way of passing parameters, without the need to modify the underlying code. In fact it's a bit like the familiar dependency injection mode of.

In short, remember: abstract basis is much more than detail with reference to build up a stable framework and therefore get the demand, to face interface programming, the first top-level design details to re-design the code structure.

Reference material

  1. www.cnblogs.com/aoyeyuyan/p... - those of advanced years do not understand the term - dependency inversion • Inversion of Control • Dependency Injection • oriented programming interface
  2. "Spring 5 core principles and 30 handwritten real class" - with Tan Yongde

---

By convention a sticky tail:

Welcome to reprint, please indicate the source!

Independent domain name blog: wmyskxz.com

Jane book ID: @ I do not have three heart

github:wmyskxz

Welcome to public attention Micro Signal: wmyskxz

To share their learning & learning materials & Living

Want to connect friends can also add qq group: 3,382,693
Money

Guess you like

Origin juejin.im/post/5dd2ac6851882567490f9404