====== Law of Demeter (LoD) ====== ===== Variants and Alternative Names ===== * Principle of Least Knowledge * Don't Talk to Strangers ===== Context ===== /* fill in contexts here: */ * [[contexts:Object-Oriented Design]] * [[contexts:Implementation]] ===== Principle Statement ===== "A method of an object should invoke only the methods of the following kinds of objects: - itself - its parameters - any objects it creates/instantiates - its direct component objects"((David Bock: //[[http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/demeter.pdf|The Paperboy, The Wallet, and The Law Of Demeter]]//)) ===== Description ===== ===== Rationale ===== ===== Strategies ===== ===== Caveats ===== See section [[#contrary principles]]. ===== Origin ===== ===== Evidence ===== /* Comment out what is not applicable and explain the rest: */ /* * [[wiki:Proposed]] * [[wiki:Examined]] * [[wiki:Accepted]] * [[wiki:Questioned]] */ ===== 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 ==== * [[Model Principle]] (MP): Adhering to LoD may result in additional methods in aggregating classes. MP tells how they should look like. ==== Principle Collections ==== ===== Examples ===== ===== Description Status ===== /* Choose one of the following and comment out the rest: */ [[wiki:Stub]] /*[[wiki:Incomplete]]*/ /*[[wiki:Complete]]*/ ===== Further Reading ===== * [[wp>Law of Demeter]] * [[wiki>LawOfDemeter]] * Andrew Hunt and David Thomas: //[[http://www.ccs.neu.edu/research/demeter/related-work/pragmatic-programmer/jan_03_enbug.pdf|The Art of Enbugging]]// * Andrew Hunt and David Thomas: //The Pragmatic Programmer// * Craig Larman: //Applying UML and Patterns// * David Bock: //[[http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/demeter.pdf|The Paperboy, The Wallet, and The Law Of Demeter]]// * Karl J. Lieberherr and Ian M. Holland: //Assuring good style for object-oriented programs//, IEEE Software, p. 38–48 * Phil Haack: //[[http://haacked.com/archive/2009/07/14/law-of-demeter-dot-counting.aspx|The Law of Demeter Is Not A Dot Counting Exercise]]// * [[http://www.ccs.neu.edu/home/lieber/LoD.html|Law of Demeter]] ===== Discussion ===== Discuss this wiki article and the principle on the corresponding [[talk:principles:Law of Demeter|talk page]].