• C#设计模式六大原则之依赖倒置原则


    C#设计模式六大原则是单一职责原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特法则、开闭原则。它们不是要我们刻板的遵守,而是根据实际需要灵活运用。只要对它们的遵守程度在一个合理的范围内,努为做到一个良好的设计。以下介绍C#依赖倒置原则。

    依赖倒置原则(Dependence Inversion Principle)

    依赖倒置原则(Dependence Inversion Principle)是程序要依赖于抽象接口,不要依赖于具体实现。简单的说就是要求对抽象进行编程,不要对实现进行编程,这样就降低了客户与实现模块间的耦合。高层模块不应该依赖低层模块,二者都应该依赖其抽象;抽象不应该依赖细节;细节应该依赖抽象。面向对象的开发很好的解决了这个问题,一般情况下抽象的变化概率很小,让用户程序依赖于抽象,实现的细节也依赖于抽象。即使实现细节不断变动,只要抽象不变,客户程序就不需要变化。这大大降低了客户程序与实现细节的耦合度。

    例如,

    1)一般的反面设计实现

    1. using System;
    2. namespace ConsoleApplication
    3. {
    4. public class HondaCar
    5. {
    6. public void Run()
    7. {
    8. Console.WriteLine("本田开始启动");
    9. }
    10. public void Turn()
    11. {
    12. Console.WriteLine("本田开始转弯");
    13. }
    14. public void Stop()
    15. {
    16. Console.WriteLine("本田开始停车");
    17. }
    18. }
    19. public class FordCar
    20. {
    21. public void Run()
    22. {
    23. Console.WriteLine("福特开始启动");
    24. }
    25. public void Turn()
    26. {
    27. Console.WriteLine("福特开始转弯");
    28. }
    29. public void Stop()
    30. {
    31. Console.WriteLine("福特开始停车");
    32. }
    33. }
    34. public class BmwCar
    35. {
    36. public void Run()
    37. {
    38. Console.WriteLine("福特开始启动");
    39. }
    40. public void Turn()
    41. {
    42. Console.WriteLine("福特开始转弯");
    43. }
    44. public void Stop()
    45. {
    46. Console.WriteLine("福特开始停车");
    47. }
    48. }
    49. public class AutoSystem
    50. {
    51. public enum CarType
    52. {
    53. Ford, Honda, Bmw
    54. };
    55. HondaCar hcar = new HondaCar();
    56. FordCar fcar = new FordCar();
    57. BmwCar bcar = new BmwCar();
    58. private CarType type;
    59. public AutoSystem(CarType type)
    60. {
    61. this.type = type;
    62. }
    63. public void RunCar()
    64. {
    65. if (type == CarType.Ford)
    66. {
    67. fcar.Run();
    68. }
    69. else if (type == CarType.Honda)
    70. {
    71. hcar.Run();
    72. }
    73. else if (type == CarType.Bmw)
    74. {
    75. bcar.Run();
    76. }
    77. }
    78. public void TurnCar()
    79. {
    80. if (type == CarType.Ford)
    81. {
    82. fcar.Turn();
    83. }
    84. else if (type == CarType.Honda)
    85. {
    86. hcar.Turn();
    87. }
    88. else if (type == CarType.Bmw)
    89. {
    90. bcar.Turn();
    91. }
    92. }
    93. public void StopCar()
    94. {
    95. if (type == CarType.Ford)
    96. {
    97. fcar.Stop();
    98. }
    99. else if (type == CarType.Honda)
    100. {
    101. hcar.Stop();
    102. }
    103. else if (type == CarType.Bmw)
    104. {
    105. bcar.Stop();
    106. }
    107. }
    108. }
    109. class Program
    110. {
    111. public static void Main()
    112. {
    113. AutoSystem autoSystem = new AutoSystem(AutoSystem.CarType.Honda);
    114. autoSystem.RunCar();
    115. autoSystem.TurnCar();
    116. autoSystem.StopCar();
    117. Console.ReadKey();
    118. }
    119. }
    120. }

     

     2)依赖倒置原则的实现

    1. using System;
    2. namespace ConsoleApplication
    3. {
    4. public interface ICar
    5. {
    6. void Run();
    7. void Turn();
    8. void Stop();
    9. }
    10. public class BmwCar : ICar
    11. {
    12. public void Run()
    13. {
    14. Console.WriteLine("宝马开始启动");
    15. }
    16. public void Turn()
    17. {
    18. Console.WriteLine("宝马开始转弯");
    19. }
    20. public void Stop()
    21. {
    22. Console.WriteLine("宝马开始停车");
    23. }
    24. }
    25. public class FordCar : ICar
    26. {
    27. public void Run()
    28. {
    29. Console.WriteLine("福特开始启动");
    30. }
    31. public void Turn()
    32. {
    33. Console.WriteLine("福特开始转弯");
    34. }
    35. public void Stop()
    36. {
    37. Console.WriteLine("福特开始停车");
    38. }
    39. }
    40. public class HondaCar : ICar
    41. {
    42. public void Run()
    43. {
    44. Console.WriteLine("本田开始启动");
    45. }
    46. public void Turn()
    47. {
    48. Console.WriteLine("本田开始转弯");
    49. }
    50. public void Stop()
    51. {
    52. Console.WriteLine("本田开始停车");
    53. }
    54. }
    55. public class AutoSystem
    56. {
    57. private ICar icar;
    58. public AutoSystem(ICar icar)
    59. {
    60. this.icar = icar;
    61. }
    62. public void RunCar()
    63. {
    64. icar.Run();
    65. }
    66. public void TurnCar()
    67. {
    68. icar.Turn();
    69. }
    70. public void StopCar()
    71. {
    72. icar.Stop();
    73. }
    74. }
    75. class Program
    76. {
    77. public static void Main()
    78. {
    79. AutoSystem autoSystem = new AutoSystem(new HondaCar());
    80. autoSystem.RunCar();
    81. autoSystem.TurnCar();
    82. autoSystem.StopCar();
    83. Console.ReadKey();
    84. }
    85. }
    86. }

     

  • 相关阅读:
    华为云服务器安装Linux并实现本地连接访问
    在 JavaScript 中循环遍历数组的多种方法
    ReentrantLock源码分析
    国密GmSSL v2版本命令行方式生成国密sm2私钥、公钥、签名和验证签名
    Find My手机保护壳|苹果Find My与手机保护壳结合,智能防丢,全球定位
    React:构建Web应用的未来
    RK3568开发笔记(九):开发板buildroot固件调通RS485口,运行项目中RS485协议调试工具Demo
    【Linux进程间通信】 管道
    9 - 类
    WebSocket 入门案例
  • 原文地址:https://blog.csdn.net/lwf3115841/article/details/133773973