1.1 基本介绍


依赖倒转原则(Dependence Inversion Principle)是指:

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

1.2 应用实例

请编程完成 Person接收消息 的功能。

1)实现方案 1 + 分析说明

  1. package com.atguigu.principle.inversion;
  2. public class DependecyInversion {
  3. public static void main(String[] args) {
  4. Person person = new Person();
  5. person.receive(new Email());
  6. }
  7. }
  8. class Email {
  9. public String getInfo() {
  10. return "电子邮件信息: hello,world";
  11. }
  12. }
  13. //完成Person接收消息的功能
  14. //方式1分析
  15. //1. 简单,比较容易想到
  16. //2. 如果我们获取的对象是 微信,短信等等,则新增类,同时Perons也要增加相应的接收方法
  17. //3. 解决思路:引入一个抽象的接口IReceiver, 表示接收者, 这样Person类与接口IReceiver发生依赖
  18. // 因为Email, WeiXin 等等属于接收的范围,他们各自实现IReceiver 接口就ok, 这样我们就符号依赖倒转原则
  19. class Person {
  20. public void receive(Email email ) {
  21. System.out.println(email.getInfo());
  22. }
  23. }

2)实现方案 2(依赖倒转) + 分析说明

  1. package com.atguigu.principle.inversion.improve;
  2. public class DependecyInversion {
  3. public static void main(String[] args) {
  4. //客户端无需改变
  5. Person person = new Person();
  6. person.receive(new Email());
  7. person.receive(new WeiXin());
  8. }
  9. }
  10. //定义接口
  11. interface IReceiver {
  12. public String getInfo();
  13. }
  14. class Email implements IReceiver {
  15. public String getInfo() {
  16. return "电子邮件信息: hello,world";
  17. }
  18. }
  19. //增加微信
  20. class WeiXin implements IReceiver {
  21. public String getInfo() {
  22. return "微信信息: hello,ok";
  23. }
  24. }
  25. //方式2
  26. class Person {
  27. //这里我们是对接口的依赖
  28. public void receive(IReceiver receiver ) {
  29. System.out.println(receiver.getInfo());
  30. }
  31. }

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

1)接口传递

2)构造方法传递应用案例代码

3)setter方式传递

4)代码演示

  1. package com.atguigu.principle.inversion.improve;
  2. public class DependencyPass {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. ChangHong changHong = new ChangHong();
  6. // OpenAndClose openAndClose = new OpenAndClose();
  7. // openAndClose.open(changHong);
  8. //通过构造器进行依赖传递
  9. // OpenAndClose openAndClose = new OpenAndClose(changHong);
  10. // openAndClose.open();
  11. //通过setter方法进行依赖传递
  12. OpenAndClose openAndClose = new OpenAndClose();
  13. openAndClose.setTv(changHong);
  14. openAndClose.open();
  15. }
  16. }
  17. // 方式1: 通过接口传递实现依赖
  18. // 开关的接口
  19. // interface IOpenAndClose {
  20. // public void open(ITV tv); //抽象方法,接收接口
  21. // }
  22. //
  23. // interface ITV { //ITV接口
  24. // public void play();
  25. // }
  26. //
  27. // class ChangHong implements ITV {
  28. //
  29. // @Override
  30. // public void play() {
  31. // // TODO Auto-generated method stub
  32. // System.out.println("长虹电视机,打开");
  33. // }
  34. //
  35. // }
  36. //// 实现接口
  37. // class OpenAndClose implements IOpenAndClose{
  38. // public void open(ITV tv){
  39. // tv.play();
  40. // }
  41. // }
  42. // 方式2: 通过构造方法依赖传递
  43. // interface IOpenAndClose {
  44. // public void open(); //抽象方法
  45. // }
  46. // interface ITV { //ITV接口
  47. // public void play();
  48. // }
  49. // class OpenAndClose implements IOpenAndClose{
  50. // public ITV tv; //成员
  51. // public OpenAndClose(ITV tv){ //构造器
  52. // this.tv = tv;
  53. // }
  54. // public void open(){
  55. // this.tv.play();
  56. // }
  57. // }
  58. // 方式3 , 通过setter方法传递
  59. interface IOpenAndClose {
  60. public void open(); // 抽象方法
  61. public void setTv(ITV tv);
  62. }
  63. interface ITV { // ITV接口
  64. public void play();
  65. }
  66. class OpenAndClose implements IOpenAndClose {
  67. private ITV tv;
  68. public void setTv(ITV tv) {
  69. this.tv = tv;
  70. }
  71. public void open() {
  72. this.tv.play();
  73. }
  74. }
  75. class ChangHong implements ITV {
  76. @Override
  77. public void play() {
  78. // TODO Auto-generated method stub
  79. System.out.println("长虹电视机,打开");
  80. }
  81. }

1.4 依赖倒转原则的注意事项和细节


1) 低层模块尽量都要有抽象类或接口,或者两者都有,程序稳定性更好.
2) 变量的声明类型尽量是抽象类或接口, 这样我们的变量引用和实际对象间,就存在 一个缓冲层,利于程序扩展和优化
3) 继承时遵循里氏替换原则