Law of Demeter
The Law of Demeter is an implementation of loose coupling, specifically in object oriented programming languages.
It’s commonly explained as follow…
- Object A may call methods on object B
- Object B may call methods on object C
- Object A may not reach through object B and call methods on object C.
Rationale
If in the above example, object A did call through to object C then object A would have knowledge of both objects B and C. If A only knows about B and B knows about C, they are less coupled and easier to change/replace.
Examples
Example that breaks the law
|
|
Example that doesn’t break the law
|
|