• Door. The elevator has a door that can be commanded to open or close.
• Up/Down button. Each floor has a pair of these buttons, except for the top and bottom floors. The button state can be pressed or not pressed, and it can either be illuminated or not illuminated.
• Floor button. The elevator has a set of buttons, one for each floor. Like the Up/Down buttons, these can be pressed or not pressed, illuminated or not illuminated.
• Controller. This class ties all the others together by controlling the elevator and door in response to button presses by passengers.
Right-click the elevator package in the Package Explorer view and select New UML Diagram→UML Class Diagram. Change the parent folder to elevator/models/
and if you choose, delete “elevator” from the file name. The Class Diagram editor toolbar is shown in Figure 8.8. The first two icons are Selection mode and Zoom mode, as in the Use Case editor. The remaining icons are:
• Create a package
• Create a class: drop down menu
• Create an interface
• Create an enumeration
• Association: drop down menu
• Dependency: drop down menu
• Generalization
• Realization: drop down menu
• Create an Element Import: drop down menu
• Create an interface provider
• Create a require interface
• Create an interface connection
• Create a note: drop down menu
• Create an indication
Figure 8.8: Class Diagram editor tool bar.
Click Create a class and then click somewhere in the top center of the editor. This brings up the New Java Class dialog. Name the class “Controller” and leave everything else as defaults. Click Finish. A class symbol shows up on the diagram. In the Package Explorer view a Java file, Controller.java
, shows up under the elevator package.
Repeat this process for the following classes:
• Elevator
• Door
• UpDownButton
• FloorButton
The result should look something like Figure 8.9.
Figure 8.9: Class diagram.
Take a quick look at Controller.java
. Not much there, just a stub for the Controller class. But as we add items to the class diagram, the template code will be expanded to remain in sync with the diagram.
Now that we have the classes, we need to figure out how they interact. Let’s start by creating a pair of directed associations between the Controller and the two button classes. From the Association drop-down menu, select Directed Association. Then click on the Controller class. The cursor changes to a plug, implying that we’re “plugging” the Controller class into something else. Now click on the UpDownButton class.
A pair of methods show up in the Controller box — getUpDownButton()
and setUpDownButton()
. This seems reasonable. We want to get the state of the buttons and in turn set the illumination state. Do the same thing with the FloorButton class. Our class diagram now resembles Figure 8.10.
Figure 8.10: Class diagram with associations.
It might be equally useful to create directed associations with the Elevator and Door. Instead, I encourage you to investigate the other associations and connection mechanisms, to see how EclipseUML treats them. And of course, unless you’re a Java programmer with some knowledge of UML, they may not make much sense just yet.
8.1.5 Sequence Diagram
To round out our brief tour of EclipseUML, we’ll create a sequence diagram to describe the system’s response to the passenger pushing an UpDownButton. The sequence is as follows:
1. The passenger presses an UpDownButton.
2. The UpDownButton sends an update to the Controller.
3. The Controller illuminates the button and moves the Elevator to the specified floor.
4. The Elevator signals that it has reached the floor.
5. The Controller stops the Elevator and turns off the UpDownButton illumination.
6. The Controller opens the Door.
7. After a suitable delay, the Controller closes the Door.
Right-click the elevator package and select New UML Diagram→UML Sequence Diagram. Name it “UpDownButtonSequence” and store it in the models/ directory. The Sequence Diagram editor tool bar (Figure 8.11) has the following buttons:
• Selection mode
• Zoom mode
• Add new property (similar to add an object)
• Create an actor
• Add a message
• Add a self message
• Add a frame
• Add an Interaction Use
• Create a Component
• Create a Class
• Create an Interface
• Create an entity
• Create a boundary
• Create a controller
• Create a note
• Create an indication
• Create a diagram link
• Create a text label
Figure 8.11: Sequence Diagram editor tool bar.
For the purpose of this illustration we’ll only be using a couple of these buttons, Create an actor and Add a message. Our objective here is to express the interactions among the classes in our system for the Press up/down button use case. Begin by creating an actor in the upper left-hand corner of the diagram and name it “passenger.”
Now the cool thing is that we can drag the classes from the elevator package in the Package Explorer view directly into the sequence diagram. Drag the classes so they show up in the following order to the right of the passenger:
1. UpDownButton
2. Controller
3. Elevator
4. Door
In order to get the Door class to show up you may have to maximize the editor window and drag the frame out to about 700 pixels. Then restore the editor to its windowed view. Your diagram should now look something like Figure 8.12. Each of the boxes is an instance of its respective class.
Figure 8.12: Sequence diagram with classes.
Note that the Outline view shows a thumbnail of the entire diagram with the portion visible in the Editor shaded. You can drag the shaded portion to scroll around in the Editor.
In the context of a sequence diagram, classes communicate by sending messages to each other. Click the Add a message button, move the cursor to just below the label of the actor (his head turns blue) and click. Now move the cursor to the right until the UpDownButton box turns blue, and click. The Message dialog of Figure 8.13 comes up. Label it “Press.”
Figure 8.13: Message dialog.
Here you can also specify the Operation (method) that carries out the message. In this case, of course, there is none — the passenger is pressing the button. The commercial version of EclipseUML will add method templates to the class code. Leave the rest of the fields at their default values and click OK.
In like fashion, add a message labeled “update” from the UpDownButton to the Controller. You might want to grab the vertical box below the controller and drag it down a little to make it clear that the update follows the press. Next add a message from the Controller back to the UpDownButton and label it “illuminate.”