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 this lesson you learn to use JDeveloper 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:
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.
This task 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.
In the Create Project dialog, name the project OrderEntry and
click OK.
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.
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.
4.
In the Create Java Class Diagram dialog, change the Name to OEClassDiagram
and the package name to orderEntry.
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.
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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):
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;
9 .
Right-click the OrderEntry project in the Applications Navigator
and choose Make from the context menu. The project should
compile successfully.
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.
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.
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.
3.
Associate OrderEntrySystem to Order. Click the Directed 1 to 1 Association
icon , then click
first on OrderEntrySystem and then on Order.
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.
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.
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.
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.
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.
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.
9.
Attempt to compile the project by right-clicking the OrderEntry
project in the Application Navigator and choosing Make from
the context menu.
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.
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.
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.
12.
Attempt to compile the project again as you did
in step 9 above. It should compile successfully now.
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.
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.)
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.
4.
Click the Directed Strong Aggregation
icon , click on Order then click on OrderState to model
a strong association.
5.
Double-click on the association. In the Association Properties dialog,
select 1 from the Multiplicity drop-down list. Click
OK.
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.
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.
3.
Click the Save icon
on the toolbar, or chooseFile | Save to save
these changes to OrderItem.java.
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.
5.
Model a strongly aggregated association between Order and OrderItem.
6.
Examine the changes that appear in the Java source code of Order.java.
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.
8.
Enter the Name of the class as Report1. Enter the
Package name as monthEnd.
Deselect the Generate default constructor check box. Click OK.
The new Report1 class is opened in the Code Editor.
9.
In a similar manner, create a Report2 class in the monthEnd
package.
10.
Click the OEClassDiagram tab in the diagrammer. From the Application
Navigator, drag the monthEnd package to the class diagram.
11.
To view the contents of the package, right-click the monthEnd
package in the diagram and choose Drill Down from the context
menu.
The class diagram for the monthEnd package opens in the diagrammer.
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].
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.
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.
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.
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
at the bottom right of the editor to be able to view more of the diagram
without scrolling.
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.
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