LoD Principle
The Law of Demeter (LoD) or principle of least knowledge says a module should not know about the internals of the objects it manipulates.
An object should not expose its internal structure through accessors because to do so is to expose, rather than to hide, its internal structure.
The advantage of following the Law of Demeter is that the resulting software tends to be more maintainable and adaptable. Since objects are less dependent on the internal structure of other objects, object containers can be changed without reworking their callers.
LoD Rules
The Law of Demeter says that a method f of a class C should only call the methods of these:
C
An object created by f
An object held in an instance variable of C
An object passed as an argument to f
Examples
Example 1:
Example 2:
Instead of digging in Department details, it is better to ask, if a department is a cost centre.
Last updated