设计模式原则-依赖倒置原则

     面向接口编程,依赖于抽象而不是依赖与具体类,写具体类时通过接口进行交互

1. 基本介绍

    (1)高层模块不应该依赖低层模块,二者都应该依赖抽象
            (2)抽象不应该依赖细节,细节应该依赖抽象
            (3)依赖倒置的中心思想是面向接口编程
            (4)依赖倒置原则的设计理念,相对于细节的多变性,抽象的东西要稳定的多,以抽象为基础搭建的框架比以细节为基础的框架稳定,java中,抽象代表接口或抽象类,细节代表具体的实现类
            (5)使用接口的目的是制定好规范,不涉及具体的操作,把具体的细节交给实现类完成

2. 应用场景

完成Person 接收消息

/**
 * 完成Person 接收信息
 * 1、如果需要接收别的信息时,需要新增类,同时Person 需要添加新的方法
 * 2、解决思路: 定义一个接收的接口,person 类去实现,
 * 3、如果需要接收别的信息,新增类可直接实现接收接口,实现接收信息
 */
static class Email {
    public String getInfo() {
        return "接收邮件信息";
    }
}

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());
    //改进
    PersonInfo personInfo = new PersonInfo();
    personInfo.receive(new WeChat());

}

/**
 * 改进方案 接收邮件、微信消息
 */

interface IReceive {
    public String getInfo();
}

static class WeChat implements IReceive {

    @Override
    public String getInfo() {
        return "接收微信消息";
    }
}

static class PersonInfo {
    //person 修改为对接口的依赖
    public void receive(IReceive receive) {
        System.out.println(receive.getInfo());
    }
}`

3. 依赖传递的三种方式

公共的接口
   interface ITv {
        public void play();
    }

    static class ChangHong implements ITv {

        @Override
        public void play() {
            System.out.println("打开电视机-----");
        }
    }

(1)通过接口传递

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)通过构造函数传递

   interface IOpenAndClose {
        public void open();
    }

    static class OpenAndClose implements IOpenAndClose {
        private ITv tv;

        //构造函数
        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)通过setter方法传递

 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();
    }
上一篇:怎样将苹果AirPods Max耳机连接到Apple TV?


下一篇:编写程序测量 pthread_create、fork 两个函数的运行时间,并进行实测比较