Design pattern principle - Dependency Inversion Principle

Posted by JeanieTallis on Mon, 04 May 2020 22:12:20 +0200

     Interface oriented programming, which depends on abstraction rather than on concrete classes, interacts through interfaces when writing concrete classes

1. Basic introduction

    (1) High level modules should not rely on low level modules, both of which should rely on abstraction
            (2) Abstraction should not depend on details. Details should depend on abstraction
            (3) The central idea of dependency inversion is interface oriented programming
            (4) Based on the design concept of inversion principle, compared with the variability of details, abstract things need to be more stable. The framework based on abstraction is more stable than the framework based on details. In java, abstraction represents interface or abstract class, and details represents specific implementation class
            (5) The purpose of using the interface is to make a good specification, not involving specific operations, and give the specific details to the implementation class for completion

2. Application scenario

Complete Person receiving message

/**
 * Complete Person receiving information
 * 1,If you need to receive other information, you need to add a new class and a new method for Person
 * 2,Solution: define a receiving interface, and implement it with person class,
 * 3,If other information needs to be received, the new class can directly implement the receiving interface to receive information
 */
static class Email {
    public String getInfo() {
        return "Receive mail information";
    }
}

static class Person {
    public void receive(Email email) {
        System.out.println(email.getInfo());
    }
}

public static void main(String[] args) {
    Person person = new Person();
    person.receive(new Email());
    //Improvement
    PersonInfo personInfo = new PersonInfo();
    personInfo.receive(new WeChat());

}

/**
 * Improvement scheme receiving email and wechat messages
 */

interface IReceive {
    public String getInfo();
}

static class WeChat implements IReceive {

    @Override
    public String getInfo() {
        return "Receive wechat message";
    }
}

static class PersonInfo {
    //Change person to interface dependency
    public void receive(IReceive receive) {
        System.out.println(receive.getInfo());
    }
}`

3. Three ways of dependency transmission

Public interface
   interface ITv {
        public void play();
    }

    static class ChangHong implements ITv {

        @Override
        public void play() {
            System.out.println("Turn on the TV-----");
        }
    }

(1) Pass through interface

interface IOpen {
        public void open(ITv tv);
}

static class Open implements IOpen {

        @Override
        public void open(ITv tv) {
                tv.play();
        }
}

public static void main(String[] args) {
        ChangHong changHong = new ChangHong();
        Open open = new Open();
        open.open(changHong);

}`

(2) Pass through constructor

   interface IOpenAndClose {
        public void open();
    }

    static class OpenAndClose implements IOpenAndClose {
        private ITv tv;

        //Constructor
        public OpenAndClose(ITv tv) {
            this.tv = tv;
        }

        @Override
        public void open() {
            tv.play();
        }
    }

    public static void main(String[] args) {
        ChangHong changHong = new ChangHong();
        OpenAndClose openAndClose = new OpenAndClose(changHong);
        openAndClose.open();
    }

(3) Pass through setter method

 interface IOpenClose {
        public void open();

        //set
        public void setOpen(ITv tv);
    }

    static class OpenClose implements IOpenClose {
        private ITv tv;

        @Override
        public void open() {
            this.tv.play();
        }

        @Override
        public void setOpen(ITv tv) {
            this.tv = tv;
        }
    }

    public static void main(String[] args) {
        ChangHong changHong = new ChangHong();
        OpenClose openClose = new OpenClose();
        openClose.setOpen(changHong);
        openClose.open();
    }

Topics: Java Programming