Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify code so that it adheres to the Law of Demeter

public class BigPerformance  
{  
    public decimal Value { get; set; }
}  

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

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

If I call:

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

I assume this this breaks the Law of Demeter / Principle of Least Knowledge?
If so, how do I remedy this if I have a large number of inner class properties?

like image 241
guazz Avatar asked Apr 19 '10 11:04

guazz


1 Answers

One of my favourite quotes from 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

like image 112
Rob Fonseca-Ensor Avatar answered Nov 07 '22 18:11

Rob Fonseca-Ensor