Law of Demeter
This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (July 2009) |
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.
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:
- O itself
- M's parameters
- any objects created/instantiated within M
- O's direct component objects
- 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.
There is disagreement as to the sufficiency of this approach.[citation needed]
Advantages and disadvantages
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.
A disadvantage of the Law of Demeter is that it sometimes requires writing a large number of small "wrapper" methods to propagate method calls to the components. Furthermore, a class's interface can become bulky as it hosts methods for contained classes, resulting in a class without a cohesive interface[citation needed]. But this might also be a sign of bad OO design.
Basili et al.[1] published experimental results in 1996 suggesting that the Law of Demeter was a valid way to reduce the probability of software faults.
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.
See also
Sources
- ^ 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.
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.
{{cite journal}}
: Cite journal requires|journal=
(help) - 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. p. 150.
- Palermo, Jeffrey; Scheirman, Ben; Bogard, Jimmy (2009). ASP.NET MVC in Action. Manning Publications. p. 14.
External links
- Simple Law of Demeter in Ruby/Rails with demeter gem
- Brad Appleton: “Introducing Demeter and its Laws”
- Law of Demeter (Web site)
- “Object-Oriented Programming: An Objective Sense of Style” (OOPSLA '88 Proceedings) (PDF)
- Law of Demeter applied to Ruby on Rails
- The Paperboy, The Wallet,and The Law Of Demeter (PDF)
- Phil Haack: "The Law of Demeter is not a Dot Counting Exercise"
- Lieber: "Phil Holland's Law of Demeter"