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

Chapter 14: Java Code Generation and Reverse Engineering

{

}

}

In this situation, arrays are created at both ends of the relationship. An array is used by default, but as we mentioned earlier, you can change the container class that is used. To do so, use the relationship specification window for the association. On the Java A or Java B tab, change the ContainerClass property to the name of the container class you wish to use.

To change the container class for all many−to−many association relationships, choose Tools → Options from the menu. On the Java tab, select Role from the drop−down list box. Change the value of the ContainerClass code−generation property to the container class you wish to use.

Reflexive Associations

A reflexive association is treated much the same as an association between two classes. Let's look at the code generated for the following situation:

//Source file: C:\\EnginePart.java

public class EnginePart

{

public EnginePart theEnginePart[];

public EnginePart()

{

}

}

As with a regular association, an attribute is created inside the class to support the relationship. If the multiplicity is one, a simple attribute is created. If the multiplicity is more than one, an array is created as an attribute, as above.

Aggregations

There are two types of aggregation relationships: by−value and by−reference. With a by−value relationship, one class contains another. With a by−reference relationship, one class contains a reference to another. Code generated for either type of aggregation in Java is the same.

Here we have an aggregation relationship between a flight schedule and the flights on the schedule. Each schedule contains one or more flights, and each flight can be on zero or more schedules.

476

Chapter 14: Java Code Generation and Reverse Engineering

The code that is generated shows a Flight attribute inside Schedule. Because the multiplicity of the relationship is one or more, there is an array of Flights inside Schedule.

The following is the code for the Schedule class:

//Source file: C:\\Schedule.java

public class Schedule

{

private date BeginDate; private date EndDate; public Flight theFlight[];

public Schedule()

{

}

}

The following is the code for the Flight class:

//Source file: C:\\Flight.java public class Flight

{

private int FlightNumber; private date FlightDate;

public Flight()

{

}

/**

@roseuid 39446ECD011F */

public boolean AddPassenger(int PassengerID)

{

}

/**

@roseuid 39446ED40101 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 39446EDB03BE */

public boolean CancelFlight()

{

}

}

477

Chapter 14: Java Code Generation and Reverse Engineering

Rose uses the value in the ContainerClass property when the multiplicity of the relationship is greater than one. If the ContainerClass contains no value, then Rose will use an array to contain the multiple objects.

Dependency Relationships

With a dependency relationship, no attributes are created. If there is a dependency between Class A and Class B, no attributes will be created in either Class A or Class B. Here we have a dependency between Flight and Passenger:

The code that is generated will look something like the following:

//Source file: C:\\Flight.java

public class Flight

{

private int FlightNumber; private date FlightDate;

public Flight()

{

}

/**

@roseuid 39446ECD011F */

public boolean AddPassenger(int PassengerID)

{

}

/**

@roseuid 39446ED40101 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 39446EDB03BE */

public boolean CancelFlight()

{

}

}

and

//Source file: C:\\Passenger.java

478

Chapter 14: Java Code Generation and Reverse Engineering

public class Passenger

{

private string FirstName; private string LastName; private string Address; private string City; private string State; private long Zip;

private string Phone;

public Passenger()

{

}

}

Rose will place no references to Flight inside Passenger or Passenger inside Flight. The dependency relationship does not generate any code for the relationship.

Generalization Relationships

A generalization relationship in UML becomes an inheritance relationship in Java. In your Rose model, an inheritance relationship is shown as follows:

For this type of relationship, Rose will generate something that looks like this:

//Source file: Parent.java

//Source file: C:\\Parent.java

public class Parent

{

public Parent()

{

}

}

and

//Source file: C:\\Child.java

public class Child extends Parent

{

479