User Tools

Site Tools


principles:law_of_demeter

This is an old revision of the document!


Law of Demeter (LoD)

Variants and Alternative Names

  • Principle of Least Knowledge
  • Don't Talk to Strangers

Context

Principle Statement

“A method of an object should invoke only the methods of the following kinds of objects:

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

Description

Rationale

Strategies

Caveats

See section contrary principles.

Origin

Evidence

Relations to Other Principles

Generalizations

  • Low Coupling (LC): The goal of LoD is to reduce coupling by reducing the knowledge of a class about other classes.
  • Tell, Don't Ask/Information Expert (TdA/IE): LoD is more specific than TdA/IE because TdA/IE can be applied in a wider context (e.g. for responsibility assignment). Applying TdA leads to solutions which are good according to LoD. Note that the reverse is not true: Accoring to LoD you may get and set values from an object passed as a parameter to a method.

Specializations

Contrary Principles

  • More Is More Complex (MIMC): Adhering to the Law of Demeter often results in additional methods.
  • Low Coupling (LC): Adhering to the Law of Demeter may create tramp couplings which are bad.
  • High Cohesion (HC): Adhering to the Law of Demeter often results in additional methods that mirror methods of aggregated objects. As these objects have other responsibilities, the additional methods have fewer commonalities with the “real” methods of the class, which results in a lower cohesion.

Complementary Principles

Principle Collections

Examples

Description Status

Further Reading

principles/law_of_demeter.1602512428.txt.gz · Last modified: 2020-10-12 16:20 by 159.69.186.191