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

Chapter 7: Attributes and Operations

might have attributes called Width and Height. It might also have an attribute called Area, which is calculated from the width and height. Because Area is derived from the Width and Height, two other attributes, it is considered a derived attribute.

In UML, derived attributes are marked with a slash (/) before the attribute name. In the above example, the Area attribute would be written as /Area.

To specify a derived attribute:

1.

Right−click the attribute in the browser.

2.

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

3.

Select the Detail tab.

4.

Select the Derived check box to mark the attribute as derived. Rose will place a slash (/) before the attribute name on the Class diagram.

Working with Operations

An operation is a behavior associated with a class. An operation has three parts: the operation name, the operation parameters, and the operation return type. The parameters are arguments the operation receives as input. The return type is the output of the operation.

On a Class diagram, you can view either the operation name or the operation name followed by the parameters and return type. To reduce clutter on Class diagrams, it can be helpful to have some Class diagrams with operation names only and others with the full operation signature, including parameters and the return type.

In UML, operations are displayed using the following notation:

Operation Name(argument1 : argument1 data type, argument2 : argument2 data type, …) : return type

Operations define the responsibilities of your classes. As you identify operations and examine your classes, keep a few things in mind:

266

Chapter 7: Attributes and Operations

Be suspicious of any classes with only one or two operations. It may be perfectly legitimate, but it may also indicate that the class should be combined with another.

Be very suspicious of classes with no operations. A class typically encapsulates both behavior and information. A class with no behavior might be better modeled as an attribute or two.

Be wary of classes with too many operations. A class should have a manageable set of responsibilities. If you have a class with too many operations, it may be difficult to maintain. Dividing it instead into two smaller classes may ease maintenance.

In this section, we'll talk about finding operations, adding them to your Rose model, and adding the operation details. We'll also take a look at how operations can be displayed on the Class diagrams.

Finding Operations

Finding operations is fairly straightforward. As you created your Sequence and Collaboration diagrams, you did most of the work necessary to find operations.

There are four different types of operations to consider: implementer, manager, access, and helper.

Implementor Operations

Implementor operations implement some business functionality. They are found by examining Interaction diagrams. The Interaction diagrams focus on business functionality, and each message on the diagram will most likely be mapped to an implementor operation.

Each implementor operation should be able to be traced back to a requirement. This is achieved through the various pieces of the model. Each operation comes from a message on an Interaction diagram, which comes from the details in the flow of events, which comes from the use case, which comes from the requirements.

This ability to trace can help you ensure that each requirement is implemented in the code and that each piece of code can be traced back to a requirement.

Manager Operations

Manager operations manage the creation and destruction of objects. For example, the constructor and destructor operations of a class fall into this category.

In Rose, you don't need to manually create constructor and destructor operations for each of your classes. When you generate code, Rose gives you the option of automatically generating constructors and destructors.

Access Operations

Attributes are typically private or protected. However, other classes may need to view or change the attributes of a particular class. This can be accomplished through access operations.

For example, if we have an attribute called Salary in an Employee class, we wouldn't want all of the other classes to be able to go in and change the Salary. Instead, we add two access operations to the Employee class: GetSalary and SetSalary. The GetSalary operation, which is public, can be called by other classes. It will go in and get the value of the Salary attribute, and return this value to the calling class. The SetSalary operation,

267

Chapter 7: Attributes and Operations

which is also public, will help another class set the value of the Salary attribute. SetSalary can contain any validation rules for the salary that must be checked before the value in Salary is changed.

This approach keeps the attributes safely encapsulated inside a class and protected from other classes, but still allows controlled access to the attributes. The industry standard has been to create a Get and Set operation for each attribute in a class.

As with manager operations, you don't have to manually enter each access operation. When you generate code, Rose can automatically create Get and Set operations for each of the attributes in the class.

Helper Operations

Helper operations are those operations that one class needs to carry out its responsibilities but that other classes don't need to know about. These are the private and protected operations of a class.

Like implementor operations, helper operations are found by examining the Sequence and Collaboration diagrams. Frequently, helper operations appear as reflexive messages on a Sequence or Collaboration diagram.

To identify operations, you can perform the following series of steps:

1.

Examine Sequence and Collaboration diagrams. Most messages will become implementor operations. Reflexive messages may become helper operations.

2.

Consider manager operations. You may want to add constructors and destructors. Again, this isn't required; Rose can generate these for you when code is generated.

3.

Consider access operations. Create a Get and Set operation for any attribute that will need to be viewed or changed by another class. As with manager operations, these don't need to be manually added; Rose can generate them for you when code is generated.

Adding Operations

Like attributes, operations can be added to your Rose model through a Class diagram or through the browser. You can also add operations to a class through the class specification window.

Once you've added an operation, you can add documentation to it. Any documentation you add to an operation will be included as a comment in the generated code. Documentation for operations typically includes information like the purpose of the operation, a short description of the operation's parameters, and the operation return type.

To add an operation to a class:

1.

Right−click the class on a Class diagram.

2.

Select New Operation.

3.

268

Chapter 7: Attributes and Operations

Type the operation name, using the following format:

Name(Argument1 : Argument1 data type): Operation Return Type

For example:

Add(X : Integer, Y: Integer) : Integer

Print(EmployeeID : Long) : Boolean

Delete() : Long

4.

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

OR

1.

Right−click the class in the browser.

2.

Select New → Operation.

3.

A new operation, called "opname" by default, will appear under the class in the browser. Type the name of the new operation. Rose does not allow you to enter the operation arguments or return type value in the browser; as with attributes, you can enter these details on the Class diagram.

OR

1.

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

2.

Select the Operations tab. If the class already has some operations, they will be listed here.

3.

Right−click anywhere inside the operations area, as shown in Figure 7.6.

269

Chapter 7: Attributes and Operations

Figure 7.6: Adding a new operation in the class specification window

4.

Select Insert from the shortcut menu.

5.

Type the name of the new operation in the Operation column.

6.

Enter the visibility, stereotype, and return type in the appropriate columns.

To add documentation to an operation:

1.

Select the operation in the browser.

2.

Type the operation documentation in the documentation window.

OR

1.

Select the operation on a Class diagram.

2.

Type the operation documentation in the documentation window.

OR

1.

270