~ Office Supplies ~~ Buy Posters ~~ A-Z Products ~~ Website Advertising


Law of Demeter - Wikipedia

<<Up     Contents

Law of Demeter

Redirected from LoD

The Law of Demeter (LoD) is a design guideline for developing software, particularly object-oriented programs. The guideline was invented at Northeastern University in the fall of 1987, and can be succinctly summarized as "Only talk to your immediate friends". The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents).

When applied to object-oriented programs, the Law of Demeter can be more precisely called the "Law of Demeter for Functions/Methods" (LoD-F). In this case, an object A can request a service (call a method) of an object instance B, but object A cannot "reach through" object B to access yet another object to request its services. Doing so would mean that object A implicitly requires greater knowledge of object B's internal structure. Instead, B's class should be modified if necessary so that object A can simply make the request directly of object B, and then let object B propogate the request to any relevant subcomponents. If the law is followed, only object B knows its internal structure.

More formally, the Law of Demeter for functions requires that any method M of an object O may only invoke the methods of the following kinds of objects:

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

In particular, an object should avoid invoking methods of a member object returned by another method.

The advantage of following the Law of Demeter is that the resulting software tends to be more maintainable and adaptable. Since objects are less dependent on the internal structure of other objects, object containers can be changes without reworking their callers.

A disadvantage of the Law of Demeter is that it requires writing a large number of small "wrapper" methods to propogate method calls to the components - these can increase initial development time, increase space overhead, and noticeably decrease performance. Automated tools exist to at least partially counteract these problems.

Basili et al published experimental results in 1996 suggesting that the Law of Demeter was a valid way to reduce the probability of software faults.

External Links

wikipedia.org dumped 2003-03-17 with terodump




 
 
30 gram picture MOONSTONE feldspar orthoclase gem Cab cabochon raw rough jewelry gemstone 154 carat
 30 gram picture MOONSTONE feldspar orthoclase Cab cabochon raw jewelry 154 carat 
 
100 carats Green PERIDOTS gem stones included rough tumbled polished jewelry gemstones lot 20 grams
 100 carats Green PERIDOTS included tumbled polished jewelry lot 20 grams 
 
25 carat PURPLE Russian CHAROITE gemstone Loose hand polished jewelry gem stone piece 5 gram PRETTY
 25 carat PURPLE Russian CHAROITE Loose hand polished jewelry piece 5 gram PRETTY 
 
14 carat blue gray sapphire MOONSTONE feldspar orthoclase gem Cab cabochon jewelry gemstone 15x13 mm
 14 carat blue gray sapphire MOONSTONE feldspar orthoclase Cab cabochon jewelry 15x13 mm 
 
17 gr Arizona pink red yellow PETRIFIED WOOD gem Cab lapidary rough tumble polished cabbing gemstone
 17 gr Arizona pink red yellow PETRIFIED WOOD Cab lapidary tumble polished cabbing