When we focus on one method of a class, we might be overwhelmed by the number of distinct classes whose services are used in that method. We call this the Method Information Overload Problem.
The Law of Demeter has been devised to control both the Structure Duplication Problem and the Method Information Overload Problem. The General Formulation of the Law of Demeter addresses directly the overload problem: "Each unit should have only limited knowledge about other units: only units "closely" related to the current unit." The idea is that the number of closely related units should be relatively small compared to the total number of units in the system. In the application to OO, the units are methods.
The following pictures use a very simple example to show violations of the Law of Demeter graphically.
Law of Demeter violation (class diagram)
Law of Demeter violation source
code (Java)
Follow Law of Demeter (class diagram)
Two small methods foo2() and bar2() are introduced.
Suppose we wish to implement a Person counting method for class BusRoute. We parameterize the counting operation by a traversal graph object.
class BusRoute{
public int countPerson(TraversalGraph tg){ // traversal/visitor
weaving
tg.traverse(this, new Visitor(){
int r;
public void before(Person
host){ r++; }
public void start()
{ r = 0;}
}
}
}
If we wish to count the number of persons riding in buses we call: aBusroute.countPerson(new TraversalGraph("via Bus to Person")). If we wish to count the number of persons waiting at bus stops we call: aBusroute.countPerson(new TraversalGraph("via BusStop to Person")). Note how much navigation code we have eliminated if we use the code in the following two class diagrams:BusRoute BusRoute with villages .
Class TraversalGraph can be implemented from scratch or down-loaded from the Adaptive Programming library page available at the Demeter home page.
When a violation of the Law of Demeter is detected we would like to
know which solution to apply. The small methods solution is convenient
when the class diagram is fairly stable or when a fast implementation is
needed. The traversal graph solution is convenient if the class diagram
is changing frequently and navigation speed is not very important. Note
that the traversal graph is interpreted at runtime.