zoukankan      html  css  js  c++  java
  • 第三节:依赖倒转原则

    依赖倒转原则(Dependence Inversion Principle)

    一、基本介绍

      依赖倒转原则是指:

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

      (2)抽象不应该依赖细节,细节应该依赖抽象;

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

      (4)依赖倒转原则是基于这样的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多。在java中,抽象指的是接口或抽象类,细节就是具体的实现类;

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

    二、应用案例

      1、以 Person接收信息为例

        代码示例:

     1 public class DependecyInversion {
     2     public static void main(String[] args) {
     3         Person person = new Person();
     4         person.receive(new Email());
     5     }
     6 }
     7 
     8 /**
     9  * 完成 Person 接收消息的功能
    10  * 方式1完成
    11  * 分析:
    12  *  1、简单,容易想到,
    13  *  2、不容易扩展,如果获取的对象是微信,短信等,
    14  *      则Person也要增加响应的方法
    15  *  3、解决思路:引入一个抽象的接口 IReveiver,表示接受者
    16  *      这样 Person 类 与IReveiver接口发生依赖
    17  *  4、因为 Email,Weixin 等属于接收的范围,让他们各自实现
    18  *      IReceiver 接口就 OK,这样就符合依赖倒转原则
    19  */
    20 class Person {
    21     public void receive(Email email) {
    22         System.out.println(email.getInfo());
    23     }
    24 }
    25 
    26 class Email {
    27     public String getInfo() {
    28         return "电子邮件:hello,world";
    29     }
    30 }

      2、将功能抽象为接口,面向接口编程,使用依赖倒转原则

        代码示例:

     1 public class DependecyInversion {
     2     public static void main(String[] args) {
     3         //客户端无需改变
     4         Person person = new Person();
     5         person.receive(new Email());
     6         person.receive(new Weixin());
     7     }
     8 }
     9 
    10 /**
    11  * 完成 Person 接收消息的功能
    12  * 方式2完成
    13  * 分析:
    14  *
    15  */
    16 class Person {
    17     //这里是对接口的依赖
    18     public void receive(IReceiver receiver) {
    19         System.out.println(receiver.getInfo());
    20     }
    21 }
    22 
    23 /**
    24  *  定义接收的接口
    25  */
    26 interface IReceiver {
    27     public String getInfo();
    28 }
    29 class Email implements IReceiver{
    30 
    31     @Override
    32     public String getInfo() {
    33         return "电子邮件:hello,world";
    34     }
    35 }
    36 
    37 class Weixin implements IReceiver {
    38 
    39     @Override
    40     public String getInfo() {
    41         return "微信信息:hello,weixin";
    42     }
    43 }

    三、依赖关系传递的三种方式

      1、接口传递

        代码示例:

     1 public class DependencyPass1 {
     2     public static void main(String[] args) {
     3         OpenAndClose openAndClose = new OpenAndClose();
     4         openAndClose.open(new ChangHong());
     5     }
     6 }
     7 //方式1: 通过接口传递实现依赖
     8 
     9 /**
    10  * 开关的接口
    11  */
    12 interface IOpenAndClose1 {
    13     public void open(ITV1 tv); //抽象方法,接收接口
    14 }
    15 
    16 interface ITV1 { //ITV接口
    17     public void play();
    18 }
    19 
    20 class ChangHong implements ITV1 {
    21 
    22     @Override
    23     public void play() {
    24         System.out.println("长虹电视机111,打开");
    25     }
    26 
    27 }
    28 
    29 // 实现接口
    30 class OpenAndClose implements IOpenAndClose1 {
    31     @Override
    32     public void open(ITV1 tv) {
    33         tv.play();
    34     }
    35 }

      2、构造方法传递

        代码示例:

     1 public class DependencyPass2 {
     2     public static void main(String[] args) {
     3         OpenAndClose2 openAndClose2 = new OpenAndClose2(new ChangHone2());
     4         openAndClose2.open();
     5     }
     6 }
     7 
     8 // 方式2: 通过构造方法依赖传递
     9 interface IOpenAndClose2 {
    10     public void open(); //抽象方法
    11 }
    12 
    13 interface ITV2 { //ITV接口
    14     public void play();
    15 }
    16 
    17 class OpenAndClose2 implements IOpenAndClose2 {
    18     public ITV2 tv; //成员
    19 
    20     public OpenAndClose2(ITV2 tv) { //构造器
    21         this.tv = tv;
    22     }
    23 
    24     @Override
    25     public void open() {
    26         this.tv.play();
    27     }
    28 }
    29 
    30 class ChangHone2 implements ITV2 {
    31 
    32     @Override
    33     public void play() {
    34         System.out.println("长虹电视机222,打开");
    35     }
    36 }

      3、setter 方法传递

        代码示例:

     1 public class DependencyPass3 {
     2     public static void main(String[] args) {
     3         //通过setter方法进行依赖传递
     4         OpenAndClose3 openAndClose3 = new OpenAndClose3();
     5         openAndClose3.setTv(new ChangHong3());
     6         openAndClose3.open();
     7     }
     8 }
     9 
    10 // 方式3 , 通过setter方法传递
    11 interface IOpenAndClose3 {
    12     public void open(); // 抽象方法
    13 
    14     public void setTv(ITV3 tv);
    15 }
    16 
    17 interface ITV3 { // ITV接口
    18     public void play();
    19 }
    20 
    21 class OpenAndClose3 implements IOpenAndClose3 {
    22     private ITV3 tv;
    23 
    24     @Override
    25     public void setTv(ITV3 tv) {
    26         this.tv = tv;
    27     }
    28 
    29     @Override
    30     public void open() {
    31         this.tv.play();
    32     }
    33 
    34 }
    35 
    36 class ChangHong3 implements ITV3 {
    37 
    38     @Override
    39     public void play() {
    40         System.out.println("长虹电视机333,打开");
    41     }
    42 }

    四、总结

      1、低层模块尽量都要有抽象类或接口,或者两者都有,程序稳定性更好;

      2、变量的声明类型尽量是抽象类或接口,这样我们的变量引用和实际对象间,就存在一个缓冲层,利于程序扩展和优化;

      3、继承时遵循里式替换原则;

     

  • 相关阅读:
    HDU3342有向图判圈DFS&&拓扑排序法
    HUD2647 Reward_反向建图拓扑排序
    hdu2094—看似拓扑实际上是一道思维题
    hdu4686 简单的矩阵快速幂求前n项和
    HDU2256&&HDU4565:给一个式子的求第n项的矩阵快速幂
    hdu2254 奥运 矩阵的应用
    Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
    HDU 2087 剪花布条【最长不重复子串】【KMP】【水题】【模板题】
    HDU 1711 Number Sequence【KMP】【模板题】【水题】(返回匹配到的第一个字母的位置)
    HDU 1711 Number Sequence【KMP】【模板题】【水题】(返回匹配到的第一个字母的位置)
  • 原文地址:https://www.cnblogs.com/niujifei/p/14152594.html
Copyright © 2011-2022 走看看