The Law of Demeter (LoD) or principle of least knowledge is a design guideline for developing software, particularly object-oriented programs
This law was proposed by Ian Holland in 1987 when he and his colleagues were
programming a system called Demeter using oriented object programming. During
the development of the system they realized that the code that fulfilled a
series of rules was less coupled.
The Demeter’s law is known as don’t talk to strangers because any method of
an object only can call to methods of:
- Each unit should have only limited knowledge about other units: only units “closely” related to the current unit.
- Each unit should only talk to its friends; don’t talk to strangers.
- Only talk to your immediate friends.
More formally, the Law of Demeter requires that a method m of an object O
may only invoke the methods of the following kinds of objects:
- O itself.
- m’s parameters.
- Any objects created/instantiated within m.
- O’s direct component objects.
- A global variable, accessible by O, in the scope of m.
In summary, all of the above rules can be stated as that you should avoid
invoking methods of a member object returned by another method. In the modern
object oriented languages the identifier used is
->. Therefore the
Demeter's law is violated when the code has more than one step between classes,
i.e the following code show an example in which Demeter's law is violated:
In this case, an object
a from the A class can request a method of an object
instanced of B class but the object A should not reach object B directly due
to that would mean the object A has greater knowledge of object B's internal
structure (tight coupling).
The following image illustrated who are friends between classes relations.
Now, I am going to show a real example implemented using TypeScript as
programming language. In the following UML diagram you can see as a
House is related with
The original code is from
and it’s coding using JAVA.
The following code can be run in the client/context whereas the first code broke
Demeter’s Law due to
Person requires a knowledge about the inner
implementation of the class
House. On the other hand, the second
implementation respects Demeter's Law and the code is less coupled.
The following steps shown as you must implemented the code to respect Demeter’s
Law and obtain a code less coupled. So, the first step is created the interfaces
which will be implemented in our concrete classes.
The next step will be the implementation of the concrete classes as you can seen
The most important in the code is that no method violated Demeter’s Law (there
is not more than two consecutive invocations of contained objects).
Another example in which the Demeter’s Law is broken is the following one:
In this case, the solution is implemented
isZipCode method in class
which you can see in the following code:
The main advantages of satisfying the Demeter’s Law are the following:
- Dependencies between classes and coupling are reduced.
- Reuse the classes with ease.
- The code is easier to test.
- The code is more maintainable and flexible to changes.
The GitHub branch of this post is https://github.com/Caballerog/blog/tree/master/demeter
Originally published at www.carloscaballero.io