Shahzad Bhatti

July 9, 2003

Law of Demeter (LoD)

Filed under: Computing — admin @ 3:34 pm

Law of Demeter (LoD)
The Law of Demeter was originally formulated as a style rule for
designing object-oriented systems. “Only talk to your immediate friends”
is the motto. The style rule was discovered at Northeastern University
in the fall of 1987 by Ian Holland.
A more general formulation of the Law of Demeter is: Each unit should
have only limited knowledge about other units: only units “closely”
related to the current unit. Or: Each unit should only talk to its
friends; Don’t talk to strangers.

In this general form, the LoD is a more specific case of the Low
Coupling Principle well-known in software engineering. The Low Coupling
Principle is very general and we tried to make it more specific. The
benefit of the specific Law of Demeter shown below is that it makes the
notion of unnecessary coupling very explicit.

The main motivation for the Law of Demeter is to control information
overload; we can only keep a limited set of items in short-term memory
and it is easier to keep them in memory if they are closely related. The
definition of “closely related” is intentionally left vague so that it
can be adapted to particular circumstances.

In the application of LoD to object-oriented design and programming we
unit = method f
closely related = methods of class of this/self of f and other argument
classes of f and methods of immediate part classes (both computed and
stored) of class of f (classes that are return types of methods of class
of this/self (= computed) and the classes of data members (= stored))
and methods of classes of objects that are created in f.

Powered by WordPress