Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Law of Demeter
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==In object-oriented programming== An object <code>a</code> can request a service (call a method) of an object instance <code>b</code>, but object <code>a</code> should not "reach through" object <code>b</code> to access yet another object, <code>c</code>, to request its services. Doing so would mean that object <code>a</code> implicitly requires greater knowledge of object <code>b</code>'s internal structure. Instead, <code>b</code>'s interface should be modified if necessary so it can directly serve object <code>a</code>'s request, propagating it to any relevant subcomponents. Alternatively, <code>a</code> might have a direct reference to object <code>c</code> and make the request directly to that. If the law is followed, only object <code>b</code> knows its own internal structure. More formally, the Law of Demeter for functions requires that a method <code>m</code> of an object <code>a</code> may only invoke the methods of the following kinds of objects:<ref>{{cite web | title = The Paperboy, The Wallet, and The Law Of Demeter | url = http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/demeter.pdf | last = Bock | first = David | page = 5 | publisher = College of Computer and Information Science, Northeastern University | access-date = 2012-07-05 }}</ref> * <code>a</code> itself; * <code>m</code>'s parameters; * any objects instantiated within <code>m</code>; * <code>a</code>'s attributes; * global variables accessible by <code>a</code> in the scope of <code>m</code>. In particular, an object should avoid invoking methods of an 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".<ref>{{cite book |page=81 |title=Practical Object-Ortiented Design: An Agile Primer Using Ruby |first=Sandi |last=Metz |edition=Second |publisher=Addison-Wesley |year=2019 |isbn=978-0134456478 |lccn=2018939833 }}</ref> That is, the code <code>a.m().n()</code> breaks the law where <code>a.m()</code> does not. As an [[analogy]], when one wants a dog to walk, one does not command the dog's legs to walk directly; instead, one commands the dog which then commands its own legs.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)