• 设计模式七大原则-接口隔离原则InterfaceSegregation


    接口隔离原则

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

    案例:

    类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类c来说不是最小接口,那么类B和类D必须去实现他们不需要的方法(造成浪费)。

    1. public class interfaceSegregation {
    2. public static void main(String[] args) {
    3. A a = new A();
    4. a.depend1(new B());
    5. a.depend2(new B());
    6. a.depend3(new B());
    7. C c = new C();
    8. c.depend1(new D());
    9. c.depend4(new D());
    10. c.depend5(new D());
    11. }
    12. }
    13. interface interface1{
    14. void method1();
    15. void method2();
    16. void method3();
    17. void method4();
    18. void method5();
    19. }
    20. class B implements interface1{
    21. @Override
    22. public void method1() {
    23. System.out.println("B实现了method1");
    24. }
    25. @Override
    26. public void method2() {
    27. System.out.println("B实现了method2");
    28. }
    29. @Override
    30. public void method3() {
    31. System.out.println("B实现了method3");
    32. }
    33. @Override
    34. public void method4() {
    35. System.out.println("B实现了method4");
    36. }
    37. @Override
    38. public void method5() {
    39. System.out.println("B实现了method5");
    40. }
    41. }
    42. class D implements interface1{
    43. @Override
    44. public void method1() {
    45. System.out.println("D实现了method1");
    46. }
    47. @Override
    48. public void method2() {
    49. System.out.println("D实现了method2");
    50. }
    51. @Override
    52. public void method3() {
    53. System.out.println("D实现了method3");
    54. }
    55. @Override
    56. public void method4() {
    57. System.out.println("D实现了method4");
    58. }
    59. @Override
    60. public void method5() {
    61. System.out.println("D实现了method5");
    62. }
    63. }
    64. class A {
    65. void depend1(interface1 interface1){
    66. interface1.method1();
    67. }
    68. void depend2(interface1 interface1){
    69. interface1.method2();
    70. }
    71. void depend3(interface1 interface1){
    72. interface1.method3();
    73. }
    74. }
    75. class C {
    76. void depend1(interface1 interface1){
    77. interface1.method1();
    78. }
    79. void depend4(interface1 interface1){
    80. interface1.method4();
    81. }
    82. void depend5(interface1 interface1){
    83. interface1.method5();
    84. }
    85. }

    按隔离原则应当这样处理:将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则。

    按照实际情况将Interface1分为Interface1、Interface2、Interface3 等,代码略。

    在项目中常见的业务类直接依赖另外一个业务类的时候往往也是违背接口隔离原则的,在实际项目中,出于开发速度、维护成本等原因,通常不会过于细化接口。

  • 相关阅读:
    ABTest如何计算最小样本量-工具篇
    【Redis学习笔记】第三章 Redis通用指令
    126. SAP UI5 进阶 - JSON 模型字段里的值,显示在最终 UI5 界面上的奥秘分析
    [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹
    远程桌面登录Windows云服务器报错0x112f
    vim常用操作
    【Java 进阶篇】Cookie 使用详解
    vs调试报错 error LNK2001: 无法解析的外部符号 main 文件MSVCRT.lib(crtexe.obj)
    Java_StringBuilder类_StringBuffer类
    armbian 安裝配置教程
  • 原文地址:https://blog.csdn.net/stan_ko/article/details/139583127