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();
        }
    }
}
相关推荐
Jackson@ML2 小时前
用Visual Studio Code最新版开发C#应用程序
ide·vscode·c#
她说彩礼65万3 小时前
C# 代理模式
开发语言·c#·代理模式
张人玉6 小时前
TCP 的三次握手和四次挥手
网络·tcp/ip·c#
曹牧6 小时前
C#:三元运算符
开发语言·c#
m0_748248029 小时前
C++与C#布尔类型深度解析:从语言设计到跨平台互操作
c++·stm32·c#
LeonDL16810 小时前
【通用视觉框架】基于C#+VisionPro开发的视觉框架软件,全套源码,开箱即用
人工智能·c#·visionpro·通用视觉框架·机器视觉框架·视觉框架软件·机器视觉软件
王道长服务器 | 亚马逊云10 小时前
AWS + 苹果CMS:影视站建站的高效组合方案
服务器·数据库·搜索引擎·设计模式·云计算·aws
一抓掉一大把11 小时前
RuoYi .net-实现商城秒杀下单(redis,rabbitmq)
redis·mysql·c#·rabbitmq·.net
在未来等你11 小时前
AI Agent设计模式 Day 1:ReAct模式:推理与行动的完美结合
设计模式·llm·react·ai agent·plan-and-execute
睡前要喝豆奶粉12 小时前
在.NET Core Web Api中使用阿里云OSS
阿里云·c#·.netcore