Tags: lawofdemeter design glossary
A rule that attempts to minimize coupling between objects.
A method of an object may only call methods belonging to:
- Itself
- Parameters that were passed to the method
- Objects it creates
- Directly held component objects (attributes of that object)
Hence a method is not allowed to call methods on a global object (static methods of other objects) or methods belonging to objects returned by calls (for example a.getB().doSomething()).
This means a method is not allowed to know or find out about the internal structure of another object.
To use a real-world analogy: A dog has legs. If you want your dog to run do you talk (send a message to) your dog or to each leg? Further, should you be able to manipulate the dog's leg without it knowing about it? What if your dog wants to move its leg and it doesn't know how you left it? You can really confuse your dog. See http://c2.com/cgi/wiki?DontConfuseYourDog
See: http://c2.com/cgi/wiki?LawOfDemeter
Last published: Tuesday 18th October 2011
<<Previous Next>>