Transforming UML to Java
The transformation function is a key feature of Rational Software Architect. You can transform the design from UML to C++, CORBA, EJB code, JACL, Java code, and so on, and begin your implementation phase with a head start. In this section, you'll transform the UML design into Java code where you can fill in the programming details and run the application.
- In the Model Explorer view, right-click Phone Book UML Model and select Transforms > UML to Java.
- Select Create new Target Container...
- In the New Java Project panel, enter
PhoneBookJavaProjectas the Java project name, and select Create separate source and output folder. Select Finish.
- Select Run to start the transformation.
- In the Model Explorer view, a Java project PhoneBookJavaProject is created automatically
with three Java files underneath.
Figure 17. Generated Java project
Let's take a look at the PhoneBookController.java, shown below. The generated class has two attributes, phonebookmodel and phonebookview, which are generated as a result of the directed association and association relationships, respectively. The two operations (userHasInput and start) that you added earlier when drawing the class diagram are also generated.
You can modify the files to implement the application based on the design infrastructure. A sample implementation is in the Downloads section. It implements a line command interface and stores the phone entries into a local file.
The design just lays out the infrastructure, which can lead to many different implementations. For example, you can create a GUI interface instead of a line command interface, store the data with EJB components instead of to a file, or use the Observer pattern to implement the notification mechanism. The provided sample implementation is just one of many ways to implement the design.
Now copy and paste the sample implementation.
- Select File > Save All to save everything.
- Select PhoneBookController, then select Run > Run As ... > Java Application to run the phone book implementation as a Java application.
- Go to the Console view, as shown in Figure 18, and interact with the application. Validate that you can perform the two
use cases, Add an entry and Search for a phone number, that were discussed earlier.
Remember, the purpose of use cases is to define the behavior of a system and capture the
requirements. It is important that the implementation fulfills the requirements, and is working as
Figure 18. Running the sample phone book application