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

Chapter 8: Relationships

To delete a generalization from the model:

1.

Select the desired generalization.

2.

Press Ctrl+D.

OR

1.

Select the desired generalization.

2.

Select Edit → Delete from Model.

OR

1.

Open the specification window for either class participating in the relationship.

2.

Select the Relations tab.

3.

Right−click the relationship.

4.

Select Delete from the shortcut menu.

Working with Relationships

In this section, we'll take a look at the detailed specifications of the relationships in Rose. In your model, you can add things like association names, role names, and qualifiers to specify why the relationship exists.

Before you generate code, you should specify the relationship multiplicity; otherwise Rose will provide a default. Most of the other specifications we present in this section, however, are optional. Rose will notify you if a required specification has not been set when you attempt to generate code.

Setting Multiplicity

Multiplicity indicates how many instances of one class are related to a single instance of another class at a given point in time. For example, if we're looking at a course registration system for a university, we may have classes called Course and Student. There is a relationship between them; courses have students and students have courses. The questions answered by the multiplicity are "How many courses can a student take at one time?" and "How many students can be enrolled in a single course at one time?"

324

Chapter 8: Relationships

Because the multiplicity answers both questions, the multiplicity indicators are included at both ends of the relationship. In the course registration example, we decide that each student can be enrolled in 0 to 4 classes, and each course can have 10 to 20 students. On a Class diagram, this would be shown as in Figure 8.19.

Figure 8.19: Relationship multiplicity

In this figure, the 0..4 means that each student can be enrolled in 0 through 4 classes, and the 10..20 means that each class can have 10 to 20 students.

Keep in mind that the multiplicity settings will let you know whether the relationship is optional. In our example above, a student can take from 0 to 4 courses at any one time. Therefore, someone is still considered a student if they have taken a semester off. Had the multiplicity been 1..4 instead, every student would be required to take at least 1 course per semester. The multiplicity, therefore, implements business rules such as "every student must take at least one course per semester."

Typically, multiplicity between forms, screens, or windows will be 0..1 on each side of the relationship. Although this doesn't always hold true, this multiplicity would indicate that each form can exist independently of the other.

UML notations for multiplicity are as follows:

Multiplicity

Meaning

*

Many

0

Zero

1

One

0..*

Zero or more

1..*

One or more

0..1

Zero or one

1..1

Exactly one

Or, you can enter your own multiplicity, using one of the following formats:

 

 

Format

Meaning

<number>

Exactly <number>

<number 1>..<number 2>

Between <number 1> and <number 2>

<number>..n

<number> or more

<number 1>,<number 2>

<number 1> or <number 2>

<number 1>, <number 2>..<number 3>

Exactly <number 1> or between <number 2> and

 

<number 3>

<number 1>..<number 2>,<number 3>..<number 4>

Between <number 1> and <number 2> or between

 

<number 3> and <number 4>

To set relationship multiplicity:

 

1.

Right−click the desired relationship on one end.

2.

325

Chapter 8: Relationships

Select Multiplicity from the shortcut menu.

3.

Select the desired multiplicity.

4.

Repeat steps 1–3 for the other end of the relationship.

OR

1.

Open the desired relationship's specification window.

2.

Select the Role Detail tab for one end.

3.

Change the multiplicity using the cardinality field.

4.

Repeat steps 1–3 for the other end of the relationship.

Using Relationship Names

Relationships can be refined using relationship names or role names. A relationship name is usually a verb or verb phrase that describes why the relationship exists. For example, we may have an association between a Person class and a Company class. From this, though, we might ask these questions: Why does this relationship exist? Is the person a customer of the company, an employee, or an owner? We can name the relationship employs to specify why the relationship exists. Figure 8.20 is an example of a relationship name.

Figure 8.20: Relationship name

Relationship names are optional, and are typically used only when the reason for the relationship is not obvious. Relationship names are shown along the relationship line.

In Rose, you can also set the relationship direction. In the example above, we can say that the company employs the person, but can't say that the person employs the company. You can set the name direction in the relationship specification window.

To set the relationship name:

1.

Select the desired relationship.

2.

Type in the desired name.

OR

1.

326

Chapter 8: Relationships

Open the desired relationship's specification window.

2.

Select the General tab.

3.

Enter the name in the Name field.

To set the name direction:

1.

Open the desired relationship's specification window.

2.

Select the Detail tab.

3.

Set the name direction using the Name Direction field.

Using Stereotypes

Like other model elements, you can assign a stereotype to a relationship. Stereotypes are used to classify relationships. In particular, when modeling web applications, you may want to set the stereotypes of the association relationships. As we discussed earlier in this chapter, there are four stereotypes in the Rose Web Modeler add−in: a link relationship, a submit relationship, a build relationship, and a redirect relationship. A link relationship is used to show one page that contains a hyperlink to another. A submit relationship shows a form submitting information to a server page. A build relationship is used to show a server page building a client page. Finally, a redirect relationship shows how processing control is passed from one page to another.

Stereotypes are shown along the association line and are enclosed in double angle brackets (<<>>). To set the relationship stereotype, you can use the General tab of the relationship specification window, as shown in Figure 8.21.

327