当前位置: 首页 > news >正文

【读书学习记录】【设计模式】依赖倒置原则

依赖倒置原则:
1.定义:系统的高层部分不应该直接依赖于系统的底层部分,相反,他们应该依赖于某种抽象
2.优点:可以减少类间的耦合性,提高系统的稳定性,降低并行开发引起的风险,提高代码的可读性和可维护性。

3.通过代码去理解(场景-解决方式):

【场景】

namespace ConsoleApp1
{public enum Relationship{Parent,Child,Sibling}public class Person{public String Name { get; set; }}// low-levelpublic class Relationships{private List<(Person, Relationship, Person)> relations= new List<(Person, Relationship, Person)>();public void AddParentAndChild(Person parent, Person child){relations.Add((parent, Relationship.Parent, child));relations.Add((child, Relationship.Child, parent));}public List<(Person, Relationship, Person)> Relations => relations;}// high-levelpublic class Research{public Research(Relationships relationships){var relations = relationships.Relations;foreach (var r in relations.Where(x => x.Item1.Name == "John" &&x.Item2 == Relationship.Parent)){Console.WriteLine($"John has a child called {r.Item3.Name}");}}static void Main(string[] args){var parent = new Person { Name = "John" };var child1 = new Person { Name = "Chris" };var child2 = new Person { Name = "Matt" };var relationships = new Relationships();relationships.AddParentAndChild(parent, child1);relationships.AddParentAndChild(parent, child2);new Research(relationships);}}
}

 

违背了依赖倒置原则

 

【解决方式】

namespace ConsoleApp1
{public enum Relationship{Parent,Child,Sibling}public class Person{public String Name { get; set; }}public interface IRelationshipBrowser{IEnumerable<Person> FindAllChildrenOf(string name);}// low-levelpublic class Relationships: IRelationshipBrowser{private List<(Person, Relationship, Person)> relations= new List<(Person, Relationship, Person)>();public void AddParentAndChild(Person parent, Person child){relations.Add((parent, Relationship.Parent, child));relations.Add((child, Relationship.Child, parent));}public List<(Person, Relationship, Person)> Relations => relations;public IEnumerable<Person> FindAllChildrenOf(string name){return relations.Where(x => x.Item1.Name == "John" &&x.Item2 == Relationship.Parent).Select(r => r.Item3);}}// high-levelpublic class Research{//public Research(Relationships relationships)//{//    var relations = relationships.Relations;//    foreach (var r in relations.Where(//        x => x.Item1.Name == "John" &&//        x.Item2 == Relationship.Parent))//    {//        Console.WriteLine($"John has a child called {r.Item3.Name}");//    }//}public Research(IRelationshipBrowser browser){foreach (var p in browser.FindAllChildrenOf("John")){Console.WriteLine($"John has a child called {p.Name}");}}static void Main(string[] args){var parent = new Person { Name = "John" };var child1 = new Person { Name = "Chris" };var child2 = new Person { Name = "Matt" };var relationships = new Relationships();relationships.AddParentAndChild(parent, child1);relationships.AddParentAndChild(parent, child2);new Research(relationships);}}
}

【对比】

image

 

http://www.zskr.cn/news/62928.html

相关文章:

  • navicat 17 安装破解教程【2025 最新版激活补丁 + 详细安装步骤​】
  • MATLAB实现多分布拉丁超立方采样实用程序
  • 2025年下半年内蒙古消防检测/房屋结构检测/承载力检测公司推荐
  • 深度变分多元信息瓶颈框架解析
  • 适合敏感肌的保湿面霜推荐:皮肤科医生权威推荐 敏感肌保湿面霜榜
  • 2025年11月蒸汽发生器品牌推荐榜:综合实力与创新技术排行解析
  • 基于MATLAB实现的PSO优化BP神经网络
  • 懒人必藏!小程序推荐休闲游戏,十款挂机休闲神作,躺着也能享受乐趣
  • 租用美国服务器的网站能做些什么
  • 再议求函数定义域中的一类难懂易错题目
  • 长文本溢出展开/收起功能
  • 2025 年 11 月应力仪厂家权威推荐榜:全自动/UTG玻璃/便携式智能/多光源/光伏玻璃/微晶玻璃/手持式/幕墙玻璃/紫外红外/电子玻璃应力仪精选指南
  • 2025年11月成都监理公司推荐榜单:基于资质、服务与口碑的综合排行
  • 2025年中国深海环境模拟装备公司TOP5推荐:卡普蒂姆的发
  • quic协议中Connection ID的协商机制
  • 主题:训练循环定制化实战:以CustomTrainer.fit为例
  • 挑战热极限:如何构建可靠的175℃ AC/DC电源系统
  • 2025年下半年木纹转印/栏杆制作/喷漆/喷塑厂家前五推荐榜单:专业选择指南
  • 这里说的go的OS 线程不会卡,什么意思,是指os线程可以处理其他的程序还是处理其他的http请求,os线程是什么
  • nvm 是什么?
  • 2025年专业靠谱口碑不错的国产操作系统公司推荐,高安全适配
  • 2025年天津包装印刷服务公司排名:天津福森数码科技有限公司
  • 2025年11月北京遗产继承律师服务全面评测与Top5推荐指南
  • 2025英国留学中介机构十强
  • 2025年BIP系统性能大比拼,口碑最佳的是它,协同云/制造云/用友 T3/好生意/人力云/财务云/税务云/好业财BIP管理系统推荐排行榜
  • 2025液体水玻璃厂商哪家好TOP5权威推荐:源头直供避坑指
  • 2025年纸板桶生产厂家权威推荐榜单:方纸桶/生产纸板桶/全纸桶源头厂家精选
  • 基于FPGA的雷达信号处理设计工具包分享 - 实践
  • 如何判断一个函数是否适合定义为inline函数?
  • 2025香港正规留学中介排名