6.4 Collaborating objects
The objects that comprise a program collaborate with one another to fulfill the functions of the system that the program represents. Collaboration occurs when one object requests a service from another object in order to perform some task. To fulfill a particular collaboration, each object takes on a different role. The object that makes the request is called the client, and the object that receives the request (and provides the service in response) is called the server. The request is communicated to the server from the client by message passing. The client sends a message to the server which on receipt executes the operation with the same name and signature as the message. When the server has completed the execution of its operation it returns a message answer to the client. Both the message and message answer may convey data between the objects The signature of the operation is the specification of the type of the arguments and of the result of the operation, e.g. create (name: String, rate: Money, ensuited: Boolean): Room. For brevity, parameter names can be omitted, e.g. create (String, Money, Boolean): Room.
Figure 5 shows the collaboration between Room and Person objects (from the hotel example in Figure 4) where the Room object requests that the Person object linked to it provides the value associated with its name attribute. The Room object, as the client, sends the getName message to the Person object which, as the server, executes the getName operation on receipt of the message. When the Person object has completed the execution of the getName operation it returns the value associated with its name attribute to the client as the message answer.
The notation used in Figure 5 is the UML notation for sequence diagrams. Each box represents an object.
For a client object to collaborate successfully with a server object, two conditions must be met:
the client must know the identity of the server (to be able to send messages to it);
the server must understand the message, i.e. it must have an operation with the same name and signature as the message.
Consider a situation where an Account object provides a debit service by executing the operation debit with the signature debit(amount: Money), and a Bank object wants to debit an amount of 50 from that Account object. Identify the client, the server and the message in this collaboration.
The Account object is the server, the Bank object is the client, the message is debit(50); there is no message answer as the debit operation does not return a value. (However, it will return the flow of control on completion of the operation.)
The public operations that are made available to clients are what matters in the use of an object, rather than any private data it may happen to store internally to represent its state. The set of public operations that can be executed by an object is called its protocol or interface. We shall use the terms interface and protocol interchangeably to refer to the set of behaviours or operations an object offers. An Account object is likely to have the following operations in its protocol: getBalance, debit, and credit.
By convention, when the name of an operation is made up of more than one word, the second and subsequent words have initial upper-case letters, e.g. getBalance.