(转)重述——依赖倒置原则

[size=medium][b]依赖倒置原则(Dependence Inversion Principle )[/b][/size]

所谓依赖倒置原则就是要[b]依赖于抽象,不要依赖于具体[/b]。简单的说就是[b]对抽象(或 接口)进行编程,不要对实现进行编程[/b],这样就降低了客户与实现模块间的耦合。
面向过程的开发,上层调用下层,上层依赖于下层,当下层剧烈变化时,上层也要跟着变化,这就会导致模块的复用性降低而且大大提高了开发的成本。
面向对象的开发很好的解决了这个问题,一般的情况下抽象的变化概率很小,让用户程序依赖于抽象,实现的细节也依赖于抽象。即使实现细节不断变化,只要抽象不变,客户程序就不需要变化。这大大降低了客户程序域实现细节的耦合度。
比如一个合资汽车公司现在要求开发一个自动驾驶系统,只要汽车上安装上这个系统,就可以实现无人驾驶,该系统可以在福特车系列和本田车系列上使用。面向过程的结构图:
[img]http://dl2.iteye.com/upload/attachment/0090/6434/4bdef77e-a201-3228-beb8-9a8a85e402da.jpg[/img]
实现代码如下:
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类如下:

public class AutoSystem{
public enum CarType{ Ford,Fonda,Jeep}
private HondaCar hondcar=new HondaCar();
private FordCar fordcar=new FordCar();
private Jeep jeep = new Jeep();
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();
}else if (this.type == CarType.Jeep){
jeep.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模块独立于它所控制的具体细节,而是依赖抽象,那么我们就可以服用它了。这就是面向对象中的“依赖倒置”机制。如下类图:
[img]http://dl2.iteye.com/upload/attachment/0090/6436/d3776bff-4760-3ed4-90f7-52182a51f0c4.jpg[/img]
实现代码如下:
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抽象。

综上所述:一个应用中的重要策略决定及业务 正是在这些高层的模块中。也正是这些模块包含这应用的特性。但是,当这些模块依赖于低层模块时,低层模块的修改比较将直接影响到他们,迫使它们也改变。这种情况是荒谬的。

应该是处于高层的模块去迫使那些低层的模块发生改变。处于高层的模块应优先于低层的模块。无论如何高层模块也不应该依赖于低层模块。而且我们想能够复用的是高层的模块,只有高层模块独立于低层模块时,复用才有可能。

总之,高层次的模块不应该依赖于低层次的模块,它们都应该依赖于抽象。抽象不应该依赖于具体,具体应该依赖于抽象。

原文链接:[url]http://www.cnblogs.com/shaosks/archive/2012/02/07/2341639.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值