
lafore_robert_objectoriented_programming_in_c
.pdf

Chapter 16
816
Display
Add Tenant
screen
Get name and
apartment number
|
[Apartment occupied] |
[else] |
|
Place data |
Display |
on |
error |
Tenant List |
message |
FIGURE 16.8
UML activity diagram.
Activity diagrams can also be used to represent complicated algorithms in program code, just as flowcharts are. They have some capabilities we won’t pursue here, such as representing several concurrent activities.
From Use Cases to Classes
The construction phase of our project begins when we begin to design the program. We’ll start by examining the nouns in the use case descriptions, as mentioned earlier.
Listing the Nouns
Here’s the list of nouns picked out of the use case descriptions:
1.User Interface screen
2.Tenant


Chapter 16
818
number and an array of 12 rents. A rent row doesn’t exist until the first rent of the year has been paid; thereafter, rents are inserted into the existing row. This is a more complicated situation than for tenants and expenses, so we’ll leave rent row as a class. This leaves the rent payment class with no data to hold except the rent amount, so we’ll eliminate this class.
The program can derive the data in the Annual Summary from the Rent Record and the Expense Record, so we probably won’t need to make classes out of the sum of rents, total expenses by category, and balance. These are simply the results of calculations.
This leaves the following classes:
1.User Interface screen
2.Tenant
3.Tenant Input screen
4.Tenant List
5.Rent Input screen
6.Rent Record
7.Rent row
8.Expense payment
9.Expense Input screen
10.Expense Record
11.Annual Summary
Discovering Attributes
Many of the nouns we rejected as classes will be candidates for attributes (member data) in classes. For example, class Tenant will have the attributes Tenant Name and Apartment Number, and class Expense will have the attributes Payee, Month, Day, Amount, and Budget Category. A majority of the attributes can be discovered this way.
From Verbs to Messages
Now let’s look at the use cases to see what light they cast on the messages one class will send to another. Because a message is actually a call to a member function in an object, discovering messages is the same as discovering the member functions of the class receiving the message. As with nouns, not every verb is a candidate for a message. Some relate instead to obtaining information from the user, displaying information or doing other things.


Chapter 16
820
User |
getTenant() |
Tenant |
insertTenant() |
Tenant |
Interface |
|
Input |
|
|
|
|
List |
||
Screen |
|
Screen |
|
|
|
|
|
The program presents the Tenant Input screen, which prompts the user to enter the new tenant's name and apartment number. It then places this information on a new row in the Tenant List. This list is sorted by apartment number.
Not used
FIGURE 16.9
Verbs in the Add a New Tenant use case.
Class Diagram
Once we have an idea what classes we will need and how they relate to each other, we can create a class diagram. We’ve seen examples of class diagrams in earlier chapters. Figure 16.10 is the class diagram of the LANDLORD program.
Sequence Diagrams
Before starting to code, we might want to understand in more detail the steps involved in each use case. One way to do this is to generate a UML sequence diagram. A sequence diagram is one of two kinds of UML interaction diagrams. The other is the collaboration diagram. Both show how events unfold over time, but the sequence diagram depicts time in a more graphical way.
In a sequence diagram the vertical axis represents time, starting at the top and flowing downward. Across the top are rectangles containing the names of the objects that will participate in the use case. The action typically starts with the object on the left sending a message to an object on its right. The further to the right they are, the less important (or the more dependent) the objects usually are.
Note that the diagram shows objects, not classes. We’re going to be focusing on sequences of messages, and messages are sent from object to object, not class to class. In UML diagrams, object names are underlined to distinquish them from class names.
Extending downward from each object is a dotted line called the lifeline. This indicates that the object exists at a particular time. If the object is deleted, its lifeline stops at that point.



