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

Chapter 7: Attributes and Operations

OR

1.

Right−click the attribute in the browser.

2.

Select Open Specification from the shortcut menu.

3.

Enter the attribute documentation in the Documentation area of the class attribute specification window.

OR

1.

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

2.

Select the Attributes tab.

3.

Select the attribute.

4.

Type the attribute documentation in the documentation window.

Deleting Attributes

At times, you may find that you need to delete an attribute you have created. This is most common when the system requirements change, removing the need for a particular attribute. In Rose, the quickest way to delete an attribute is typically through the browser. However, you can also use a Class diagram to delete an attribute. When an attribute is deleted from one Class diagram, Rose will automatically remove it from the model, including any other Class diagrams on which it appeared.

To delete an attribute from a class:

1.

Right−click the attribute in the browser.

2.

Select Delete from the shortcut menu.

OR

1.

Select the attribute on a Class diagram.

2.

Use the Backspace key to erase the attribute name, data type, and initial value from the diagram.

3.

255

Chapter 7: Attributes and Operations

Single−click anywhere on the diagram.

4.

Rose will confirm the deletion before the attribute is removed.

OR

1.

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

2.

Select the Attributes tab.

3.

Right−click the attribute you want to delete.

4.

Select Delete from the shortcut menu.

5.

Rose will confirm the deletion before the attribute is removed.

Setting Attribute Specifications

As with other Rose model elements, there are a number of detailed specifications you can add to an attribute. These include, among other things, the attribute data type, initial value, stereotype, and visibility. In the next several sections, we'll take a look at each specification.

All of the specifications are viewed or changed on the attribute specification window, as shown in Figure 7.2.

256

Chapter 7: Attributes and Operations

Figure 7.2: Attribute specification window

To open the attribute specifications:

1.

Right−click the attribute in the browser.

2.

Select Open Specification from the shortcut menu.

OR

1.

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

2.

Select the Attributes tab.

3.

Double−click the appropriate attribute.

Note In the following sections, if you are working with a Java, COM, or CORBA class, the specification window will be different. To open the standard specifications, right−click and select Open Standard Specification from the shortcut menu.

Setting the Attribute Data Type

One of the main pieces of information you specify about an attribute is its data type. The data type is the language−specific type, such as string, integer, long, or Boolean. Before you can generate code, you must enter a data type for each attribute.

257

Chapter 7: Attributes and Operations

When you are entering the data type, you can either use built−in data types (string, integer, long, etc.) for your programming language or the names of classes that you have defined in your Rose model. To see the classes you have defined in the drop−down list box, select the Show Classes check box.

To set the attribute data type:

1.

Right−click the attribute in the browser.

2.

Select Open Specification from the shortcut menu. Rose will open the class attribute specification window.

3.

Select a data type from the Type drop−down list box or enter a new data type.

OR

1.

Select the attribute on a Class diagram.

2.

Type a colon and the data type after the attribute name. For example, if you have an attribute called Address that you want to set as a string, type Address : String.

Setting the Attribute Stereotype

Like actors, use cases, and classes, attributes can be stereotyped. An attribute stereotype is a way to classify the attribute. For example, you may have some attributes that map to fields in your database, and other attributes that do not. You can define two stereotypes, one for each of these types of attributes.

In Rose, you are not required to assign stereotypes to attributes. You can generate code without using attribute stereotypes. Stereotypes can, however, improve the readability and comprehensibility of your model.

To set the attribute stereotype:

1.

Right−click the attribute in the browser.

2.

Select Open Specification from the shortcut menu. Rose will open the Class Attribute Specification window.

3.

Select a stereotype from the drop−down list box or enter a new stereotype.

OR

1.

Select the attribute in the browser.

2.

258

Chapter 7: Attributes and Operations

Single−click the attribute again to edit the name. Before the name, the double angle bracket characters (<< >>) will appear:

3.

Type the stereotype between the double angle brackets:

Setting the Attribute Initial Value

Many attributes will have some sort of a default value associated with them. For example, you may have a class called Order, which holds information and behavior about purchase orders for your company. The Order class may have an attribute called TaxRate for the sales tax rate of the purchase. In your city, for example the tax rate may be 7.5%, so most of your orders would be at the 7.5% rate. You can assign an initial value of

.075 to the TaxRate attribute.

Like attribute stereotypes, initial values aren't required in order to generate code. However, if an initial value is present, Rose will generate the code necessary to initialize the attribute.

To set the attribute initial value:

1.

Right−click the attribute in the browser.

2.

Select Open Specification from the shortcut menu. The class attribute specification window will appear.

3.

In the Initial Value field, enter the attribute's default value.

OR

1.

Select the attribute on a Class diagram.

2.

After the attribute data type, enter an equals sign (=), followed by the default value. For example, if you have an integer attribute called EmployeeID and you wish to set its default value to 0, your Class

259

Chapter 7: Attributes and Operations

diagram would look like this:

Setting the Attribute Visibility

One of the central concepts of object−oriented programming is that of encapsulation. Each class, by having attributes and operations, encapsulates a little bit of information and a little bit of behavior. One of the benefits of this approach is the ability to have small, self−contained pieces of code. The Employee class, for example, has all of the information and behavior related to an employee.

You can view a class like this:

The private attributes are contained inside the class, hidden away from other classes. Because the attributes are encapsulated within a class, you will need to define which classes have access to view and change the attribute. This is known as the attribute visibility.

There are four visibility options for an attribute. Let's look at each in the context of an example: We have an Employee class with an attribute called Address, and a Company class.

Public Suggests that the attribute is visible to all other classes. Any other class can view or modify the value of the attribute. In this case, the Company class could view or modify the value of the Address attribute of the Employee class. The UML notation for a public attribute is a plus sign (+).

Private Means that the attribute is not visible to any other class. The Employee class would know what the value of the Address attribute is, and would be able to change the value, but the Company class wouldn't be able to view or edit the Address attribute. If the Company class needed to view or edit Address, it would have to ask the Employee class to view or edit the attribute. This is typically done through public operations. We'll talk more about this in the operations part of this chapter. The UML notation for a private attribute is a minus sign (−).

Protected Suggests that the class and any of its descendants have access to the attribute. In our example, assume we have two different types of employees, hourly employees and salaried employees. The classes

260

Chapter 7: Attributes and Operations

HourlyEmp and SalariedEmp are inherited from the Employee class. If the Address attribute has protected visibility, it can be viewed or changed by the Employee class, the HourlyEmp class, or the SalariedEmp class, but not by the Company class. The UML notation for a protected attribute is a pound sign (#).

Package or implementation Indicates that the attribute is public, but only to classes in the same package. In our example, assume the Address attribute of the Employee class has package visibility. Address could be modified by Company only if Company and Employee are in the same package. With implementation visibility, no icon appears next to the attribute.

In general, private or protected visibility is recommended for attributes. Using these options helps you maintain better control of your code and the attribute. By using private or protected attributes, you won't have a situation where an attribute is being modified by all sorts of classes throughout the system. Instead, the logic for modifying an attribute is encapsulated in a single class, along with the attribute. The visibility options you select will affect the generated code. For example, Figure 7.3 is the Java code generated for the class in the previous example.

261

Chapter 7: Attributes and Operations

Figure 7.3: Attribute visibility in generated code

Rose supports two sets of visibility notations. The first is UML notation (+, −, #) for public, private, and protected attributes, respectively. The second notation includes four Rose icons, as shown in Table 7.1.

Table 7.1: Rose Visibility Icons

Icon Description

Public

Private

Protected

Package or implementation

On a Class diagram, you can use either of these notations. See the upcoming numbered steps for a description of how to switch between these two notations. Figure 7.4 shows an example of a class using UML visibility notation. Figure 7.5 shows the same class using Rose visibility notation. Rose and UML visibility notations are summarized in Table 7.2.

262

Chapter 7: Attributes and Operations

Figure 7.4: UML visibility notation

Figure 7.5: Rose visibility notation

Table 7.2: Rose and UML Visibility Notations

Rose Notation

If you want to…

Use…

UML Notation

 

Make an attribute visible to all

Public visibility

+

 

classes

 

 

 

Make an attribute visible to only one

Private visibility

 

class

 

 

 

Make an attribute visible to a class

Protected visibility

#

 

and its descendants

 

 

 

Make an attribute visible to all

Package or implementation visibility

<no icon>

 

classes in the same pacakage

 

 

To set the attribute visibility:

 

 

1.

 

 

 

Right−click the attribute in the browser.

 

 

2.

Select Open Specification from the shortcut menu. The class attribute specification window will appear.

3.

In the Export Control field, select the attribute's visibility: Public, Protected, Private, or Implementation. By default, all attributes have private visibility.

OR

1.

Select the attribute on a Class diagram.

2.

If you are using UML notation for visibility, single−click the icon (+, −, or #) next to the attribute. Select a visibility option from the list of Rose visibility icons that appears.

3.

263