Law of Demeter
A design guideline that an object should only talk to its immediate collaborators, not to the objects those collaborators expose.
Also known as: Principle of Least Knowledge, Demeter's Law, Don't talk to strangers
Category: Software Development
Tags: software-development, programming, design, architecture, principles
Explanation
The Law of Demeter, also called the principle of least knowledge, was formulated at Northeastern University in 1987 during the Demeter project. It says a method should only invoke methods on itself, on its own parameters, on objects it creates, and on its direct components; it should not reach through one object to call methods on another. The colloquial version is to talk only to friends, not to strangers, and the code smell it targets is the train wreck of chained calls such as order.getCustomer().getAddress().getCity().getName(). Each link in such a chain is a dependency on a structure the caller has no business knowing, so a change anywhere along it breaks code far away. Obeying the law produces objects that expose behaviour rather than data, which tends to push logic to where the data lives. The trade-off is real: strict application can generate many delegating wrapper methods, and the law is usually relaxed for data transfer objects, fluent builders, and query interfaces where chaining is the intended design rather than an accidental leak.
Related Concepts
← Back to all concepts