Legal | Privacy
Modeling Java Classes

Modeling Java Classes

This OBE tutorial describes how to use JDeveloper to create a diagram that captures the essential information requirements of a system.

Topics

The tutorial discusses the following:


45 minutes

Move your mouse over this icon to show all screenshots. You can also move your mouse over each individual icon to see only the screenshot associated with it.

Overview

Modeling enables you to capture information about the structure, properties, and behavior of a system. The class diagram visualizes the interfaces and classes that comprise a system and the associations, implementations and generalizations between them. UML notation is used to represent the elements on the diagram.

In the following tutorial, you learn to create a diagram that captures the basic data requirements of a system in the form of modeled Java classes. You also learn how to extend the diagram by adding an existing class. Because JDeveloper automatically synchronizes the elements in the diagram with their underlying Java code, you can use modeling to visually design a system.

The diagram that you create will look similar to this:

Move your mouse over this icon to see the image

Back to Topic List

You have been asked to design a Java application to manage part of an order entry system, and to present your design as a class diagram of the required Java classes. You also need to incorporate existing Java classes into your design.

Prerequisites

Back to Topic List

In order for this lesson to work successfully, you will need to have performed the following:

Install Oracle JDeveloper 10g.

Creating a Class Diagram

Back to Topic List

This topic takes you through the steps required to create a Java diagram from within JDeveloper. First, you create a workspace and project for your work and then you create a Java diagram to contain diagram elements that you will create.

1.

Create a new empty project in a new workspace:

In the Applications Navigator, right-click Applications and choose New from the context menu.

Select General from the Categories list, select Workspace from the Items list, and click OK.


In the Create Workspace dialog, name the workspace ClassModeling. Be sure that Add a New Empty Project is checked, and click OK.

Move your mouse over this icon to see the image

In the Create Project dialog, name the project OrderEntry and click OK.

Move your mouse over this icon to see the image

 

2.

You can optionally choose the colors and fonts for the diagram. Choose Tools | Preferences in the main menu to open the Preferences dialog, then open the Diagrams | Diagram | Java Class | Java Class tree control. You can then select the default font and color preferences for the diagram elements. When finished, click OK.

Move your mouse over this icon to see the image

The example shows changing the representation of Java classes to a light purple color.

 

3.

Select OrderEntry in the Navigator and create the class diagram by choosing File | New from the menu.

In the New Gallery, select General | Diagrams in the Categories pane, and Java Class Diagram in the Items pane. Click OK.

Move your mouse over this icon to see the image

 

4.

In the Create Java Class Diagram dialog, change the Name to OEClassDiagram and the package name to orderEntry.

Move your mouse over this icon to see the image

 

5.

You can choose to display the Component Palette as just icons, or as icons with a description. Toggle between these by selecting either Icon View or List View from the Component Palette context menu.

Move your mouse over this icon to see the image

 

6.

The Structure pane shows information about the contents of the class diagram. This can be useful if, for example, you want to see quickly how many classes there are in a diagram and what their names are.

Once you have elements on the diagram, you can also use the Structure Pane to center the diagram on a particular element. Right-click an element on the Structure Pane for a diagram and choose Find this Element on Diagram from the context menu.

Move your mouse over this icon to see the image

 

Modeling Interfaces and Classes

Back to Topic List

Now you begin to build the diagram by using the Component Palette to create new Java interfaces and classes on the diagram. To model these elements, perform the following steps:

1.

Select Java from the drop-down list on the Component Palette.

Move your mouse over this icon to see the image

 

2.

Model an interface on the class diagram. Click the Java Interface icon on the Component Palette to select it, then click on the upper left portion of the class diagram.

Move your mouse over this icon to see the image

Notice that when you create a diagram element it is displayed at the default size. To change the size, click on the object then drag one of the handles. To move it to a new position, click on the selected element and drag it to the new position.

 

3.

Type OrderObservable as the name for the interface, and press Enter.

Move your mouse over this icon to see the image

Notice that when you create the interface on the diagram, a .java file is automatically created. Because the diagram element and the .java file are different views of the same object, they remain synchronized as you work.

Move your mouse over this icon to see the image

When you work with class diagrams, you may occasionally see an ellipse (...) on a diagram element such as a class. This means that there is additional information that is not displayed because of the size of the element. To view it, resize the element.

 

4.

Now create an operation for this interface. Move the cursor to the operations compartment of the interface (just below the second horizontal line) and click. In the highlighted area type observeOrder and press Enter.

Move your mouse over this icon to see the image

 

5.

Model a Java class on the diagram by clicking on the Java Class icon , then click on the class diagram below the OrderObservable interface.

Enter Order as the name of the class. Add attributes orderRef and customerContact in the attributes compartment (just below the first line), and the operations doInvoice and doShip in the operations compartment (just below the second line).

Move your mouse over this icon to see the image

 

6.

Model the Java class MyOrder in the same way as you modeled the previous class. Put the MyOrder class to the right of the Order class, leaving quite a bit of space between the two classes.

Move your mouse over this icon to see the image

Another way of adding attributes and operations to a modeled Java class is to double-click on the class, and enter attributes in the Code Editor.

Double-click the MyOrder class in the class diagram. Click the Class tab at the bottom of the Code Editor, then select the Methods tab at the top.

Move your mouse over this icon to see the image

Click the Add button and in the Method Settings dialog, enter the method overrideMethod. Select public from the Scope drop-down list and click OK.

Move your mouse over this icon to see the image

If you click back into the class diagram, you will see that the MyOrder class in the diagram has been synchronized with the change you made in the Code Editor. It now shows the overrideMethod operation.

 

7.

In a similar fashion, add the following interfaces and classes to the diagram in the orderEntry package, positioning them as in the illustration below (you may optionally add the attributes and operations shown):

Type Name Attributes Operations
Interface OrderListener   + orderChanged(): int
Class OrderEntrySystem

 

 
Class OrderState   + getNextState(): OrderState
getStateLocalizedName(Locale local)
- fetchInternalStateDetails(): Object
Interface OrderFactory   + OrderEntry(int type): Order
Class BusinessOrderFactory    
Class HomeOrderFactory    

Move your mouse over this icon to see the image

 

8.

Double-click the OrderState class to open it in the editor. Click the Source tab. Immediately after the first line, add the following code if it is not already present:

import java.util.Locale;

Move your mouse over this icon to see the image

 

9 .

Right-click the OrderEntry project in the Applications Navigator and choose Make from the context menu. The project should compile successfully.

Move your mouse over this icon to see the image

 

Modeling Relationships and Associations

Back to Topic List

Now that you have a basic model showing the interfaces and classes that will comprise the order entry system, you can add elements that define how the interfaces and classes relate to one another. To model relationships and associations, perform the following steps:

1.

Model an Implements relationship between Order and OrderObservable. In the Component Palette, click the Implements icon , then in the diagram click first on the Order class, then on the OrderObservable interface.

Move your mouse over this icon to see the image

After adding this interface, you can see that the code in the Order.java file has changed. Double-click Order.java in the Applications Navigator to open the Code Editor and view the change.

Move your mouse over this icon to see the image

 

2.

Model an Extends relationship between Order and MyOrder. Click on the Extends icon , then click first on MyOrder and then on Order. You can make a bent line by clicking on desired bend points as you draw the line between the two classes.

Move your mouse over this icon to see the image

 

3.

Associate OrderEntrySystem to Order. Click the Directed 1 to 1 Association icon , then click first on OrderEntrySystem and then on Order.

Move your mouse over this icon to see the image

 

4.

Model a weakly aggregated association between Order and OrderListener. Although there is no icon for a weakly aggregated association, JDeveloper enables you to use the predefined associations and then modify the association properties to change aggregation or multiplicity as needed.

Click the Directed 1 to 1 Association icon , then click first on Order then on OrderListener.

Move your mouse over this icon to see the image

Double-click the association on the diagram to display the Association Properties dialog. Select Aggregate (Weak) from the Aggregation drop-down list, then click OK. This defines the 'from' end of the association as weakly aggregated.

Move your mouse over this icon to see the image

Note: Another way of displaying the Association Properties dialog for a diagram element is by right-clicking the association in the diagram and selecting Properties from the context menu.

 

5.

Model an Implements relationship between HomeOrderFactory and OrderFactory, and between BusinessOrderFactory and OrderFactory.

Move your mouse over this icon to see the image

 

6.

Model a Directed 1 to * Association between OrderEntrySystem and OrderFactory. To make a bent line as shown, click first in OrderEntrySystem, then click the bend point, then click in OrderFactory.

Move your mouse over this icon to see the image

 

7.

Model a dependency between OrderFactory and MyOrder using the Dependency icon . You can optionally overtype the default label for the dependency to change it to a more meaningful name.

Move your mouse over this icon to see the image

 

8.

You can add notes to the diagram itself or to any of its elements. Click the Note icon and then click the diagram to create a note stating that OrderFactory creates MyOrder. Attach the note to the dependency by clicking the Attach icon , then clicking on the note and then on the dependency.

Move your mouse over this icon to see the image

 

9.

Attempt to compile the project by right-clicking the OrderEntry project in the Application Navigator and choosing Make from the context menu.

Move your mouse over this icon to see the image

The log window displays compilation errors such as:

Error(3,8): class orderEntry.BusinessOrderFactory should be declared abstract; it does not define method OrderEntry(int) in interface orderEntry.OrderFactory

You receive these errors because classes are not defining all the methods of the interfaces that they are implementing.

 

10.

To correct the errors, click into the OrderEntry method line of the OrderFactory interface, then click it again so that the method is highlighted. Press [Ctrl-C] to copy the method to the clipboard. Click into the method line of the HomeOrderFactory class, then click it again to add a default method. Press [Ctrl-V] to paste the OrderEntry method so that it overtypes the default method, then press [Enter].

In a similar manner, paste the OrderEntry method into the BusinessOrderFactory class.

Move your mouse over this icon to see the image

 

11 .

To correct the problem with the Order class not defining the methods of the OrderObservable interface, there is another way instead of copying and pasting methods that you can use.

Select the Order class in the class diagram, then from the menu choose Tools | Implement Interface.

Move your mouse over this icon to see the image

In the Available Interfaces area of the Implement Interface dialog, expand orderEntry, then select OrderObservable. Click OK. The observeOrder method is added to the Order class.

Move your mouse over this icon to see the image

 

12. Attempt to compile the project again as you did in step 9 above. It should compile successfully now.

Modeling Inner Classes

Back to Topic List

You can also model inner classes on a JDeveloper class diagram. To model inner classes, perform the following steps:

1.

Right-click OrderState and choose View As | Expanded.

Move your mouse over this icon to see the image

 

2.

Model the Java classes Cleared, Packed, Shipped and Invoiced inside the expanded OrderState class. This creates the classes as inner classes of OrderState. (This is not a typical use of inner classes, but it illustrates how to create them in a class diagram.)

Move your mouse over this icon to see the image

 

3.

Open OrderState.java in the Code Editor by double-clicking the file name in the Application Navigator. You can see that the code for the four inner classes has been automatically added.

Move your mouse over this icon to see the image

 

4.

Click the Directed Strong Aggregation icon , click on Order then click on OrderState to model a strong association.

Move your mouse over this icon to see the image

 

5.

Double-click on the association. In the Association Properties dialog, select 1 from the Multiplicity drop-down list. Click OK.

Move your mouse over this icon to see the image

 

Adding Existing Classes and Packages

Back to Topic List

This task shows you how to extend the diagram by adding an existing class and an existing package to the project and then adding them to the diagram. The ability to model existing classes makes it possible to create diagrams of existing systems or to merge existing classes into new system designs.

To extend the class model, perform the following steps:

1.

Right-click the project node in the Navigator pane, choose New, then choose Java Class from the General category of the New Gallery. Click OK.

Move your mouse over this icon to see the image

 

2.

Enter the Name of the class as OrderItem. Enter the Package name as orderEntry.

Deselect the Generate default constructor check box. Click OK.

The new OrderItem class is opened in the Code Editor.

Move your mouse over this icon to see the image

 

3.

Click the Save icon on the toolbar, or choose File | Save to save these changes to OrderItem.java.

Move your mouse over this icon to see the image

 

4.

Click the diagram name in the tab at the top of the editor window. Drag the OrderItem class from the Application Navigator and drop it on the diagram.

Move your mouse over this icon to see the image

 

5.

Model a strongly aggregated association between Order and OrderItem.

Move your mouse over this icon to see the image

 

6.

Examine the changes that appear in the Java source code of Order.java.

Move your mouse over this icon to see the image

 

7.

Right-click the project node in the Navigator pane, choose New, then choose Java Class from the General category of the New Gallery. Click OK.

Move your mouse over this icon to see the image

 

8.

Enter the Name of the class as Report1. Enter the Package name as monthEnd.

Deselect the Generate default constructor check box. Click OK.

Move your mouse over this icon to see the image

The new Report1 class is opened in the Code Editor.

 

9.

In a similar manner, create a Report2 class in the monthEnd package.

Move your mouse over this icon to see the image

 

10.

Click the OEClassDiagram tab in the diagrammer. From the Application Navigator, drag the monthEnd package to the class diagram.

Move your mouse over this icon to see the image

 

11.

To view the contents of the package, right-click the monthEnd package in the diagram and choose Drill Down from the context menu.

Move your mouse over this icon to see the image

The class diagram for the monthEnd package opens in the diagrammer.

Move your mouse over this icon to see the image

 

12.

You can add existing classes that relate to the classes already on the diagram. To illustrate this, click the OEClassDiagram tab in the diagrammer. Select the MyOrder class in the diagrammer and press [Delete].

Move your mouse over this icon to see the image

The MyOrder class is deleted from the diagram, but the MyOrder.java file remains in the Application Navigator.

 

13.

The Order class is related to the MyOrder class by inheritance. Right-click the Order class in the diagrammer and choose Show | Related Elements from the context menu.

Move your mouse over this icon to see the image

The MyOrder class is added to the diagram, and all other elements on the diagram that are related to Order are highlighted. You can move the MyOrder element to its previous position on the diagram.

Note: When classes are added to a diagram from the project's source path, they are automatically added to the current project if not already part of it.

Modifying the Diagram Layout

Back to Topic List

As class diagrams grow in size and complexity, they may become difficult to read and manage. This final task shows you how to modify the layout of class diagrams.

To change the layout of the diagram, perform the following steps:

1.

Right-click a blank spot in the OEClassDiagram and choose Lay Out Shapes | Hierarchical (Top to Bottom) from the context menu.

Move your mouse over this icon to see the image

This lays out the diagram elements in a top-down hierarchy based on inheritance structures and other connectors with a defined direction. Connectors between the nodes are laid out using the most direct route. Nodes on a diagram that are not connected to any other nodes, such as the monthEnd package in this example, are laid out in a grid layout.

 

2.

Click the Zoom Out tool Zoom Out tool at the bottom right of the editor to be able to view more of the diagram without scrolling.

Move your mouse over this icon to see the image

 

You may want to experiment with other types of Lay Out Shapes and apply them to the entire diagram as above, or to selected elements only.

Back to Topic List

This lesson showed you how to create a new Java class diagram where you can model Java classes and interfaces and the relationships between them. You accomplished the following tasks:

Created a Java class diagram

Used the Component Palette to create new Java interfaces and classes on the class diagram

Added attributes and operations directly on the class diagram
Observed that the Java code is automatically synchronized when you make changes in the diagram

Used the Component Palette to create relationships and associations between the interfaces and classes on the class diagram

Modified the properties of associations to change aggregation and multiplicity

Added a note and attached it to a dependency

Created inner classes by expanding the view of a class and using the Component Palette to create new Java classes within it

Added an existing class and a package to the class diagram

Modified the layout of the class diagram

Oracle Education Online Library eClass: Oracle9i JDeveloper: Class Modeling

OTN viewlet: UML Class Modeling

OTN: JDeveloper Products page

Move your mouse over this icon to hide all screenshots.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy