Severin Perez

Reference: Law of Demeter

October 08, 2018

The Law of Demeter (LoD), also known as the principle of least knowledge, is an object-oriented design pattern that aims to decrease unnecessary coupling between objects. According to the LoD, an object should be able to request services from a second object, but should not “reach through” that object to access a third object. In adhering to the LoD, a method (m) on a given object (O) must only invoke methods that are: also defined on O; parameters of m; on objects instantiated by O; or, on direct component objects of O. This is a form of information hiding, in which a given object has only limited knowledge of the structure / properties of partner objects. If a method needs to access some behavior or property defined on an object to which its parent has no direct connection, then it should either do so via a wrapper on some directly connected object. The benefit of the LoD is a decrease in dependencies, thus allowing for more flexible and maintainable code.


You might enjoy...

© Severin Perez, 2021