所谓依赖倒置原则(Dependence Inversion Principle)就是要依赖于抽象,不要依赖于具体。编程
简单的说就是对抽象进行编程,不要对实现进行编程,这样就下降了客户与实现模块间的耦合。this
面向过程的开发,上层调用下层,上层依赖于下层,当下层剧烈变化时,上层也要跟着变化,这就会致使模块的复用性下降并且大大提升了开发的成本。spa
面向对象的开发很好的解决了这个问题,通常的状况下抽象的变化几率很小,让用户程序依赖于抽象,实现的细节也依赖于抽象。即便实现细节不断变化,只要抽象不变,客户程序就不须要变化。设计
这大大下降了客户程序域实现细节的耦合度。code
好比一个合资汽车公司如今要求开发一个自动驾驶系统,只要汽车上安装上这个系统,就能够实现无人驾驶,该系统能够在福特车系列和本田车系列上使用。面向过程的结构图:对象
面向过程的设计:blog
public class HondaCar { public void Run() { Console.WriteLine("本田车启动了!"); } public void Turn() { Console.WriteLine("本田车拐弯了!"); } public void Stop() { Console.WriteLine("本田车中止了!"); } } public class FordCar { public void Run() { Console.WriteLine("福特车启动了!"); } public void Turn() { Console.WriteLine("福特车拐弯了!"); } public void Stop() { Console.WriteLine("福特车中止了!"); } } public class AutoSystem { public enum CarType{ Ford,Fonda} private HondaCar hondcar=new HondaCar(); private FordCar fordcar=new FordCar(); private CarType type; public AutoSystem(CarType carType) { this.type = carType; } public void RunCar() { if (this.type == CarType.Fonda) { hondcar.Run(); } else if (this.type == CarType.Ford) { fordcar.Run(); } } public void StopCar() { if (this.type == CarType.Fonda) { hondcar.Stop(); } else if (this.type == CarType.Ford) { fordcar.Stop(); } } public void TurnCar() { if (this.type == CarType.Fonda) { hondcar.Turn(); } else if (this.type == CarType.Ford) { fordcar.Turn(); } } }
显然这个实现代码也可知足如今的需求。
可是如何如今公司业务规模扩大了,该自动驾驶系统还要把吉普车也兼容了。这些就须要修改AutoSystem类以下:ip
public class AutoSystem { public enum CarType{ Ford,Fonda} private HondaCar hondcar=new HondaCar(); private FordCar fordcar=new FordCar(); private CarType type; public AutoSystem(CarType carType) { this.type = carType; } public void RunCar() { if (this.type == CarType.Fonda) { hondcar.Run(); } else if (this.type == CarType.Ford) { fordcar.Run(); } } public void StopCar() { if (this.type == CarType.Fonda) { hondcar.Stop(); } else if (this.type == CarType.Ford) { fordcar.Stop(); } else if (this.type == CarType.Jeep) { jeep.Stop(); } } public void TurnCar() { if (this.type == CarType.Fonda) { hondcar.Turn(); } else if (this.type == CarType.Ford) { fordcar.Turn(); } else if (this.type == CarType.Jeep) { jeep.Turn(); } } }
经过代码分析得知,上述代码也确实知足了需求,可是软件是不断变化的,软件的需求也是变化的,若是未来业务又扩大了,该自动驾驶系统还有能实现通用、三菱、大众汽车,这样咱们不得不又要修改AutoSystem类了。
这样会致使系统愈来愈臃肿,愈来愈大,并且依赖愈来愈多低层模块,只有低层模块变更,AutoSystem类就不得不跟着变更,致使系统设计变得很是脆弱和僵硬。
致使上面所述问题一个缘由是,含有高层策略的模块,如AutoSystem模块,依赖于它所控制的低层的具体细节的模块(如FordCar和HondaCar)。
若是能使AutoSystem模块独立于它所控制的具体细节,而是依赖抽象,那么咱们就能够服用它了。这就是面向对象中的“依赖倒置”机制。ci
实现代码以下:开发
public interface ICar { void Run(); void Stop(); void Turn(); } public class HondaCar:ICar { public void Run() { Console.WriteLine("本田车启动了!"); } public void Turn() { Console.WriteLine("本田车拐弯了!"); } public void Stop() { Console.WriteLine("本田车中止了!"); } } public class FordCar :ICar { public void Run() { Console.WriteLine("福特车启动了!"); } public void Turn() { Console.WriteLine("福特车拐弯了!"); } public void Stop() { Console.WriteLine("福特车中止了!"); } } public class Jeep:ICar { public void Run() { Console.WriteLine("福特车启动了!"); } public void Turn() { Console.WriteLine("福特车拐弯了!"); } public void Stop() { Console.WriteLine("福特车中止了!"); } } public class AutoSystem { private ICar car; public AutoSystem(ICar car) { this.car = car; } public void RunCar() { this.car.Run(); } public void StopCar() { this.car.Stop(); } public void TurnCar() { this.car.Turn(); } }
如今Autosystem系统依赖于ICar这个抽象,而与具体的实现细节HondaCar:和FordCar无关,因此实现细节的变化不会影响AutoSystem.对于实现细节只要实现ICar便可。即实现细节依赖于ICar抽象。综上所述:一个应用中的重要策略决定及业务 正是在这些高层的模块中。也正是这些模块包含这应用的特性。可是,当这些模块依赖于低层模块时,低层模块的修改比较将直接影响到他们,迫使它们也改变。这种状况是荒谬的。应该是处于高层的模块去迫使那些低层的模块发生改变。处于高层的模块应优先于低层的模块。不管如何高层模块也不该该依赖于低层模块。并且咱们想可以复用的是高层的模块,只有高层模块独立于低层模块时,复用才有可能。总之,高层次的模块不该该依赖于低层次的模块,它们都应该依赖于抽象。抽象不该该依赖于具体,具体应该依赖于抽象。