The Law of Demeter, also called the Principle of Least Knowledge, limits a class’s dependencies by hiding implementation details and exposing only what other code needs. Private members make hidden implementation inaccessible, so internal changes need not force changes elsewhere. section 5.1
A member function may call another object’s function when the object is:
- Aggregated by the calling class. section 5.6
- Passed as an argument to the calling function. section 5.6
- Instantiated by the calling function. section 5.6
- Not merely returned by another object’s member function; navigating that returned object creates a problematic dependency. section 5.6
Automobile maintenance
In the DemeterAuto example, Engine replaces its own Sparkplug, and DemeterAuto can pass a Sparkplug to service_sparkplug or create one locally. Calling get_sparkplug and then replacing the returned plug violates the law; engine.replace_sparkplug preserves the boundary. section 5.6

