Java设计模式:(1)设计模式七大设计原则-依赖倒转原则

2020-03-17 15:12:24 浏览数 (1)

基本介绍

1)依赖倒转原则基于这样的设计理念:相对于细节的多变形,抽象的东西要稳定的多,以抽象为基础搭建的架构比以细节为基础的架构要稳定的多

在Java中,抽象指的是接口或抽象类,细节就是具体的实现类

2)使用接口或抽象类的目的是制订好规范,而不涉及任何具体的操作,把展现细节的任务交给他们的实现类去完成

3)依赖倒转(倒置)的中心思想是面向接口编程

4)高层模块不应该依赖低层模块,二者都应该依赖其抽象

举个例子1

代码语言:javascript复制
public class Inversion01 {
    public static void main(String[] args) {
        Person person = new Person();
        person.receive(new Email());

    }
}

class Email{
    public String getInfo(){
        return "Hello World";
    }
}

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

分析:在例子1中,类Person 中recevie()接口一个Email的对象的参数来接收消息,如果这个时候我们获取的对象增加了其他的方式,微信、短信,Persons也要增加接口方法

举个例子2

代码语言:javascript复制
public class Inversion02 {
    public static void main(String[] args) {
        Person person = new Person();
        person.receive(new Email());
        person.receive(new WeiXin());
    }
}

interface IReceiver{
    public String getInfo();
}

class Email implements IReceiver{
    public String getInfo(){
        return "Hello Email ";
    }
}

class WeiXin implements IReceiver{
    public String getInfo(){
        return "Hello WeiXin ";
    }
}

class Person{
    public void receive(IReceiver iReceiver){
        System.out.println(iReceiver.getInfo());
    }
}

// 运行结果
Hello Email 
Hello WeiXin 

分析:在例子2中,引入了一个抽象的接口IReceiver,表示接收者,Person类与IReceiver类发生依赖,然后Email和WeiXin各自实现IReceiver接口。这样就事项了依赖倒转原则

依赖关系传递的三种关系和应用案例

1. 通过接口传递实现依赖
代码语言:javascript复制
public class Inversion03 {

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

interface IOpenAndClose{
  public void open(ITV tv);
}

interface ITV{
  public void play();
}

class ChangHong implements ITV{
  public void play(){
    System.out.println("ChangHong is play");
  }
}

class OpenAndClose implements IOpenAndClose{
  public void open(ITV tv){
    tv.play();
  }
}

// 运行结果
ChangHong is play
2. 通过构造方法依赖传递
代码语言:javascript复制
public class Inversion04 {
    public static void main(String[] args) {
        OpenAndClass openAndClass = new OpenAndClass(new ChangHong());
        openAndClass.open();
    }
}

interface IOpenAndClose{
    public void open();
}

interface ITV{
    public void play();
}

class OpenAndClass implements IOpenAndClose{
    public ITV tv;
    public OpenAndClass(ITV tv){
        this.tv = tv;
    }
    public void open(){
        this.tv.play();
    }
}

class ChangHong implements ITV{
    public void play(){
        System.out.println("ChangHong is play");
    }
}

// 运行结果
ChangHong is play
3. 通过setter方法传递
代码语言:javascript复制
public class Inversion05 {
    public static void main(String[] args) {
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setTv(new ChangHong());
        openAndClose.open();
    }
}

interface IOpenAndClose{
    public void open();
    public void setTv(ITV tv);
}

interface ITV{
    public void play();
}

class OpenAndClose implements IOpenAndClose{
    private ITV tv;
    public void setTv(ITV tv){
        this.tv = tv;
    }

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

class ChangHong implements ITV{
    public void play(){
        System.out.println("ChangHong is play");
    }
}
// 运行结果
ChangHong is play

0 人点赞