Law of Demeter

Recently I have read some articles that have mentioned the Law of Demeter, and not having heard of it, I went looking for it. Blatantly stolen from the article, the Law of Demeter is as follows:

A method “M” of an object “O” should invoke only the
the methods of the following kinds of objects:

  1. itself
  2. its parameters
  3. any objects it creates/instantiates
  4. its direct component objects

 Why is this a good thing? In simple terms, following the Law of Demeter, you enforce encapsulation of a method’s implementation and dependencies on other objects. Should that method have to change at a later time, it minimizes the impact on callers of the method, localizing the change to the method in question and its subordinates.

For more information, and perhaps a more succinct explanation, see the linked article above.

Leave a Reply