加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何修改代码以使其符合Demeter法则

发布时间:2020-12-15 18:18:22 所属栏目:百科 来源:网络整理
导读:public class BigPerformance { public decimal Value { get; set; }} public class Performance { public BigPerformance BigPerf { get; set; }} public class Category { public Performance Perf { get; set; } } 如果我打电话: Category cat = new Cat
public class BigPerformance  
{  
    public decimal Value { get; set; }
}  

public class Performance  
{  
    public BigPerformance BigPerf { get; set; }
}  

public class Category    
{  
    public Performance Perf { get; set; }     
}

如果我打电话:

Category cat = new Category();  
cat.Perf.BigPerf.Value = 1.0;

我认为这打破了Law of Demeter / Principle of Least Knowledge?
如果是这样,如果我有大量的内部类属性,我该如何解决这个问题呢?

解决方法

Martin Fowler最喜欢的一句话:

I’d prefer it to be called the
Occasionally Useful Suggestion of
Demeter

http://haacked.com/archive/2009/07/14/law-of-demeter-dot-counting.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读