Law of Demeter
Encyclopedia
The Law of Demeter or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

. In its general form, the LoD is a specific case of loose coupling
Loose coupling
In computing and systems design a loosely coupled system is one where each of its components has, or makes use of, little or no knowledge of the definitions of other separate components. The notion was introduced into organizational studies by Karl Weick...

. 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
Aspect-oriented programming
In computing, aspect-oriented programming is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns...

 effort. The project was named in honor of Demeter
Demeter
In Greek mythology, Demeter is the goddess of the harvest, who presided over grains, the fertility of the earth, and the seasons . Her common surnames are Sito as the giver of food or corn/grain and Thesmophoros as a mark of the civilized existence of agricultural society...

, “distribution-mother” and the Greek goddess
Goddess
A goddess is a female deity. In some cultures goddesses are associated with Earth, motherhood, love, and the household. In other cultures, goddesses also rule over war, death, and destruction as well as healing....

 of agriculture
Agriculture
Agriculture is the cultivation of animals, plants, fungi and other life forms for food, fiber, and other products used to sustain life. Agriculture was the key implement in the rise of sedentary human civilization, whereby farming of domesticated species created food surpluses that nurtured the...

, to signify a bottom-up philosophy of programming which is also embodied in the law itself.

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. Ms parameters
  3. any objects created/instantiated within M
  4. Os 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
Maintainability
In engineering, maintainability is the ease with which a product can be maintained in order to:* isolate defects or their cause* correct defects or their cause* meet new requirements* make future maintenance easier, or* cope with a changed environment...

 and adaptable
Adaptive reuse
Adaptive reuse refers to the process of reusing an old site or building for a purpose other than which it was built or designed for. Along with brownfield reclamation, adaptive reuse is seen by many as a key factor in land conservation and the reduction of urban sprawl...

. Since objects are less dependent on the internal structure of other objects, object containers can be changed without reworking their callers.

Basili et al. 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 bug
Software bug
A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program's...

s. 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
Multilayered architecture
A multilayered software architecture is using different layers for allocating the responsibilities of an application.There is also an architectural pattern that is named Layers and has been described in different publications, including the book Pattern-Oriented Software ArchitectureA System of...

 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
Layer (object-oriented design)
In object-oriented design, a layer is a group of classes that have the same set of link-time module dependencies to other modules. In other words, a layer is a group of reusable components that are reusable in similar circumstances...

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

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
Aspect-oriented programming
In computing, aspect-oriented programming is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns...

 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.

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
Code smell
In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem.Often the deeper problem hinted by a code smell can be uncovered when the code is subjected to a short feedback cycle where it is refactored in small, controlled steps, and...

as opposed to a methodology for building loosely coupled systems.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK