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

Chapter 8: Relationships

Select the Relations tab.

3.

Right−click the relationship.

4.

Select Delete from the shortcut menu.

Generalizations

A generalization is an inheritance relationship between two model elements such as classes, actors, use cases, and packages. It allows one class to inherit the public and protected attributes and operations of another class. For example, we may have the relationship shown in Figure 8.18.

Figure 8.18: Generalization relationship

In this example, we have two types of employees: hourly and salaried, both of which inherit from the Employee class. The Employee class is known as the superclass, and the HourlyEmp and SalariedEmp classes are known as the subclasses. The arrow points from the subclass to the superclass.

The elements that are common to both types are placed in the Employee class. Both HourlyEmp and SalariedEmp inherit the Name, Address, and SSN attributes of the Employee class and the Hire() and Fire() operations of the Employee class.

Each subclass can have its own unique attributes, operations, and relationships in addition to those it inherits. For example, only hourly employees have an hourly rate, and only salaried employees have a salary.

321

Chapter 8: Relationships

Generalization relationships can save a great deal of time and effort in both development and maintenance. In the above example, you don't need to program and maintain two separate copies of the Hire() operation (one in HourlyEmp and one in SalariedEmp). Instead, you can create and maintain one copy of the operation. Any changes you make to the operation will be automatically inherited by HourlyEmp and SalariedEmp, and any other subclasses of Employee.

When you are defining your generalizations, you can build your inheritance structure either from the top down or the bottom up. To build the structure from the top down, look for classes that might have different types. For example, you would start with the Employee class and soon realize that there are different types of employees. To build the structure from the bottom up, look for classes with commonality. Here, you would start with the HourlyEmp and SalariedEmp classes and then realize that they are similar. You would then create an Employee class to hold the common elements.

Be careful not to create an inheritance structure that isn't maintainable. A hierarchy with too many layers can become very difficult to maintain. Every change toward the top of the structure will ripple down through the hierarchy to the classes below. While this can be an advantage, it can also make change analysis and control even more essential. In some languages, too many layers can also slow down the application.

Creating Generalizations

As you discover generalization relationships, you can add them to your Rose model using a Class diagram. Frequently, an organization will create a single Class diagram or two that are dedicated to the inheritance structure. These Class diagrams will typically show only limited attribute or operation information.

As you add generalizations, you may need to move some of the attributes and operations. For example, you may start with the HourlyEmp and SalariedEmp classes, each of which has an attribute called Address, and now, you want to move the Address attribute up the structure to the Employee class. In the browser, you can drag the Address attribute from either the HourlyEmp or the Salaried−Emp class to the Employee class. Be sure to remember to remove the other copy of Address from HourlyEmp or SalariedEmp.

To create a generalization on a Class diagram:

1.

Select the Generalization icon from the toolbox.

2.

Drag the generalization line from the subclass to the superclass.

OR

1.

Select Tools → Create → Generalization.

2.

Drag the generalization line from the subclass to the superclass.

To add documentation to a generalization:

1.

Double−click the desired generalization.

2.

322

Chapter 8: Relationships

Select the General tab.

3.

Enter documentation in the Documentation field.

OR

1.

Select the desired generalization.

2.

Select Browse → Specification.

3.

Select the General tab.

4.

Enter documentation in the Documentation field.

To change a relationship to a generalization:

1.

Select the desired relationship.

2.

Select Edit → Change Into → Generalization.

Deleting Generalizations

As with the other relationships, you can delete a generalization from a single diagram or from the model. If you delete a generalization from the entire model, keep in mind that the attributes, operations, and relationships of the superclass will no longer be inherited by the subclass. Therefore, if the subclass needs those attributes, operations, or relationships, they will have to be added to the subclass.

To delete a generalization from the diagram:

1.

Select the desired generalization.

2.

Press the Delete key.

OR

1.

Select the desired generalization.

2.

Select Edit → Delete.

Note Deleting a generalization from the diagram does not delete it from the model.

323