Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

lafore_robert_objectoriented_programming_in_c

.pdf
Скачиваний:
51
Добавлен:
27.03.2023
Размер:
8.94 Mб
Скачать

Object-Oriented Software Development

We should note that the Unified Process can be applied to any type of software architecture, not just to object-oriented languages. In fact, a potential weakness of this pocess is that it does not actively encourage object-oriented design.

The elaboration phase of the Unified Process usually begins with a technique called use case modeling. This is the starting point for developing a detailed design for the system. For this reason the Unified Process is said to be a use case driven process. In the next section we’ll discuss use case modeling, and in the section after that we’ll apply it to a sample software project.

Use Case Modeling

Use case modeling allows future users of a software system as much input as possible into its design. It uses the vocabulary of the users, not programmers. This focus on users means that the initial specification of the program can be understood both by its users and by the software engineers designing it.

There are two main entities in the use case approach: actors and use cases. Let’s see what they are.

Actors

An actor is (usually) a person who will use the system we are designing. A bank customer interacting with the software of an ATM machine is an actor. An astronomer inputting the coordinates of a star to a telescope aiming program is an actor. A bookstore clerk checking the computer to see if a particular book is available is an actor. Usually an actor initiates some operation, although sometimes the actor may act in other ways, such as receiving information or assisting in an operation.

Actually, “role” is a probably a better name than “actor.” One human playing different roles may be represented by several actors. For example, in a small business, Harry Jones might be represented by an actor called “salesperson” when making a sale, but by an actor called “bookkeeper” when adding up the day’s sales. Conversely, a single actor may represent several different individuals. Harry, Jose, and Elma may all be represented by the actor called “salesperson.”

Other systems connected to the one we’re designing, such as a different computer system or a link to the Web, may also be actors. For example, the computer system in a particular bookstore may be linked to a remote system in the head office. This remote system can be considered an actor in the bookstore’s system.

805

16

O D BJECT EVELOPMENT S

OFTWARE - RIENTEDO

Chapter 16

806

In a large project, just identifying all the actors may be difficult. The designer needs to look for people or other systems that:

Provide information to the system

Need information from the system

Assist other actors

Use Cases

A use case is a specific task, usually initiated by an actor. It describes a single goal the actor wants to attain. Examples are the withdrawal of cash by the bank customer, the aiming of the telescope by the astronomer, and the investigation of a book’s availability by the bookstore clerk.

In most situations the use case is initiated by the actor, but sometimes it’s initiated by the system, as when the electric company’s accounting program sends you a reminder that you haven’t paid your bill, or your car’s computer turns on a warning light when it decides the engine is too hot.

In general, everything you want the system to do should be specified by a use case.

Scenarios

A use case usually consists of a number of scenarios. The use case specifies a goal, while a scenario represents a particular outcome when attempting to reach that goal. For example, let’s consider a use case consisting of a bookstore clerk querying the store’s computer system for the location of a particular book. There are several possible outcomes or scenarios:

The book is in the store and the computer displays its shelf location.

The book is out of stock, but the system gives the customer the opportunity to order it from the publisher.

The book is not only out of stock, it’s out of print; so the sytem informs the customer that she or he is out of luck.

In a formal development process, each scenario would have its own documentation, describing in detail all the events in the scenario.

Use Case Diagrams

The UML specifies how to diagram use cases. Actors are represented by stick figures; use cases by ovals. A rectangular frame surrounds the use cases, leaving the actors outside. This rectangle is the system boundary. The system inside is what the software developer is trying to design. Figure 16.3 shows a use case diagram for a bookstore computer system.

Object-Oriented Software Development

807

Find Book

 

Information

 

Clerk

 

Record Sale

 

 

Sales

 

Clerk

Order Book

 

Review

 

Sales Data

 

Request

Manager

Sales Data

 

Central

 

Office

 

System

 

Book Store System

 

FIGURE 16.3

Use case diagram for a bookstore.

In the use case diagram, lines, called associations, connect actors with use cases. Typically the lines don’t show any direction, but arrows can be used to show who initiated the use case.

In this diagram we assume that the bookstore is part of a chain, and that accounting and similar functions are handled at a central office. Clerks in the store record the sale of each book and request information about a book’s location and availability. The manager can check what books have been sold and order new ones. The actors are Sales Clerk, Information Clerk, Manager, and the Central Office System. The use cases are Record Sale, Find Book, Order Book, Review Sales Data, and Request Sales Data.

Use Case Descriptions

The use case diagram does not have room for detailed descriptions of individual use cases, so these must be supplied separately. Different degrees of formality are used in these use case descriptions, depending on the size of the project and the philosophy of the developers. In most cases there will be a detailed description of each scenario in a use case.

16

O D BJECT EVELOPMENT S

OFTWARE - RIENTEDO

Chapter 16

808

The simplest version of a use case description is a paragraph or so of text. Sometimes two columns are used, with the actor’s actions in one column and the system’s response in another. A more formal version might include such details as preconditions, postconditions, and a detailed sequence of steps. A UML diagram called an activity diagram, which is a form of flowchart, is sometimes used to show graphically the sequence of steps in a use case.

Use case diagrams and use case descriptions are primarily used in the initial design of a system to aid communication between users and developers. However, they are also useful throughout the development process. They can be consulted whenever anyone needs to verify what the system is supposed to do, and they can even provide a basis for testing and documentation.

From Use Cases to Classes

When the actors and use cases have been identified, the development process moves from the elaboration to the construction phase. The emphasis shifts from the users to the developers. Our first concern is to develop the classes that will be used in the program.

One approach is to look at the nouns in the use case descriptions. We want objects in the program to correspond with objects in the real world, and these nouns represent real-world entities specified by the users. They are candidates for classes, but not all nouns make good classes. We need to eliminate nouns that are too general, too trivial, or which are better represented as attributes (simple variables).

Once we have some candidate classes, we can begin to understand how they interact by examining the verbs in the use case descriptions. In many cases a verb translates into a message sent from one object to another, or some other association between classes.

A UML class diagram (described in previous chapters) can be used to show classes and their relationships. A use case is realized by a sequence of messages between objects. We can use another UML diagram, an interaction diagram, to detail such a sequence. In fact, we might want to use a separate interaction diagram for each of the scenarios in a use case. We’ll see examples of sequence diagrams, one kind of interaction diagram, in the next section.

The development process is easier to visualize using an example, so let’s walk through the development of a real program. Of necessity this example is so small that it’s questionable whether it even requires a formal development process. However, applying the process to even this small project should help to demystify the topics we’ve mentioned.

Object-Oriented Software Development

809

The Programming Problem

The program we’ll design in this chapter is called LANDLORD. You may or may not like your landlord, but you can understand the sorts of data (such as rents and expenses) that the landlord must deal with. This gets us started with an easily-understood business domain (what we’re writing the program about).

Let’s suppose that you’re an independent programmer, and you’re approached by a potential customer whose name is Beverly Smith. Beverly is a small-time landlord: She owns an apartment building with 12 units. She wants you to write a program that will make it easier for her to record data and print reports regarding the finances of the apartment building. If you and Beverly can agree on payment, schedule, and the overall purpose of the program, you’ve completed the inception part of the development process.

Hand-Written Forms

Currently Beverly is recording all the information about her apartment building by hand, in old-fashioned ledger books. She shows you the forms she’s currently using. There are four of them:

The Tenant List

The Rental Income Record

The Expense Record

The Annual Summary

The Tenant List shows apartment numbers in one column and the corresponding tenant’s names in the adjacent column.

The Rental Income Record records incoming rent payments. It contains 12 columns, one for each month; and one row for each apartment number. Each time Beverly receives a rent payment from a tenant, she records it in the appropriate row and column of the Rental Income Record, which is shown in Figure 16.4.

The layout of the Rental Income Record makes it easy to see which rents have been paid.

The Expense Record records outgoing payments. It’s similar to your personal check register. It has columns for the date, the payee (the company or person to whom Beverly writes the check), and the amount being paid. In addition, there’s a column where Beverly can specify the budget category to which the payment should be charged. Budget categories include Mortgage, Repairs, Utilities, Taxes, Insurance, and so on. The Expense Record is shown in Figure 16.5.

16

O D BJECT EVELOPMENT S

OFTWARE - RIENTEDO

Chapter 16

810

FIGURE 16.4

The Rental Income Record.

FIGURE 16.5

The Expense Record.

Object-Oriented Software Development

The Annual Report uses data from the Rental Income Record and the Expense Record to summarize how much money came in and how much went out during the year. All the rents are summed and the result is displayed. The expenses are summed and displayed by budget category, which makes it easy to see, for example, how much was spent on repairs during the year. Figure 16.6 shows the Annual Report. Finally, expenses are subtracted from income to show how much money Beverly made (or lost) during the year.

FIGURE 16.6

The Annual Report.

In Beverly’s existing system, the Annual Report isn’t created until the end of the year, when all the rents and expenses for December have been recorded. Our computerized system should be able to show a partial Annual Report at any time in the year.

Beverly tells you she wants the program to pretty much duplicate what she’s currently doing on the paper forms. She wants to be able to enter data about tenants, rents, and expenses, and display the various reports.

Assumptions

Of course we’ve already made some simplifying assumptions. There are other kinds of data associated with running an apartment building, such as damage deposits, depreciation, mortgage interest, and income from late fees and the rental of laundry machines. We won’t consider these details.

811

16

O D BJECT EVELOPMENT S

OFTWARE - RIENTEDO

Chapter 16

812

There are also other kinds of reports Beverly might want, such as a Net Worth statement. It might even be nice to have the program interface with an income tax program and online banking. And from a big-picture perspective, there are commercial landlord programs available, so it might not be smart for Beverly to contract to have one custom-written. We’ll ignore all of these distractions to make the problem more tractable.

The Elaboration Phase for the LANDLORD Program

In the elaboration phase of a major software development project, a group of people, which includes the potential users of the program and the software people designing it, meet to discuss what the program should do. In this small example the group is Beverly, who will be using the system, and you, the software expert who will both design and code it.

Actors

The group begins by identifying the actors. Who will be inputting information into the program? Who will be requesting information? Will anyone else interact with the program? Will the program interact with other programs or systems?

In the LANDLORD example, only one person will be using the program: the landlord. The same person inputs information and asks to see it displayed in various ways.

Even in this small project one can imagine other actors. If the landlord’s accountant could access the program’s data (perhaps via the Internet), the accountant would be an actor, and if the program provided data to an income tax program, that program would also be an actor. For simplicity we’ll ignore these possibilities.

Use Cases

Next the group considers what tasks the actor will want to carry out. In a real software project this would be a major effort, with input from many users and much discussion and refining of ideas. Here it’s not too complicated to list the tasks the landlord needs. These tasks are recorded on a use case diagram.

In our situation the the landlord actor will need to do the following:

Start the program

Add a new tenant to the Tenant List

Input a rent to the Rent Record

Input an expense to the Expense Record

Display the Tenant List

Display the Rent Record

Object-Oriented Software Development

813

 

• Display the Expense Record

16

• Display the Annual Summary

EVELOPMENTD OFTWARES RIENTEDO-BJECTO

The resulting use case diagram is shown in Figure 16.7.

 

Start the

 

program

 

Add new

 

tenant

 

 

Input

 

rent

Input

 

expense

 

 

Display

Landlord

Tenant List

Display

Rent Record

Display

Expense Record

Display

Annual Summary

Landlord program

FIGURE 16.7

Use case diagram for the LANDLORD program.

Use Case Descriptions

Now we need to describe each use case in more detail. As noted earlier, use case descriptions can be quite formal and complex. However, our project is so simple that short prose descriptions are all we need.

Start the Program

This may seem too obvious to mention, but when it’s first started, the program should display a screen from which the user can choose the task to perform. This can be called the User Interface screen.

Chapter 16

814

Add a New Tenant

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 automatically sorted by apartment number.

Input a Rent Payment

The Rent Input screen prompts the user to enter the tenant’s name, the month the rent is for, and the amount of rent received. The program looks in the Tenant List for the name of the tenant, and uses the corresponding apartment number to access the Rent Record. If this is the first time the tenant has paid rent, a new row is created in the Rent Record and the rent amount is inserted for the appropriate month. Otherwise, the rent is inserted in the existing row.

Input an Expense Payment

The Expense Input screen prompts the user to enter the payee (the person or company the landord is paying), the amount paid, the day and month the payment was made, and the budget category. The program then creates a new row containing this information and inserts it in the Expense Record.

Display the Tenant List

The program displays the Tenant List, each row of which contains an apartment number and the tenant’s name.

Display the Rent Record

The program displays the Rent Record, each row of which contains an apartment number and the amount paid each month.

Display the Expense Record

The program displays the Expense Record, each row of which contains the month, day, payee, amount, and budget category.

Display the Annual Summary

The program displays the Annual Summary, which consists of

1.The sum of all rents paid for the year to date

2.A list of the total expenses for each budget category

3.The resulting balance (profit or loss for the year to date)