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

Chapter 14: Java Code Generation and Reverse Engineering

public boolean AddPassenger(int PassengerID)

{

}

}

The visibility options affect the generated attribute. In the above example, and as the default, attributes are given private visibility. If you set an attribute to Protected or Public, the following code is an example of what you will generate:

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

/* Copyright Notice */

/**

Comments for Flight Class */

public class Flight

{

private transient int FlightNumber; public long DepartureDate; protected int GateNumber;

public Flight()

{

}

/**

@roseuid 394461CB01F3 */

public boolean AddPassenger(int PassengerID)

{

}

}

Operations

Rose generates each of the operations in the class. For each operation, the generated code includes the operation name, the parameters, the parameter data types, and the return type. Again, we'll examine the code generated for the Flight class:

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

public class Flight

{

private int FlightNumber;

public Flight()

{

466

Chapter 14: Java Code Generation and Reverse Engineering

}

/**

@roseuid 39446D1B0084 */

public boolean AddPassenger(int PassengerID)

{

}

/**

@roseuid 39446D230253 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 39446D2B02AE */

public boolean CancelFlight()

{

}

}

As you can see, the full operation signature is generated in the code. Any documentation you enter for the operation is also generated as a comment in the code.

Rose generates skeletal code for operations. That is, the operation signature is generated. Once you have generated the code, you insert the implementation code for each operation between the { and } delimiters of the operation. This protects the code during round−trip engineering.

You can enter exception information using the Java operation specification window, as shown here:

In the Throws field, select the appropriate Java exception class. When you generate code, the exception information will be generated as well:

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

public class Flight

467

Chapter 14: Java Code Generation and Reverse Engineering

{

private int FlightNumber;

public Flight()

{

}

/**

@roseuid 39446ECD011F */

public boolean AddPassenger(int PassengerID) throws java.lang.Exception

{

}

/**

@roseuid 39446ED40101 */

public boolean RemovePassenger(int PassengerID)

{

}

/**

@roseuid 39446EDB03BE */

public boolean CancelFlight()

{

}

}

As with other model elements, you can control the code generated for an operation by modifying its code−generation properties.

Bidirectional Associations

To support bidirectional associations, Rose will generate attributes in the code. Each of the classes in the relationship will contain an attribute to support the association. By default, Rose names the roles by appending "the" to the class name. Let's look at the code generated for the following relationship:

The following 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;

468

Chapter 14: Java Code Generation and Reverse Engineering

private long Zip; private string Phone;

public FrequentFlyerAccount theFrequentFlyerAccount;

public Passenger()

{

}

}

The following is the code for FrequentFlyerAccount:

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

public class FrequentFlyerAccount

{

private int AccountID; private int NumberOfMiles;

private string DateEstablished; public Passenger thePassenger;

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 automatically generate attributes on both sides of the bidirectional association relationship. With the FrequentFlyerAccount attribute, Passenger can access the public attributes and operations of FrequentFlyerAccount. With the Passenger attribute, FrequentFlyerAccount can access the public attributes and operations of Passenger. If you want to use different names for the attributes instead of thePassenger and theFrequentFlyerAccount, you can set role names for the association. These names will be

469

Chapter 14: Java Code Generation and Reverse Engineering

used instead, as in the following code for Passenger. In this example, we set the role names to MyAccount and AccountHolder.

//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 myAccount;

public Passenger()

{

}

}

The following is the code for FrequentFlyerAccount:

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

public class FrequentFlyerAccount

{

private int AccountID; private int NumberOfMiles;

private string DateEstablished; public Passenger AccountHolder;

public FrequentFlyerAccount()

{

}

/**

@roseuid 394590F100A2 */

public boolean AddMiles(int Miles)

{

}

/**

@roseuid 394590FE0209 */

public boolean UseMiles(int Miles)

{

}

/**

@roseuid 394591060124 */

public int NewAccount()

{

}

/**

470