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

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

依赖倒置原则(Dependence Inversion Principle)

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

例如,

1)一般的反面设计实现

using System;
namespace ConsoleApplication
{
    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 BmwCar
    {
        public void Run()
        {
            Console.WriteLine("福特开始启动");
        }
        public void Turn()
        {
            Console.WriteLine("福特开始转弯");
        }
        public void Stop()
        {
            Console.WriteLine("福特开始停车");
        }
    }
    public class AutoSystem
    {
        public enum CarType
        {
            Ford, Honda, Bmw
        };
        HondaCar hcar = new HondaCar();
        FordCar fcar = new FordCar();
        BmwCar bcar = new BmwCar();
        private CarType type;
        public AutoSystem(CarType type)
        {
            this.type = type;
        }
        public void RunCar()
        {
            if (type == CarType.Ford)
            {
                fcar.Run();
            }
            else if (type == CarType.Honda)
            {
                hcar.Run();
            }
            else if (type == CarType.Bmw)
            {
                bcar.Run();
            }
        }
        public void TurnCar()
        {
            if (type == CarType.Ford)
            {
                fcar.Turn();
            }
            else if (type == CarType.Honda)
            {
                hcar.Turn();
            }
            else if (type == CarType.Bmw)
            {
                bcar.Turn();
            }
        }
        public void StopCar()
        {
            if (type == CarType.Ford)
            {
                fcar.Stop();
            }
            else if (type == CarType.Honda)
            {
                hcar.Stop();
            }
            else if (type == CarType.Bmw)
            {
                bcar.Stop();
            }
        }
    }
    class Program
    {
        public static void Main()
        {
            AutoSystem autoSystem = new AutoSystem(AutoSystem.CarType.Honda);
            autoSystem.RunCar();
            autoSystem.TurnCar();
            autoSystem.StopCar();
            Console.ReadKey();
        }
    }
}

 

 2)依赖倒置原则的实现

using System;
namespace ConsoleApplication
{
    public interface ICar
    {
        void Run();
        void Turn();
        void Stop();
    }
    public class BmwCar : 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 HondaCar : ICar
    {
        public void Run()
        {
            Console.WriteLine("本田开始启动");
        }
        public void Turn()
        {
            Console.WriteLine("本田开始转弯");
        }
        public void Stop()
        {
            Console.WriteLine("本田开始停车");
        }
    }
    public class AutoSystem
    {
        private ICar icar;
        public AutoSystem(ICar icar)
        {
            this.icar = icar;
        }
        public void RunCar()
        {
            icar.Run();
        }
        public void TurnCar()
        {
            icar.Turn();
        }
        public void StopCar()
        {
            icar.Stop();
        }
    }
    class Program
    {
        public static void Main()
        {
            AutoSystem autoSystem = new AutoSystem(new HondaCar());
            autoSystem.RunCar();
            autoSystem.TurnCar();
            autoSystem.StopCar();
            Console.ReadKey();
        }
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忧郁的蛋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值