Design Mode Software Development Original Open Close Principle and Dependency Inverted Principle

Open and Close Principle

Definition

The so-called open-close principle is that a software entity such as classes, modules, and functions should be open to extensions and closed to modifications.
Emphasizes building a framework with abstraction to achieve expanded detail.

It has the advantage of improving the reusability and maintainability of the software.Is the most basic principle of object-oriented.

Dependency Inversion Principle

Definition

High-level modules should not depend on low-level modules, and both should depend on their abstraction.
Abstract should not depend on detail: detail should depend on abstraction.
Programming for interfaces, not for implementations.

Advantage

Lower coupling improves stability and readability and maintainability of code.Reduce the risk your code may pose when you modify it.

Let's use code to illustrate what depends on the inversion principle

Suppose we want to fulfill one's learning needs, we can write

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class Redstar {
    public void studyJava() {
        System.out.println("redstar I'm learning java");
    }

    public void studyFE() {
        System.out.println("redstar On the Front End of Learning");
    }
}

And then there's a test class

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class RedstarTest {
    public static void main(String[] args) {
        Redstar redstar = new Redstar();
        redstar.studyFE();
        redstar.studyJava();
    }
}

If we want to learn Python, then we need to modify the Redstar class.This way of writing is implementation-oriented because the entire Redstar class is an implementation class.This Redstar class is frequently modified.It is also less expansive.Our RedstarTest class is the application layer (high level module) class that depends on this Redstar implementation class (low level module).Because we don't have abstraction.High-level modules should not rely on low-level modules according to the principle of dependency inversion.Each Redstar expansion is supplemented by RedstarTest.

Let's start with code that relies on inversion and create an interface first.This interface has a learning method.

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
 
public interface ICourse {
    void studyCourse();
}

Here are a few implementation classes to implement this interface


public class JavaCourse implements ICourse {

    @Override
    public void studyCourse() {
        System.out.println("redstar I'm learning Java curriculum");
    }
}
public class FECourse implements ICourse {
    @Override
    public void studyCourse() {
        System.out.println("redstar I'm learning FE curriculum");
    }

}

At this point we will modify the Redstar class to

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class Redstar {
    public void studyJava(ICourse iCourse) {
        iCourse.studyCourse();
    }
}

Test class modified to

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class RedstarTest {
    public static void main(String[] args) {
        Redstar redstar = new Redstar();
        redstar.studyJava(new JavaCourse());
        redstar.studyJava(new FECourse());
    }
}

The output is

redstar is learning Java
 redstar is learning FE courses

At this point, if we have other large lessons to learn, we can add them by adding an implementation class for ICourse.
Redstar is a class that won't move him.We have a high-level class, RedstarTest, to choose which courses we want to take.
By the way, we inject dependencies on the ICourse interface using interface methods in the RedstarTest class.We can also inject dependencies as constructors.

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class Redstar {
    private ICourse iCourse;
    public Redstar(ICourse iCourse) {
        this.iCourse = iCourse;
    }

    public void studyJava() {
        iCourse.studyCourse();
    }
}

That's what we should say in Redstar at this point

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class RedstarTest {
    public static void main(String[] args) {
        JavaCourse javaCourse = new JavaCourse();
        FECourse feCourse = new FECourse();
        Redstar redstar = new Redstar(javaCourse);
        redstar.studyJava();
        redstar = new Redstar(feCourse);
        redstar.studyJava();
    }
}

There's a constructor and we can also use set, and that's what our Redstar wrote

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class Redstar {
    private ICourse iCourse;

    public void setiCourse(ICourse iCourse) {
        this.iCourse = iCourse;
    }

    public void studyJava() {
        iCourse.studyCourse();
    }
}

That's what RedstarTest does

/**
 * @author Yang Hongxing
 * @version 1.0.0
 * @date 2018/11/30
 */
public class RedstarTest {
    public static void main(String[] args) {
        JavaCourse javaCourse = new JavaCourse();
        FECourse feCourse = new FECourse();
        Redstar redstar = new Redstar();
        redstar.setiCourse(feCourse);
        redstar.studyJava();
        redstar.setiCourse(javaCourse);
        redstar.studyJava();
    }
}

So here we are finished with the principle of dependency inversion. To sum up, the general principle is Interface-oriented programming, or abstract-oriented programming.The interface in the example above can also be replaced by abstract classes.Students can use abstract classes to simulate the above process.

Keywords: Java Programming Python less

Added by jonskinny12 on Sun, 19 May 2019 00:02:12 +0300