1.1 基本介绍


1) 客户端不应该依赖它不需要的接 口,即一个类对另一个类的依赖 应该建立在最小的接口上
2) 先看一张图:
image.png
3) 类A通过接口Interface1依赖类B,类C通过 接口Interface1依赖类D,如果接口 Interface1对于类A和类C来说不是最小接口, 那么类B和类D必须去实现他们不需要的方 法。
4) 按隔离原则应当这样处理: 将接口Interface1拆分为独立的几个接口, 类A和类C分别与他们需要的接口建立依赖 关系。也就是采用接口隔离原则

1.2 应用实例

1)类 A通过接口 Interface1依赖类 B,类 C通过接口 Interface1依赖类 D,请编写代码完成此应用实例。
image.png
2)看老师代码-没有使用接口隔离原则代码

  1. package com.atguigu.principle.segregation;
  2. public class Segregation1 {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. }
  6. }
  7. //接口
  8. interface Interface1 {
  9. void operation1();
  10. void operation2();
  11. void operation3();
  12. void operation4();
  13. void operation5();
  14. }
  15. class B implements Interface1 {
  16. public void operation1() {
  17. System.out.println("B 实现了 operation1");
  18. }
  19. public void operation2() {
  20. System.out.println("B 实现了 operation2");
  21. }
  22. public void operation3() {
  23. System.out.println("B 实现了 operation3");
  24. }
  25. public void operation4() {
  26. System.out.println("B 实现了 operation4");
  27. }
  28. public void operation5() {
  29. System.out.println("B 实现了 operation5");
  30. }
  31. }
  32. class D implements Interface1 {
  33. public void operation1() {
  34. System.out.println("D 实现了 operation1");
  35. }
  36. public void operation2() {
  37. System.out.println("D 实现了 operation2");
  38. }
  39. public void operation3() {
  40. System.out.println("D 实现了 operation3");
  41. }
  42. public void operation4() {
  43. System.out.println("D 实现了 operation4");
  44. }
  45. public void operation5() {
  46. System.out.println("D 实现了 operation5");
  47. }
  48. }
  49. class A { //A 类通过接口Interface1 依赖(使用) B类,但是只会用到1,2,3方法
  50. public void depend1(Interface1 i) {
  51. i.operation1();
  52. }
  53. public void depend2(Interface1 i) {
  54. i.operation2();
  55. }
  56. public void depend3(Interface1 i) {
  57. i.operation3();
  58. }
  59. }
  60. class C { //C 类通过接口Interface1 依赖(使用) D类,但是只会用到1,4,5方法
  61. public void depend1(Interface1 i) {
  62. i.operation1();
  63. }
  64. public void depend4(Interface1 i) {
  65. i.operation4();
  66. }
  67. public void depend5(Interface1 i) {
  68. i.operation5();
  69. }
  70. }

1.3 应传统方法的问题和使用接口隔离原则改进

1)类 A通过接口 Interface1依赖类 B,类 C通过接口 Interface1依赖类 D,如果接口 Interface1对于类 A和类 C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法
2)将接口 Interface1拆分为独立的几个接口,类 A和类 C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
3)接口 Interface1 中出现的方法,根据实际情况拆分为三个接口

image.png

4)代码实现

  1. package com.atguigu.principle.segregation.improve;
  2. public class Segregation1 {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. // 使用一把
  6. A a = new A();
  7. a.depend1(new B()); // A类通过接口去依赖B类
  8. a.depend2(new B());
  9. a.depend3(new B());
  10. C c = new C();
  11. c.depend1(new D()); // C类通过接口去依赖(使用)D类
  12. c.depend4(new D());
  13. c.depend5(new D());
  14. }
  15. }
  16. // 接口1
  17. interface Interface1 {
  18. void operation1();
  19. }
  20. // 接口2
  21. interface Interface2 {
  22. void operation2();
  23. void operation3();
  24. }
  25. // 接口3
  26. interface Interface3 {
  27. void operation4();
  28. void operation5();
  29. }
  30. class B implements Interface1, Interface2 {
  31. public void operation1() {
  32. System.out.println("B 实现了 operation1");
  33. }
  34. public void operation2() {
  35. System.out.println("B 实现了 operation2");
  36. }
  37. public void operation3() {
  38. System.out.println("B 实现了 operation3");
  39. }
  40. }
  41. class D implements Interface1, Interface3 {
  42. public void operation1() {
  43. System.out.println("D 实现了 operation1");
  44. }
  45. public void operation4() {
  46. System.out.println("D 实现了 operation4");
  47. }
  48. public void operation5() {
  49. System.out.println("D 实现了 operation5");
  50. }
  51. }
  52. class A { // A 类通过接口Interface1,Interface2 依赖(使用) B类,但是只会用到1,2,3方法
  53. public void depend1(Interface1 i) {
  54. i.operation1();
  55. }
  56. public void depend2(Interface2 i) {
  57. i.operation2();
  58. }
  59. public void depend3(Interface2 i) {
  60. i.operation3();
  61. }
  62. }
  63. class C { // C 类通过接口Interface1,Interface3 依赖(使用) D类,但是只会用到1,4,5方法
  64. public void depend1(Interface1 i) {
  65. i.operation1();
  66. }
  67. public void depend4(Interface3 i) {
  68. i.operation4();
  69. }
  70. public void depend5(Interface3 i) {
  71. i.operation5();
  72. }
  73. }