The Law Of Demeter

Introduction

The Law of Demeter is also know as the Principle of Least Knowledge. It is a software design guideline specially related to loose coupling. It was proposed by Ian Holland. This law mentions the below basic rules,
  • Don't talk to Strangers:
  • Don't send messages to objects returned from other message sends.
  • Only talk to your immediate friends.
Let "M" is a method and "O" is an object so, M of object "O" should invoke only the methods of following kinds of objects,
  1. Itself
  2. Its Parameters
  3. It direct component object
  4. Any object it creates or instantiates

The Law of Demeter Violation

Below code example is a violation of the Law of Demeter,
  1. public class Test1  
  2. {  
  3.     public Test4 test4 { getset; }  
  4.     public Test2 test2 { getset; }  
  5.   
  6.     public Test1()  
  7.     {  
  8.         test4 = new Test4();  
  9.         test2 = new Test2();  
  10.     }  
  11.   
  12.     public void Method1()  
  13.     {  
  14.         test2.test3.Test3Method();  
  15.         test4.test5.Test5Method();  
  16.     }  
  17. }  
  18. public class Test2  
  19. {  
  20.     public Test3 test3 { getset; }  
  21.   
  22.     public Test2()  
  23.     {  
  24.         test3 = new Test3();  
  25.     }  
  26. }  
  27.   
  28. public class Test3  
  29. {  
  30.     public void Test3Method() { }  
  31. }  
  32.   
  33. public class Test4  
  34. {  
  35.     public Test5 test5 { getset; }  
  36.     public Test4()  
  37.     {  
  38.         test5 = new Test5();  
  39.     }  
  40. }  
  41.   
  42. public class Test5  
  43. {  
  44.     public void Test5Method() { }  
  45. }  
In the above code, Method1() of class Test1 is calling the methods of classes Test3 and Test5 by using classes Test2 and Test5 respectively. Class Test1 is not aware of classes Test3 and Test5. Both classes are stranger for the class Test1. So, this code is violating the rule of Demeter Principle. 
 
Basically, the Law of Demeter is focused on Coupling. If class Test1 needs to know about class Test3 or any other class then it is a violation. It clearly defines the need of two things in the code - first, Balance the need of Decoupling and second, keep the responsibility clearly separated.
 
The main purpose of this law is to restrict outside objects being able to access the internals of another object. Accessing internals encounters problems,
  • It might give information about the internal structure of the object.
  • Outside object can modify the internal structure of object

How can we obey the Law of Demeter if Multiple Levels of Class Dependencies are there?

The below rules should be followed to obey the Law of Demeter,
  •  Apply Aggregation
  • Apply Composition
  • Use good encapsulation at each level.

Conclusion

The Law of Demeter states that "you should only speak to objects that you know about directly". Do not do method chaining communication with other objects. By doing this it increases coupling.

Up Next
    Ebook Download
    View all
    Learn
    View all