\section{Clients, suppliers and dependencies}

Precondition: compile-time enforcing.

1. strong typing with explicit type declarations (C++) 

2. strong typing with type hierarchy and signatures of methods 
   described by Demeter type notation.
   The type of every statement or expression can be inferred at compile-time
   through type inferencing. (Lisp Flavors, Smalltalk)

Goal of Law of Demeter: organize and reduce dependencies
between classes.

Approximations: Each method can only send messages to a ``limited'' set
of objects.

Each method is ``dependent'' on a ``limited'' set of objects.
(organize dependencies)

Each method ``collaborates'' with a limited set of objects.
(organize collaborations)


We adopt the client/supplier terminology from \cite{meyer:book-88}.

\begin{definition}
The protocol of a class C is the set of messages that can be sent
to C and its instances.
\end{definition}

\begin{definition}
Method M is a client of class B and B is a supplier of method M,
if an object of class B is sent a message in method M.

or if an instance of a class in associated(B) is sent a message in method M.

?? not precise enough ??
note: more restrictive than Meyers. Passing and never sending a message
does not count for client.
\end{definition}

Next we define a special kind of clients and suppliers.

\begin{definition}
Class B is called a preferred supplier of method M (attached to class C) if
class B is a supplier of method M and one of the following conditions holds:
  B is an instance variable class of C or
    B is an argument class of M, including C or

do not need: C inherits from B or

        B is a class of objects created in M
	  B is a class of a global variable used in M.

%Include: have to send a message??
\end{definition}

\begin{definition}
Method M is called a preferred client of B if and only if
class B is a preferred supplier of method M.
\end{definition}

\begin{example}
In the following 4 examples,
class B is a preferred supplier of method M and M is a preferred client of B.

\bv
METHOD M (<self> Y <s> B ...)

// A = <s> B ...
METHOD M (<self> A ...)

METHOD M (<self> A ...)
  { ... f(s) ... }

// s is of type B, global to M
METHOD M (<self> A ...)
  { ... f(s) ... }
\end{verbatim}

\end{example}


\begin{fact}
Class B is a preferred supplier of method M attached to B.
Method M attached to class B is a preferred client of class B.
\end{fact}

Law of Demeter (type version):
In all classes C and in all methods M attached to C, use only the protocol of
the preferred suppliers of M.

\begin{fact}
Reformulation 1 of Law of Demeter (selective information hiding):
Only a preferred client method M of class B is allowed 
to use the protocol of class B.
\end{fact}

\begin{fact}
Reformulation 2 of Law of Demeter (selective information hiding):
The protocol of a class B can only be used in preferred clients of B.
\end{fact}

\begin{definition}
A method M is dependent on class C, if M uses a method attached to C.

What does uses mean? Need compile-time checking.
\end{definition}


\begin{fact}
Reformulation 3 of Law of Demeter (selective information hiding):
Only a preferred client method M of class B is allowed 
to be dependent on class B.
\end{fact}


This can be viewed as an equivalent formulation of the type version
of the Law which does not use associated. 

%\begin{definition}
%Class A is dependent on class B if A uses B's protocol.
%\end{definition}

\begin{fact}
Method M is dependent on B if and only if M is a client of B.

Depends on definition of dependent.
\end{fact}

%If A is a client of B then A is not necessarily dependent on B since
%no method of A might use A's protocol. (An object of type A might
%be passed as second argument and never sent a message.)

%The Law reduces the dependency between classes since only a preferred
%client class of a class B is allowed to use the protocol of B.
%Using the dependency concept, we can give the following equivalent
%formulation of the Law:

%\begin{fact}
%Only a preferred client class of B is allowed to be dependent
%on B.
%\end{fact}

%Yet another definition:
%\begin{fact}
%In any method M attached to class C use only the protocol
%of classes of which C is a preferred client.
%\end{fact}

Yet another reformulation:
\begin{fact}
In any method M use only the protocol
of preferred suppliers of M.
\end{fact}

The current definition of preferred client and supplier does not
deal with associated classes.
%Refined definition:
%
%\begin{definition}
%A is a preferred client of class B if B sends a message to an instance 
%The preferred clients A of class B are the classes which
%send a message
%to an instance of the classes associated with the following classes:
%  instance variable classes of B or
%  argument classes of methods of B or
%  classes from which B inherits or
%  classes which create
%
%A inherits from B or
%	B creates an object of class A or
%	  B has a method which uses a global variable which is of class A.
%	    Every class is a preferred client and a 
%	    preferred supplier of itself.
%\end{definition}


