概述
开放封闭原则是面向对象的设计的中级目标,而依赖倒置原则是实现开放封闭原则的基础。
如果开放封闭原则是设计大楼的蓝田,那么依赖倒置原则就是大楼的钢铁架构。
高层模块(调用者)不应该依赖于低层模块(被调用者),两个都应该依赖于抽象。依赖倒置原则的本质是通过接口或抽象使各个模块的实现独立,彼此互补影响,从而实现模块间的松耦合。
未使用依赖倒置原则的代码
/// <summary> /// 重复代码很多, Singer作为高层模块,严重依赖底层模块 不符合依赖倒置原则,开放封闭原则 -单一职责原则 /// </summary> public class Singer { public void SingSong(ChineseSong chineseSong) { Console.WriteLine($"歌手正在唱{chineseSong.GetSongWords()}"); } public void SingSongEnglish(EnglishSong englishSong) { Console.WriteLine($"歌手正在唱{englishSong.GetSongWords()}"); } } public class ChineseSong { public string GetSongWords() { return "中国歌曲"; } } public class EnglishSong { public string GetSongWords() { return "外国歌曲"; } }
//C#控制台调用 Singer singer = new Singer(); singer.SingSong(new ChineseSong()); singer.SingSongEnglish(new EnglishSong());
结论:重复代码很多, Singer作为高层模块,严重依赖底层模块 不符合依赖倒置原则,开放封闭原则 -单一职责原则。
面向接口编程的代码
public interface ISongWords { public string GetSongWords(); } public class ChineseSong2: ISongWords { public string GetSongWords() { return "中国歌曲"; } } public class EnglishSong2 : ISongWords { public string GetSongWords() { return "外国歌曲"; } } public class Singer2 { public void SingSong(ISongWords songWords) { Console.WriteLine($"歌手正在唱{songWords.GetSongWords()}"); } }
//C#控制台调用 Singer2 singer2 = new Singer2(); ISongWords songWords = new ChineseSong2(); singer2.SingSong(songWords);
依赖倒置原则的代码
抽象不应该依赖细节 细节应该依赖抽象。
public interface ICar { void Run(); } public class Benz : ICar { public void Run() { Console.WriteLine("奔驰汽车在本跑"); } } public class AoDi : ICar { public void Run() { Console.WriteLine("奥迪汽车在本跑"); } } public interface IDriver { //在接口或类中 将要注入的服务对象,以参数的形式直接注入,我们称之为接口注入; void Drive(ICar car); void Drive(); void SetCar(ICar car); } public class Student : IDriver { private ICar _car; /// <summary> /// 接口注入 /// </summary> /// <param name="car"></param> public void Drive(ICar car) { car.Run(); } public void Drive() { _car.Run(); } /// <summary> /// 属性注入 /// </summary> /// <param name="car"></param> public void SetCar(ICar car) { _car=car; } } public class Teacher : IDriver { private readonly ICar _car; /// <summary> /// 构造函数注入 /// </summary> /// <param name="car"></param> public Teacher(ICar car) { _car = car; } public void Drive(ICar car) { throw new NotImplementedException(); } public void SetCar(ICar car) { throw new NotImplementedException(); } public void Drive() { _car.Run(); } }
//C#控制台调用 IDriver driver = new Student(); ICar car = new Benz(); driver.Drive(car); driver.SetCar(car); driver.Drive(); IDriver driver1 = new Teacher(car); driver1.Drive(car);
总结
以上就是关于依赖倒置原则的内容,有何疑问,欢迎与我沟通交流。
标签:原则,必知,car,void,ICar,class,new,倒置,public From: https://www.cnblogs.com/mhg215/p/17097888.html