26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

352 Arrays Chapter 7<br />

Both a FloorDoor and the Eleva<strong>to</strong>rDoor inform a Person that they have<br />

opened or closed, so both objects send doorOpened and doorClosed messages. 2<br />

Finally, the Eleva<strong>to</strong>rModel creates a Person, but the Eleva<strong>to</strong>rModel cannot send<br />

a personCreated message, because that Person does not yet exist. We discover, in<br />

Section 8.6, how <strong>to</strong> use a special method that creates, or instantiates, new objects. This<br />

method is referred <strong>to</strong> as an construc<strong>to</strong>r—the Eleva<strong>to</strong>rModel will create a Person by<br />

calling that Person’s construc<strong>to</strong>r (which is similar <strong>to</strong> sending the personCreated<br />

message).<br />

Lastly, the Eleva<strong>to</strong>rDoor must send openDoor and closeDoor messages <strong>to</strong> a<br />

FloorDoor <strong>to</strong> guarantee that these doors open and close <strong>to</strong>gether.<br />

Collaboration Diagrams<br />

Now let us consider the objects that must interact so that people in our simulation can enter<br />

and exit the eleva<strong>to</strong>r when it arrives on a floor. The UML provides the collaboration diagram<br />

<strong>to</strong> model such interactions. Collaboration diagrams are a type of interaction diagram;<br />

they model the behavioral aspects of the system by providing information about how objects<br />

interact. Collaboration diagrams emphasize which objects participate in the interactions.<br />

The other type of interaction diagram is the sequence diagram, which we present in<br />

Chapter 15. Figure 7.24 shows a collaboration diagram that models a person who is pressing<br />

a floor but<strong>to</strong>n. An object in a collaboration diagram is represented as a rectangle that<br />

encloses the object’s name. We write object names in the collaboration diagram by using<br />

the convention we introduced in the object diagram of Fig. 3.24—objects are written in the<br />

form objectName : ClassName. In this example, we disregard the object name, because<br />

we care only about the object type. Collaborating objects are connected with solid<br />

lines, and messages are passed between objects along these lines in the direction shown by<br />

arrows. The name of the message, which appears next <strong>to</strong> the arrow, is the name of a method<br />

belonging <strong>to</strong> the receiving object—think of the name as a “service” that the receiving object<br />

(a “server”) provides for its sending objects (its “clients”).<br />

pressBut<strong>to</strong>n( )<br />

: Person : FloorBut<strong>to</strong>n<br />

Fig. Fig. 7.24 7.24 Collaboration diagram of a person pressing a floor but<strong>to</strong>n<br />

The arrow in Fig. 7.24 represents a message in the UML and a method—or synchronous<br />

call—in <strong>Java</strong>. This arrow indicates that the flow of control is from the sending object<br />

<strong>to</strong> the receiving object—the sending object may not send another message until the<br />

receiving object processes the message and returns control <strong>to</strong> the sending object. For<br />

example, in Fig. 7.24, a Person calls method pressBut<strong>to</strong>n of a FloorBut<strong>to</strong>n and<br />

2. Note that most of the messages perform some specific action on the receiving object; for example,<br />

the Eleva<strong>to</strong>r resets the Eleva<strong>to</strong>rBut<strong>to</strong>n. <strong>How</strong>ever, other messages inform receiving objects<br />

of events that have already happened; for example, the FloorDoor informs the Person<br />

that the FloorDoor has opened. In Section 10.22, we elaborate on the <strong>to</strong>pic of events—for now,<br />

however, we proceed as if the two types of messages are indistinguishable.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!