Modeling Java Classes Tutorial
Xi Hua
Contents
- Overview
- Prerequisites
- Step 1 – Creating a Class Diagram
- Step 2 –Modeling Interfaces and Classes
- Step 3 – Modeling Relationships and Associations
- Step 4 – Modeling Inner Classes
- Step 5 – Adding Existing Classes and Packages
- Step 6 – Modifying the Diagram Layout
- Summary
Overview
UML class diagram is a popular way of representing class structure, information and behavior in software design. This tutorial shows how to model java class diagram using JDeveloper. Such as creating new class diagram, extending the diagram by adding an existing class, etc...
The final diagram is showed below:
Final Diagram Layout
Prerequisites
Oracle JDeveloper10g platform must be installed in order to make the tutorial work properly.
Step 1 – Creating a Class Diagram
In this section, we first create a new project, and then create a new class diagram. Also do some customize work for our class diagram.
- Creating a new project in a new workspace
Start JDeveloper, right-click Applications in the Applications Navigator, select New.
Figure 1.1(New Gallery) opens, click General in Categories list, click Workspace in Items list, and click OK.
Figure 1. 1
Figure 1.2(Create Workspace) opens, name the workspace as ClassModeling. Check Add a New Empty Project, click OK.
Figure 1. 2
Figure 1.3(Create Project) opens, name the project as OrderEntry, and clickOK.
Figure 1. 3
- Customize diagram colors and fonts
Select Tools->Preferences, open the Preferences window, and select Diagrams -> Diagram -> Java Class -> Java Class (figure 1.4). You can customize the fonts and colors of the class diagram. Click OK when finished.
Figure 1. 4
- Creating a new class diagram
Right-click on OrderEntry in Applications Navigator, select New, Figure 1.5 (New Gallery) opens, select General->Diagrams in the Categories list, select Java Class Diagram from the Items list. Click OK.
Figure 1. 5
Figure 1.6 (Create Java Class Diagram) opens, name the diagram as OEClassDiagram and the package name as orderEntry. Click OK.
Figure 1. 6
- Customize component palette view
You can switch Component Palette view between icon view (icon only) and list view (icon with description) by right click on the Component Palette and select icon view orlist view. Figure 1.7.
Figure 1. 7
- The use of structure pane
The structure pane is a useful window during modeling, it shows the information about the diagram, such as how many classes are in the diagram, and you can locate specific class by right-click on the element and select Find this Element on Diagram. Figure 1.8.
Figure 1. 8
Step 2 – Modeling Interfaces and Classes
In this section, we start to construct the class diagram with interfaces and classes with the help of the component palette.
- Create a java interface
Click Java from the drop-down list in Component Palette. Figure 2.1.
Figure 2. 1
To model a java interface, select Java Interface on the Component Palette and click on the class diagram, we put it on the upper left side of the class diagram. Figure 2.2. You can change the size or change the position of the interface element by click on the element and drag its handle or drag it to another position.
Figure 2. 2
Name the interface as OrderObservable, and press Enter. Figure 2.3.
Figure 2. 3
You can see a .java file is created in the Application Navigator as you create the interface. Figure 2.4. Sometimes you can see an ellipse on a diagram, it means some information can not be shown, resize the element to see those information.
Figure 2. 4
To create an operation for this interface, click on the operation line on the interface (the third line), we name the operation as observeOrder, press Enter. Figure 2.5.
Figure 2. 5
- Create a java class
Click on the Java Class icon and create a java class below the interface we build just now. Figure 2.6.
Figure 2. 6
Name the class as Order, set orderRef and customerContact attributes in the second line (attribute compartment), set doInovice and doShip in the operation compartment (the third line). Figure 2.7.
Figure 2. 7
- Create another java class
Create another java class called MyOrder on the right side in the same way with the previous one. Figure 2.8.
Figure 2. 8
Here we can set the attributes and operations of the class in another way: double-click on the class, set attributes in the Code Editor. Select Class tab at the bottom of the Code Editor, and Method tab at the top. Figure 2.9.
Figure 2. 9
Select Add button and set the method name as overrideMethod, and set the class public in the Scope drop-down list, click OK. Figure 2.10.
Figure 2. 10
- Create the rest classes and interfaces
Create the following elements in the same way, the result diagram is shown in Figure 2.11.
Type / Name / Attributes / OperationsInterface / OrderListener / + orderChanged(): int
Class / OrderEntrySystem
Class / OrderState / + getNextState(): OrderState
getStateLocalizedName(Locale local)
- fetchInternalStateDetails(): Object
Interface / OrderFactory / + OrderEntry(int type): Order
Class / BusinessOrderFactory
Class / HomeOrderFactory
Figure 2. 11
- Compile the project
Double-click on OrderState, open Code Editor window, select Source tag on the bottom, add import java.util.Locale; to the second line of the code (right after package statement). Figure 2.12.
Figure 2. 12
Right-click on the project OrderEntry, select Make, the project should compile successfully. Figure 2.13.
Figure 2. 13
Step 3 – Modeling Relationships and Associations
Now we need to model the relationships and associations between the classes and interfaces we just built, it’s also very easy in JDeveloper.
- Model Implementation
Now we want to create an implementation between Order and OrderObservable. Click Implements icon, then click on the Order class first, OrderObservable class second. Figure 3.1.
Figure 3. 1
After modeling the implementation, the .java file will also modify it self according to this change. Figure 3.2.
Figure 3. 2
- Model Extension
To model an extension between Order and MyOrder, click on the Extends icon in the Component Palette, click on MyOrder first and then Order. You can bend the line just by clicking on the bend point you want. Figure 3.3.
Figure 3. 3
- Model Association
Associate OrderEntrySystem to Order, select Directed 1 to 1 Association icon, click OrderEntrySystem and then click on Order. Figure 3.4.
Figure 3. 4
- Model Weakly Aggregated Association
Now we want to model a weakly aggregated association between Order and OrderListener. There is no icon for such association but we can use the defined association and then modify some of its properties to meet our requirement.
Select Directed 1 to 1 Association icon, then first click on Order and then OrderListener. Figure 3.5.
Figure 3. 5
Double-click on the association to open Association Properties window. Select Aggregate(Weak) from the Aggregation drop-down list, click OK. Figure 3.6.
Figure 3. 6
- Model Other Implementations and Associations
Create an Implements relationship between HomeOrderFactory and OrderFactory, and between BusinessOrderFactory and OrderFactory. Figure 3.7.
Figure 3. 7
Create a Directed 1 to * Association between OrderEntrySustem and OrderFactory. You can also make the line bend like previous one. Figure 3.8.
Figure 3. 8
- Model Dependency
To model a dependency between OrderFactory and MuOrder, click Dependency icon and click OrderFactory first, MyOrder second, you can change the default dependency name to whatever you want. Figure 3.9.
Figure 3. 9
- Add Notes to the Element
You can add notes to any element you create, classes, relationships or associations. To add a note, click Note icon and then click on the diagram, input note content as OrderFactory creates MyOrder, then click Attach icon, then first click the note and second the dependency created just now. Figure 3.10.
Figure 3. 10
- Compile the Project
Now compile the project, first right-click on the project OrderEntry then select Make.
The compiler reports an error like:
Error(3,8): class orderEntry.BusinessOrderFactory should be declared abstract;
it does not define method OrderEntry(int) in interface orderEntry.OrderFactory
To correct these errors, we need to define all the methods in the classes implement from the interfaces. There are two ways to do so, first, copy the method in the interface, and paste to the classes implement the interface, Figure 3.11.
Figure 3. 11
Second, use JDeveloper defined tool, click on the classOrder, then select Tool->ImplementInterfacefrom the menu, in the new opened window, expand orderEntry, select OrderObservable, click OK. JDeveloper will automatically add the OrderObservable methods to Order. Figure 3.12.
Figure 3. 12
Now compile the project again, it should pass now.
Step 4 – Modeling Inner Classes
- Create Inner Classes
Right-click class OrderState in the class diagram, select View As->Expanded. Figure 4.1.
Figure 4. 1
Model inner Java classes Cleared, Packed, Shipped and Invoiced inside the expended OrderState class. Figure 4.2.
Figure 4. 2
Double-click OrderState.java in the Application Navigator to open it in the Code Editor. You can see the code for the inner class is added to the file automatically. Figure 4.3.
Figure 4. 3
- Create Strong Aggregation
Select Directed Strong Aggregation icon, then first click on Order and then OrderState. Figure 4.4.
Figure 4. 4
Double-click the association. The Association Properties window opens, select 1 in the Multiplicity drop-down list. Select OK. Figure 4.5.
Figure 4. 5
Step 5 – Adding Existing Classes and Packages
You can add existing classes or packages to the diagram, it’s helpful in reusing your system designs.
- Adding Existing Class to the Diagram
Create a new java class by right-clicking the project name in the Applications Navigator, select New, and then select Java Class in the General category. Figure 5.1.
Figure 5. 1
Name the class as OrderItem, package is orderEntry. Deselect the Generate Default Constructor check box, click OK. Figure 5.2.
Figure 5. 2
Save the changes by clicking the Save icon or select File->Save. Figure 5.3.
Figure 5. 3
Now we back to the OEClassDiagram, Drag the OrderItem.java to the diagram area from the Applications Navigator. The OrderItem element is added to the diagram. Figure 5.4.
Figure 5. 4
Create a strongly aggregated association between Order and OrderItem. Figure 5.5.
Figure 5. 5
Now double-click the Order.java file, you can see the code changes automatically. Figure 5.6.
Figure 5. 6
- Adding Existing Package to the Diagram
Create a new java class called Report1, the package name is monthEnd, deselect the General Default Constructor,and clickOK. Figure 5.7.
Figure 5. 7
Create another class called Report2 in the monthEnd package in the same way. Figure 5.8.
Figure 5. 8
Click OEClassDiagram tab to go back to the diagram area, drag the monthEnd package from the Applications Navigator to the diagram area. Figure 5.9.
Figure 5. 9
You can view the content of the package by right-clicking the package and select Drill Down. Figure 5.10.
Figure 5. 10
A new diagrammer opened to show the classes in the package. Figure 5.11.
Figure 5. 11
- Adding Related Classes to the Diagram
You can also add an existing class which is already related to the classes in the diagram. For instance, we delete the MyOrder element from the diagram by click on the element and then press [Delete]. Figure 5.12. Notice that MyOrder is removed from the diagrammer, but MyOrder.java still exists.
Figure 5. 12
Because MyOrder is inherited from Order, so right-click on Order and select Show->Related Elements. Figure 5.13. MyOrder is added to the diagram now, with all the fields and methods related to Order highlighted. You can move MyOrder back to where it was.
Figure 5. 13
Step 6 – Modifying the Diagram Layout
We have created the entire diagram but a little bit mess to read. We can use the JDeveloper tool to rearrange the layout with just few clicks.
- Rearrange the diagram layout
Right-click on any blank place in the diagram, select Lay Out Shapes->Hierarchical (Top to Bottom). Figure 6.1.
Figure 6. 1
- Zoom in and zoom out
Click the Zoom Out or Zoom In icon at the bottom right of the diagrammer to zoom out/in. Figure 6.2.
Figure 6. 2
Summary
In this tutorial, we learned how to model a class diagram with JDeveloper. Class diagram is import and useful in software design, and JDeveloper provides an easy and effective way to model it. What we have done is:
- Create a diagram and customize its properties like color;
- Create Java classes and interfaces with JDeveloper;
- Model the relationships and associations between the elements;
- Create inner class;
- Add existing classes and packages to the diagram;
- Modify the layout of the Diagram.
That’s all for the tutorial, it’s really a good way to model the java class diagram with JDeveloper!
1