接口隔离原则定义如下:

客户端不应该依赖它不需要的接口
类间的依赖关系应该建立在最小的接口上
 其实通俗来理解就是,不要在一个接口里面放很多的方法,这样会显得这个类很臃肿不堪。接口应该尽量细化,一个接口对应一个功能模块,同时接口里面的方法应该尽可能的少,使接口更加轻便灵活。或许看到接口隔离原则这样的定义很多人会觉得和单一职责原则很像,但是这两个原则还是有着很鲜明的区别。接口隔离原则和单一职责原则的审视角度是不同的,单一职责原则要求类和接口职责单一,注重的是职责,是业务逻辑上的划分,而接口隔离原则要求方法要尽可能的少,是在接口设计上的考虑。
例如一个接口的职责包含10个方法,这10个方法都放在一个接口中,并且提供给多个模块访问,各个模块按照规定的权限来访问,并规定了“不使用的方法不能访问”,这样的设计是不符合接口隔离原则的,接口隔离原则要求“尽量使用多个专门的接口”,这里专门的接口就是指提供给每个模块的都应该是单一接口(即每一个模块对应一个接口),而不是建立一个庞大臃肿的接口来容纳所有的客户端访问。

1.错误示范

image.png
对于A,通过Interface1使用C中的1,2,3方法,Interface1其中4,5方法是多余的
对于C,通过Interface1使用D中的1,4,5方法,Interface1其中2,3方法是多余的
违背接口隔离原则,理论上A只是用1,2,3方法,其使用接口有且仅有1,2,3方法,同理B也一样。

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

2.正确示范

image.png

image.png

  1. package com.kris.designmode.principle.interfacesegregation;
  2. public class Segregation02 {
  3. public static void main(String[] args) {
  4. //测试
  5. A2 a2 = new A2();
  6. //A类通过接口依赖C类
  7. a2.depend1(new C2());
  8. a2.depend2(new C2());
  9. a2.depend3(new C2());
  10. B2 b2 = new B2();
  11. //B类通过接口依赖D类
  12. b2.depend1(new D2());
  13. b2.depend4(new D2());
  14. b2.depend5(new D2());
  15. }
  16. }
  17. interface Interface01{
  18. void operation1();
  19. }
  20. interface Interface02{
  21. void operation2();
  22. void operation3();
  23. }
  24. interface Interface03{
  25. void operation4();
  26. void operation5();
  27. }
  28. class C2 implements Interface01,Interface02{
  29. public void operation1() {
  30. System.out.println("B 中实现 operation1");
  31. }
  32. public void operation2() {
  33. System.out.println("B 中实现 operation2");
  34. }
  35. public void operation3() {
  36. System.out.println("B 中实现 operation3");
  37. }
  38. }
  39. class D2 implements Interface01,Interface03{
  40. public void operation1() {
  41. System.out.println("D 中实现 operation1");
  42. }
  43. public void operation4() {
  44. System.out.println("D 中实现 operation4");
  45. }
  46. public void operation5() {
  47. System.out.println("D 中实现 operation5");
  48. }
  49. }
  50. /**
  51. * Interface01,Interface02 依赖使用C类
  52. */
  53. class A2{
  54. public void depend1(Interface01 i){
  55. i.operation1();
  56. }
  57. public void depend2(Interface02 i){
  58. i.operation2();
  59. }
  60. public void depend3(Interface02 i){
  61. i.operation3();
  62. }
  63. }
  64. /**
  65. * B类通过Interface1,Interface03 依赖使用D类
  66. */
  67. class B2{
  68. public void depend1(Interface01 i){
  69. i.operation1();
  70. }
  71. public void depend4(Interface03 i){
  72. i.operation4();
  73. }
  74. public void depend5(Interface03 i){
  75. i.operation5();
  76. }
  77. }

打印

  1. B 中实现 operation1
  2. B 中实现 operation2
  3. B 中实现 operation3
  4. D 中实现 operation1
  5. D 中实现 operation4
  6. D 中实现 operation5