Law of Demeter


Law of Demeter

The Law of Demeter (LoD) or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling. The guideline was invented at Northeastern University towards the end of 1987, and can be succinctly summarized in one of the following ways:

  • 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.

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).

It is so named for its origin in the Demeter Project, an adaptive programming and aspect-oriented programming effort. The project was named in honor of Demeter, “distribution-mother” and the Greek goddess of agriculture, to signify a bottom-up philosophy of programming which is also embodied in the law itself.

Contents

In object-oriented programming

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, C, to request its services. Doing so would mean that object A implicitly requires greater knowledge of object B's internal structure. Instead, B's interface should be modified if necessary so it can directly serve object A's request, propagating it to any relevant subcomponents. Alternatively, A might have a direct reference to object C and make the request directly to that. If the law is followed, only object B knows its own internal structure.

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

  1. O itself
  2. M's parameters
  3. any objects created/instantiated within M
  4. O's direct component objects
  5. a global variable, accessible by O, in the scope of M

In particular, an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code a.b().Method() breaks the law where a.Method() does not. As a simple example, when one wants to walk a dog, it would be folly to command the dog's legs to walk directly; instead one commands the dog and lets it take care of its own legs.

Advantages

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 changed without reworking their callers.

Basili et al.[1] published experimental results in 1996 suggesting that a lower Response For a Class (the number of methods potentially invoked in response to calling a method of that class) can reduce the probability of software bugs. Following the Law of Demeter can result in a lower RFC. However, the results also suggest that an increase in Weighted Methods per Class (the number of methods defined in each class) can increase the probability of software bugs. Following the Law of Demeter can also result in a higher WMC; see Disadvantages.

A Multilayered architecture can be considered to be a systematic mechanism for implementing the Law of Demeter in a software system. In a layered architecture, code within each layer can only make calls to code within the layer and code within the next layer down. "Layer skipping" would violate the layered architecture.

Disadvantages

If you conform to LoD, while it may quite increase the maintainability and "adaptiveness" of your software system, you also end up having to write lots of little wrapper methods to propagate methods calls to its components (which can add noticeable time and space overhead)[2].

At the method level, the LoD leads to narrow interfaces because each method needs to know about a small set of methods of closely related objects. On the other hand, at the class level, the LoD leads to wide interfaces because the LoD requires that we introduce many auxiliary methods instead of digging directly into the object structures. Wide interfaces for classes are a problem and the solution is an aspect-oriented approach where the behavior of the method is specified as an aspect at a high level of abstraction. We still have wide interfaces but we manage them through a language that specifies implementations[3].

Because the LoD exemplifies a specific type of coupling, and does not specify a method of addressing this type of coupling, it is more suited as a metric for Code smell as opposed to a methodology for building loosely coupled systems.

See also

Sources

  1. ^ Basili, Victor; Briand, L.; Melo, W.L. (October 1996). "A Validation of Object-Oriented Design Metrics as Quality Indicators". IEEE Transactions on Software Engineering 22 (10): 751–761. 
  2. ^ Brad Appleton. "Introducing Demeter and its Laws". http://www.cmcrossroads.com/bradapp/docs/demeter-intro.html. Retrieved 18 August 2011. 
  3. ^ Karl Lieberherr. "Law of Demeter (LoD)". http://www.cmcrossroads.com/bradapp/docs/demeter-intro.html. Retrieved 18 August 2011. 

Further reading

  • Lieberherr, Karl; Holland, I. (September 1989). "Assuring good style for object-oriented programs". IEEE Software: 38–48. 
  • Lieberherr, Karl J. (1995). Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns. Boston: PWS Publishing Company, International Thomson Publishing. 
  • Hunt, Andrew; Thomas, David (2002). The Pragmatic Programmer: From Journeyman to Master. Addison-Wesley. pp. 140–141. 
  • Larman, Craig (2005). Applying UML and Patterns (3rd ed.). Prentice Hall. pp. 430–432.  (from this book, "Law of Demeter" is also known as "Don't talk to strangers")
  • McConnell, Steve (2004). Code Complete (2nd ed.). Microsoft Press. pp. 150. 
  • Palermo, Jeffrey; Scheirman, Ben; Bogard, Jimmy (2009). ASP.NET MVC in Action. Manning Publications. pp. 14. 

External links


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Law of Demeter — Das Gesetz von Demeter (englisch: Law of Demeter, kurz: LoD) ist eine Entwurfs Richtlinie in der objektorientierten Softwareentwicklung. Sie besagt im Wesentlichen, dass Objekte nur mit Objekten in ihrer unmittelbaren Umgebung kommunizieren… …   Deutsch Wikipedia

  • Demeter (disambiguation) — Demeter is usually the Greek grain goddess. It may also refer to: Astronomy: Demeter, name formerly used for Lysithea (moon), satellite of Jupiter, from 1955 to 1975 1108 Demeter, asteroid Literature: Demeter (c. 300 BC), an elegy by… …   Wikipedia

  • Demeter's Manual of Parliamentary Law and Procedure — Demeter s Manual Demeter s Manual of Parliamentary Law and Procedure, by George Demeter, is a parliamentary authority manual. It is included in the bank of study materials used in preparing for the Certified Parliamentarian (CP) designation… …   Wikipedia

  • Demeter — For other meanings, see Demeter (disambiguation). Demeter Goddess of the Earth, Agriculture, Harvest, and Forests Abode Mount Olympus …   Wikipedia

  • Law of abode — The Law of Abode is a law in Greek mythology that is older than Zeus and all are powerless to revoke. The Law of Abode states that if any food or drink should pass through your lips while in the realm of Hades, then you may not go back above to… …   Wikipedia

  • Gesetz von Demeter — Das Gesetz von Demeter (englisch: Law of Demeter, kurz: LoD) ist eine Entwurfs Richtlinie in der objektorientierten Softwareentwicklung. Sie besagt im Wesentlichen, dass Objekte nur mit Objekten in ihrer unmittelbaren Umgebung kommunizieren… …   Deutsch Wikipedia

  • Loi De Déméter — La loi de Déméter (son sigle anglais: LoD), ou Principe de connaissance minimale est une règle de conception pour développer un logiciel, particulièrement du logiciel orienté objet. Cette règle a été inventée à la Northeastern University à Boston …   Wikipédia en Français

  • Loi de Demeter — Loi de Déméter La loi de Déméter (son sigle anglais: LoD), ou Principe de connaissance minimale est une règle de conception pour développer un logiciel, particulièrement du logiciel orienté objet. Cette règle a été inventée à la Northeastern… …   Wikipédia en Français

  • Loi de déméter — La loi de Déméter (son sigle anglais: LoD), ou Principe de connaissance minimale est une règle de conception pour développer un logiciel, particulièrement du logiciel orienté objet. Cette règle a été inventée à la Northeastern University à Boston …   Wikipédia en Français

  • Loi de Déméter — La loi de Déméter (son sigle anglais: LoD), ou Principe de connaissance minimale est une règle de conception pour développer un logiciel, particulièrement du logiciel orienté objet. Cette règle a été inventée à la Northeastern University à Boston …   Wikipédia en Français


Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.