当前位置: 首页 > 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.jsqmd.com/news/52956/

相关文章:

  • 2025年高端央国企工作服订制厂权威推荐榜单:高端工衣订制/高端工装订制/高端夏季工作服源头厂家精选
  • 2025年下半年消防检测/房屋结构/承载力/房屋鉴定公司前五推荐
  • navicat 17 安装破解教程【2025 最新版激活补丁 + 详细安装步骤​】
  • 杭州左工防潮:专为别墅打造的结构性防水防潮系统解决方案
  • MATLAB实现多分布拉丁超立方采样实用程序
  • 2025年AI排名搜索优化GEO公司推荐:惠州惠算GEO科技优势多
  • 2025年下半年内蒙古消防检测/房屋结构检测/承载力检测公司推荐
  • 深度变分多元信息瓶颈框架解析
  • 哪些精华适合敏感肌修复皮肤屏障?2025敏感肌屏障修护榜
  • 07.入门篇-了解Java历史
  • 2025年佛山储罐厂家最新推荐榜,SF储油罐、硫酸储罐、硝酸储罐、制冷储罐、聚焦产品品质与定制服务竞争力深度剖析
  • 眼油去黑眼圈哪个牌子效果好?2025全肤质好用抗皱眼油实测:lefufu多场景适配
  • 简易大变形二维数字图像相关方法设计
  • springboot项目注册到nacos使用dubbo互相调用例子
  • 2025年佛山市烧水油罐厂家最新推荐,花生油罐/卧式油罐/立式油罐/不锈钢食品油罐/化工油罐/食品油罐/加油站油罐//地埋式油罐/设备制造企业服务能力与产品特色分析
  • 2025年双吸离心泵供货商权威推荐榜单:耐腐蚀离心泵/离心泵/螺旋离心泵源头厂家精选
  • NOIP 集训 day6 杂题选讲(图论)
  • 【weblogic】协议反序列化漏洞 - 指南
  • 成都冷库制冷设备螺杆机组制造厂家哪家靠谱?认准成都科斯特!
  • 制冷设备哪家好?成都科斯特多维优势口碑推荐!
  • 2025年11月蒸汽发生器品牌推荐榜:综合实力与创新技术排行解析
  • 2025年药品检测柱后衍生系统十大推荐品牌,赛智柱后衍生技术
  • 四、使用Spring AI实现MCP Client调用MCP Server
  • 2025长沙西点培训学校TOP5权威推荐:长沙欧米奇专业吗?
  • 2025年11月蒸汽发生器品牌评价排行:行业数据与用户场景化分析
  • Android开发TextView字体大小自适应实现
  • 我的机器人制作流程
  • 解决Unity中使用C#自带类库没有代码提示的问题
  • 球缺与球台公式完整总结表
  • 基于MATLAB实现的PSO优化BP神经网络