Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Unfortunately, the LAW OF DEMETER has nothing to do with the Goddess of the harvest. It’s named for the Demeter Project at Northwestern University where it was developed. It’s also known as the PRINCIPLE OF LEAST KNOWLEDGE (but that’s not nearly as much fun, is it?)
Like the OCP, the Law of Demeter addresses the issue of coupling between classes. It states, essentially, that you should only talk to the objects you instantiate, not the ones they instantiate. Let’s look at an example:
According to the Law of Demeter, it would be legal to do any of these things in MyClass:
aFriend = new MyFriend();
aFriend.aFriendlyProperty = 3;
aFriend.aFriendsFriend = new aFriendsFriend();