接口隔离原则:服务调用者不能多要!服务的提供者不能少给这是硬性要求,但是服务的调用者不能多要这是软性要求,实现服务调用者不能多要需要我们在设计时实现。下面是已经设计好的、符合接口隔离原则!下面还有一个不符合接口隔离原则的实例代码。
    符合

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace ConsoleApp49
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Driver driver = new Driver(new Car());
    13. driver.Drive();
    14. }
    15. }
    16. class Driver
    17. {
    18. private IVehicle _vehicle;
    19. public Driver( IVehicle vehicle)
    20. { // 创建的是IVehicle接口类型,IVehicle接口不包含开炮方法,即使下面ITank接口方法继承了IVehicle,也不包含开炮方法!(是一个概念!!)
    21. _vehicle = vehicle;
    22. }
    23. public void Drive()
    24. {
    25. _vehicle.Run();
    26. //在这里调用不到坦克开炮方法,因为(是一个概念) ITank属于IVehicle 但IVehicle不属于ITruck
    27. }
    28. }
    29. interface IVehicle
    30. {
    31. void Run();
    32. }
    33. class Car : IVehicle
    34. {
    35. public void Run()
    36. {
    37. Console.WriteLine("皮皮虾我们走!");
    38. }
    39. }
    40. class Truck : IVehicle
    41. {
    42. public void Run()
    43. {
    44. Console.WriteLine("卡车!!!");
    45. }
    46. }
    47. interface IWeapon
    48. {
    49. void Fire();
    50. }
    51. interface ITank :IVehicle, IWeapon
    52. {//继承IVehicle,IWeapon 两个接口!下面坦克实例实现了开炮和开车方法。
    53. }
    54. class LightTank : ITank
    55. {
    56. public void Fire()
    57. {
    58. Console.WriteLine("开火!");
    59. }
    60. public void Run()
    61. {
    62. Console.WriteLine("跑!");
    63. }
    64. }
    65. class MediumTank : ITank
    66. {
    67. public void Fire()
    68. {
    69. Console.WriteLine("开火!!");
    70. }
    71. public void Run()
    72. {
    73. Console.WriteLine("跑!!");
    74. }
    75. }
    76. class HeavyTank : ITank
    77. {
    78. public void Fire()
    79. {
    80. Console.WriteLine("开火!!!");
    81. }
    82. public void Run()
    83. {
    84. Console.WriteLine("跑!!!");
    85. }
    86. }
    87. }

    下面代码是不符合“接口隔离原则”的,假如我要调用LightTank的Run方法,需要在Driver类里面创建ITank接口的字段和实例,创建成功了之后,还会返回Fire和Run两个方法。但是我只需要Run方法,还有另外一个问题,假如我要调用Car的Run方法呢?还要去改Driver类里面的接口实例。所以下面这段代码功能调用者多调用了一个方法,还有我想调用另外一个接口的方法还需要改接口实例。
    不符合

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp49
    {
        class Program
        {
            static void Main(string[] args)
            {
                Driver driver = new Driver(new Car());
                driver.Drive();
    
            }
        }
        class Driver
        {
            private IVehicle _vehicle;
            public Driver(IVehicle vehicle)
            {
                _vehicle = vehicle;
    
            }
            public void Drive()
            {
                _vehicle.Run();
            }
        }
        interface IVehicle
        {
            void Run();
        }
        class Car : IVehicle
        {
            public void Run()
            {
                Console.WriteLine("皮皮虾我们走!");
            }
        }
        class Truck : IVehicle
        {
            public void Run()
            {
                Console.WriteLine("卡车!!!");
            }
        }
        interface ITank 
        {
            void Run();
            void Fire();
        }
        class LightTank : ITank
        {
            public void Fire()
            {
                Console.WriteLine("开火!");
            }
    
            public void Run()
            {
                Console.WriteLine("跑!");
            }
        }
        class MediumTank : ITank
        {
            public void Fire()
            {
                Console.WriteLine("开火!!");
            }
    
            public void Run()
            {
                Console.WriteLine("跑!!");
            }
        }
        class HeavyTank : ITank
        {
            public void Fire()
            {
                Console.WriteLine("开火!!!");
            }
    
            public void Run()
            {
                Console.WriteLine("跑!!!");
            }
        }
    }