Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 7: Attributes and Operations

In the previous chapter, we looked at classes and packages. Remember that classes are encapsulated attributes (data) and the operations (behaviors) that act on those attributes. Now we will discuss working with attributes and operations. We will begin by talking about how to find attributes, add them to the Rose model, and add details about the attributes. We will then look at finding operations, adding them to the model, and adding details to the operations. Next, we will look at displaying attributes and operations on Class diagrams. Lastly, we will discuss how to map operations to messages on Interaction diagrams.

Working with Attributes

An attribute is a piece of information associated with a class. For example, a Company class might have attributes called Name, Address, and NumberOfEmployees. A Ticket class might have attributes called FlightNumber, Cost, and PassengerName.

In Rose, you may add one or more attributes to each of the classes in the model. The following sections describe how to find attributes, add them to the model, delete them from the model, and set the detailed specifications for each attribute.

Finding Attributes

There are many sources of attributes. To begin, you can take your use case documentation and look for nouns in your flow of events. Some of the nouns will be objects or classes, some will be actors, and others will be attributes. For example, your flow of events may read "The user enters the employee's name, address, social security number, and phone number," letting you know that the Employee class has attributes called Name, Address, SSN, and Phone. In our airline example, the user is looking for a flight and enters a departure and destination city as well as a departure date. From this, we know that a Flight class would have attributes called DepartureCity, DestinationCity, and DepartureDate.

Another good place to look is the requirements document. There may be requirements that outline what information should be collected by the system. Any piece of information that is collected should be an attribute in a class.

Finally, you can check the database structure. If your database structure has already been defined, the fields in the tables will give you a good idea of what your attributes are. Frequently, there's a one−to−one mapping between database tables and entity classes. Going back to our previous example, a Flight table may have fields called DepartureCity, DestinationCity, and DepartureDate. The corresponding class, called Flight, will have attributes called DepartureCity, DestinationCity, and DepartureDate. It's important to note that there isn't always a one−to−one mapping between the database tables and the classes. There are different considerations when designing your database and designing your classes. Relational databases, for example, don't directly support inheritance.

However, when you identify attributes, be sure that each one can be traced back to a requirement. This can help solve the classic problem of an application capturing a great deal of information that nobody uses. Each requirement should be traced back to the flow of events of a use case, to a particular requirement, or to an existing database table. If you cannot trace the requirement, you cannot be sure that it is needed by the customer. This can be a bit of a deviation from some older methodologies—rather than create the database structure first and then wrap the system around it, you're building the system and the database at the same time to conform to the same requirements.

251

Chapter 7: Attributes and Operations

As you identify attributes, carefully assign them to the appropriate classes. An attribute should be a piece of information related to the class. For example, an Employee class might have name and address information, but shouldn't include information about the products the employee's company manufactures. A Product class would be a better place to store information about products.

Be cautious of classes with too many attributes. If you find that a particular class has a large number of attributes, it might be an indication that the class should be split into two smaller classes. If you have a class with more than about 10 or 15 attributes, be sure to take a close look at it. The class may be perfectly legitimate; just be sure that all of the attributes are needed and truly belong to that class. Similarly, be cautious of classes with too few attributes. Again, it may be perfectly legitimate. Control classes, for example, tend to have very few attributes. However, it may also be a sign that two or more classes should be combined. If you have a class with only one or two attributes, it may be worth a closer look.

Sometimes you may run into a piece of information and wonder whether it's an attribute or a class. For example, let's look at an attribute like Company Name. The question you face might be: Is the company name an attribute of a Person class, or should Company be its own class? The answer really depends on the application you are writing. If you need to keep information about the company and there is some behavior associated with a company, it may be its own class. For example, you may be building a system to keep track of your customers. In this case, you'll want to keep some information about the companies you sell products or services to. You may want to know how many employees the company has, the company's name and address, the name of your contact with the company, and so on.

On the other hand, you may not need to know specific information about the company. You may be writing an application that will generate letters to your contacts in other organizations. When generating the letters, you will need to know a company name. However, you don't need to know any more information about the company. In this case, you could consider the company name to be an attribute of a Contact class.

Another thing to consider is whether the piece of information in question has behavior. If the Company has some behavior in your application, it is better modeled as a class. If the company has no behavior, it may be better modeled as an attribute.

Once you've identified the attributes, the next step is to add them to your Rose model. In the following sections, we'll discuss how to add attributes and add details to the attributes, such as the data type and default value.

Adding Attributes

As you identify attributes, you can add them to the appropriate class in your Rose model. There are three main pieces of information you can supply for each attribute: the attribute name, the data type, and the initial value. Before you can generate code for your model, you must supply a name and data type for each attribute. Initial values are optional.

There are three ways to add an attribute. You may type the attribute directly onto a Class diagram, add the attribute using the browser, or add the attribute using the class specification window.

Once you've added an attribute, you can add documentation for it. Typically, attribute documentation would include a short description or definition of the attribute. Any attribute documentation will be included as a comment in the code generated from the model. By documenting the attributes as you go along, you are beginning to document the code as well.

To add an attribute to a class:

1.

252

Chapter 7: Attributes and Operations

Right−click the class on a Class diagram.

2.

Select New → Attribute.

3.

Type the attribute name, using the format Name : Data Type = Initial Value. For example:

Address : String

IDNumber : Integer = 0

The data type is required in order to generate code, but the initial value is optional.

4.

To add more attributes, press Enter and type the new attributes directly on the Class diagram.

OR

1.

Right−click the class in the browser.

2.

Select New → Attribute.

3.

A new attribute, called "name" by default, will appear under the class in the browser. Type the name of the new attribute. Attribute data types and default values cannot be assigned in the browser; you can enter them on the Class diagram, as we'll discuss shortly.

OR

1.

Open the class specification window for the attribute's class.

2.

Select the Attributes tab. If the class already has some attributes, they will be listed here.

3.

Right−click anywhere inside the Attributes area, as in Figure 7.1.

253

Chapter 7: Attributes and Operations

Figure 7.1: Adding a new attribute in the class specification window

4.

Select Insert from the shortcut menu.

5.

Type the name of the new attribute.

6.

Enter the visibility, stereotype, data type, and initial value in the appropriate columns. We'll discuss each of these in detail in the following sections.

To add documentation to an attribute:

1.

Select the attribute in the browser.

2.

Type the attribute documentation in the documentation window.

OR

1.

Select the attribute on a Class diagram.

2.

Type the attribute documentation in the documentation window.

254