2.0 - Design Patterns seven principles of software architecture design - Dependency Inversion Principle

When the Dependency Inversion Principle (Dependence Inversion Principle, DIP) refers to the design code structure, layer module should not rely on the underlying module, both of which should depend abstraction. Abstract should not rely on details; 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 the program can be modified to reduce the risks caused.

Next we look at a piece of code, or in the course as an example:

public class Ma {

    public void studyJavaCourse(){
        System.out.println("MA在学习 Java 的课程");
    }
    public void studyPythonCourse(){
        System.out.println("MA在学习 Python 的课程");
    }


    public static void main(String[] args) {
        Ma ma = new Ma();
        ma.studyJavaCourse();
        ma.studyPythonCourse();
    }
}

MA students now want to study large data course, this time the business will expand, our code from the bottom to the top (call level) once to modify the code. StudyAICourse increase in MA class () method, but also in additional high-level calls. Thus, after the system release, in fact, it is very unstable, modifying the code will also bring unexpected risks.

Next we optimized code:

/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1718:29
 */
public interface Curriculum {
    void study();
}
/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1717:59
 */
public class JavaCurriculum implements Curriculum {
    @Override
    public void study() {
        System.out.println("马同学在学习 Java 课程");
    }
}
/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1718:31
 */
public class PythonCurriculum implements Curriculum {
    @Override
    public void study() {
        System.out.println("马同学在学习 Python 课程");
    }
}
/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1718:17
 */
public class Ma {

    public void study(Curriculum course){
        course.study();
    }

    public static void main(String[] args) {
        Ma ma = new Ma();
        ma.study(new JavaCurriculum());
        ma.study(new PythonCurriculum());
    }
}

This time let's look at the code, MA students want to learn the new curriculum, only to create a new class, by the way parameter passing without the need to modify the underlying code. In fact, this is a very familiar way, called dependency injection. There constructor injection way way way and setter. We take a look constructor injection method:

/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1718:17
 */
public class Ma {

    private Curriculum curriculum;

    public Ma(Curriculum curriculum) {
       this.curriculum=curriculum;
    }

    public void study(){
        curriculum.study();
    }

    public static void main(String[] args) {
        Ma ma = new Ma(new JavaCurriculum());
        ma.study();
    }
}

According to constructor injection mode, when you call, every time create an instance. So, if MA is a global singleton, then we can only choose a way to inject Setter, Tom continued to modify the code like:

/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1718:17
 */
public class Ma {

    private Curriculum curriculum;

    public void setCurriculum(Curriculum curriculum) {
        this.curriculum = curriculum;
    }

    public void study(){
        curriculum.study();
    }

    public static void main(String[] args) {
        Ma ma = new Ma();
        ma.setCurriculum(new JavaCurriculum());
        ma.study();
    }
}

We have to remember: abstract detail with reference to the ratio as a reference architecture to build up much more stable, so after we get the demand for programming to an interface, then the details of the first top-level design code structure.

Guess you like

Origin blog.csdn.net/madongyu1259892936/article/details/92803952