Java Reference
In-Depth Information
Messages and Events
Message sending is a key component of object-oriented design because it describes how objects work
together. For an object to do something, it must be sent a message. The message must have two parts:
(1) the name of the object to which the message is being sent, and (2) the name of the operation that will
be performed by the object. As you have learned, an operation is an activity that reads or manipulates the
data of an object. An operation also can send additional messages while it performs its task.
Messages are sent through an interface to the object. Just as a user interface allows a program to
accept data and instructions from the user, the interface is the way that an object receives messages.
As an example, suppose each time an assembly-line worker turns on a machine (an object), the
machine tracks how many times it has been turned on by incrementing a counter. To turn on the
machine, the worker presses a button on a panel to send a message to the machine. The button on the
panel is the interface to the on() method of the machine, and pressing the button is the event that
sends a message to execute the on() method. When the on() method executes, part of its operation is
to increment the numberTimesOn counter, which is an attribute of the machine. Suppose that the
shop also uses an automated system to operate its machines, so that the machines can be turned on
remotely using a computer. The interface that the computer uses to turn on the machine is different
from the one the worker uses. The on() method that executes on the machine, however, remains the
same and the on() method still increments the numberTimesOn counter attribute when it executes.
In OOD terminology, the operation, increment counter, is a service and the message, turn machine
on, is called a request for service . Remember that in OOP terminology, the service is called a method
and the message is what is sent when an event, such as a user pressing the on button, occurs. Sequence
diagrams are used to represent the relationships among events and objects. In a sequence diagram, mes-
sages or events are shown as lines with arrows, classes are shown across the top in rectangles, and class
names are underlined and prefaced by a colon (Figure A-21). As you read the sequence diagram, time
progresses from top to bottom, and the time the object is active is shown by vertical rectangles.
Figure A-21 illustrates a sequence diagram for a Foreman assigning a Worker to a Machine. The
Foreman object in the first column interacts with other objects through the Foreman Interface. The
Foreman sends a message through the Foreman Interface to find a Worker based on the worker's
name. Next, the Foreman finds a Machine to assign the Worker based on the worker's skill. Finally, the
assignment is made.
As shown in Figure A-21, nothing happens in a system unless a message is sent when an event
occurs. At the conclusion of an operation, the system will do nothing until another event occurs. This
relationship of events causing operations is a key feature of OOP, and programs that are constructed in
this way are said to be event driven .
The UML is a powerful tool because it can be used to describe any item, process, or concept in the
real or imagined world. Its usefulness goes well beyond the programming world. People working in
different disciplines or working in different industries can communicate concepts using the UML in a
standard and well-understood manner. Another feature of the UML is that many types of diagrams
provide different views of the same system, or object, in addition to the ones shown here. Different
views of the same system are useful depending on a person's or object's role in the system.
Search WWH ::




Custom Search