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

Chapter 14: Java Code Generation and Reverse Engineering

@roseuid 39459113021D */

public boolean DeleteAccount(int AccountID)

{

}

}

Rose will use the role name to generate the attribute name. By default, each generated attribute will have public visibility. To change the visibility, open the association specification window. Select either the Role A General or Role B General tab, and change the Export Control property.

Note that this association has a multiplicity of one to one. See the upcoming sections titled "Associations with a Multiplicity of One to Many" or "Associations with a Multiplicity of Many to Many" for a discussion of how other multiplicity settings will affect code generation.

You can specify initial values for the generated attributes. Open the desired association's standard specification, then select the Java A or Java B tab. Modify the InitialValue property to contain the initial value for the generated attribute.

Unidirectional Associations

As with bidirectional associations, Rose will generate attributes to support unidirectional associations. With a unidirectional association, however, an attribute is generated at only one end of the relationship. Let's look at the Passenger and FrequentFlyerAccount classes again, but this time with a unidirectional relationship.

For these classes, code similar to the following code for Passenger would be created:

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

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 FrequentFlyerAccount theFrequentFlyerAccount;

public Passenger()

{

}

}

This is the code for FrequentFlyerAccount:

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

public class FrequentFlyerAccount

{

private int AccountID; private int NumberOfMiles;

471

Chapter 14: Java Code Generation and Reverse Engineering

private string DateEstablished;

public FrequentFlyerAccount()

{

}

/**

@roseuid 394590F100A2 */

public boolean AddMiles(int Miles)

{

}

/**

@roseuid 394590FE0209 */

public boolean UseMiles(int Miles)

{

}

/**

@roseuid 394591060124 */

public int NewAccount()

{

}

/**

@roseuid 39459113021D */

public boolean DeleteAccount(int AccountID)

{

}

}

As you can see, Rose will generate an attribute for the relationship at only one end of the association. Specifically, it will generate an attribute in the client class, but not in the supplier class.

The code generated in the supplier class is the same as discussed in the previous section about bidirectional associations. With a bidirectional association, each class is given a new attribute, and the code discussed in the previous section is included in both classes. With a unidirectional association, the code is included only in the client class.

Again, note that the multiplicity here is one to one. Next let's take a look at how code is affected when the multiplicity settings are changed.

Associations with a Multiplicity of One to Many

In a one−to−one relationship, Rose can simply create the appropriate attributes to support the association. With a one−to−many relationship, however, one class must contain a set of the other class. Let's look at an example.

472

Chapter 14: Java Code Generation and Reverse Engineering

In this case, we have a one−to−many relationship. Each flight has many tickets, but each ticket is for only one flight. The code generated for this relationship is shown below in the code for Flight:

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

public class Flight

{

private int FlightNumber; public Ticket theTicket[];

public Flight()

{

}

/**

@roseuid 39446ECD011F */

public boolean AddPassenger(int PassengerID)

{

}

/**

@roseuid 39446ED40101 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 39446EDB03BE */

public boolean CancelFlight()

{

}

}

Following is the code for Ticket:

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

public class Ticket

{

private int SeatNumber; private long PurchasePrice; public Flight theFlight;

public Ticket()

{

}

/**

@roseuid 3945946F0378 */

public int RefundTicket()

{

}

}

473

Chapter 14: Java Code Generation and Reverse Engineering

In this example, Ticket simply contains an attribute of type Flight because the multiplicity states that there is one instance of Flight for each instance of Ticket. However, the multiplicity also states that there are many instances of Ticket for each instance of Flight. Therefore, an array of Ticket objects is created inside Flight.

If you don't want to use an array, you can change the code−generation properties to use a different container class. In either the Java tab of the standard association specification window or the Field tab of the Java association specification window, set the ContainerClass code−generation property to the name of the container class you'd rather use.

In the following example, we created a class called TicketList, which is a collection of Tickets. The following code is generated for the above association, but with the container class as type TicketList.

Code for Flight:

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

public class Flight

{

private int FlightNumber; public TicketList theTicket;

public Flight()

{

}

/**

@roseuid 39446ECD011F */

public boolean AddPassenger(int PassengerID)

{

}

/**

@roseuid 39446ED40101 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 39446EDB03BE */

public boolean CancelFlight()

{

}

}

Associations with a Multiplicity of Many to Many

The code generated here is similar to that created for a one−to−many relationship. With a many−to−many relationship, however, Rose will generate arrays on both ends of the relationship. Let's look at the code generated for the following relationship.

474

Chapter 14: Java Code Generation and Reverse Engineering

This is the code for Flight:

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

public class Flight

{

private int FlightNumber;

public Passenger thePassenger[];

public Flight()

{

}

/**

@roseuid 39446ECD011F */

public boolean AddPassenger(int PassengerID)

{

}

/**

@roseuid 39446ED40101 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 39446EDB03BE */

public boolean CancelFlight()

{

}

}

This is the code for Passenger:

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

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 Flight theFlight[];

public Passenger()

475