客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上

image.png

优化前:

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

_

优化后:

image.png

image.png

优化后

  1. interface Interface1 {
  2. void operation1();
  3. }
  4. // 接口2
  5. interface Interface2 {
  6. void operation2();
  7. void operation3();
  8. }
  9. // 接口3
  10. interface Interface3 {
  11. void operation4();
  12. void operation5();
  13. }
  14. class B implements Interface1, Interface2 {
  15. public void operation1() {
  16. System.out.println("B 实现了 operation1");
  17. }
  18. public void operation2() {
  19. System.out.println("B 实现了 operation2");
  20. }
  21. public void operation3() {
  22. System.out.println("B 实现了 operation3");
  23. }
  24. }
  25. class D implements Interface1, Interface3 {
  26. public void operation1() {
  27. System.out.println("D 实现了 operation1");
  28. }
  29. public void operation4() {
  30. System.out.println("D 实现了 operation4");
  31. }
  32. public void operation5() {
  33. System.out.println("D 实现了 operation5");
  34. }
  35. }
  36. class A { // A 类通过接口Interface1,Interface2 依赖(使用) B类,但是只会用到1,2,3方法
  37. public void depend1(Interface1 i) {
  38. i.operation1();
  39. }
  40. public void depend2(Interface2 i) {
  41. i.operation2();
  42. }
  43. public void depend3(Interface2 i) {
  44. i.operation3();
  45. }
  46. }
  47. class C { // C 类通过接口Interface1,Interface3 依赖(使用) D类,但是只会用到1,4,5方法
  48. public void depend1(Interface1 i) {
  49. i.operation1();
  50. }
  51. public void depend4(Interface3 i) {
  52. i.operation4();
  53. }
  54. public void depend5(Interface3 i) {
  55. i.operation5();
  56. }
  57. }

java