TITLE: The Law of Demeter PROBLEM: COATES@UMUC.UMD.EDU (Ellster), 17 May 94 (The following is directly from "An Introduction to Object Oriented Programming" by Timothy Budd. ENJOY!) One such guideline has been proposed by Karl Lieberharr as part of his work on an object-oriented programming tool called Demeter; thus, the guideline is called the Law Demeter. There are two forms of the law, a strong form and a weak form, Both forms strive to reduce the degree of coupling between objects by limiting their interconnections. Law of Demeter. In any method M attached to a class B, only methods defined by the following classes may be used: 1. The instance variable classes of B. 2. The argument classes of method (including B); note that global objects or objects created inside the method M are considered arguments to M. We can rephrase the law in terms of instances (or objects) instead of methods; we arrive at the following: Law of Demeter (weak form). Inside a method, data can be accessed in and messages can be sent to only the following objects: 1. The arguments associated with the method being executed (including the self [or this] object). 2. Instance variables for the receiver of the method. 3 Global variables. 4. Temporary variables created inside the method. The strong form of the law restricts access to instance variables to only those variables defined in the class in which the method appears. Access to instance variables from superclasses must be mediated through the use of accessor functions. Law of Demeter (strong form). Inside a method it is only permitted to access or send messages to the following objects: 1. The arguments associated with the methods being executed (including the self [or this] object). 2. Instance variables defined in the class containing the method being executed. * 3. Global variables. 4. Temporary variables created inside the method. [* my comments: As opposed to the weak form which allows the access to the receiver's super and friend classes. In C++ only friends, or derived receivers (using their super's "protected or public" instances) may do this. -coates@umuc.umd.edu]