Step 2: Modeling Java Classes

Previous topic
Previous
Next topic
Next

This task takes you through the steps required to model Java classes, interfaces and associations on a class diagram.

To model Java classes:

  1. Select Java from the dropdown on the Component Palette.
  2. Model an interface on the class diagram. Click the Interface icon Interface icon  on the Component Palette to select it, then click on 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.

    When you work with UML diagrams, you may occasionally see an ellipse (...) on an diagram element, for example, 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 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 Java Class icon, then click on the class diagram. Enter Order as the name of the class, and 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 a realization between Order and OrderObservable. Click the Realization icon Realization icon  and click first on the Order class, then on the OrderObservable interface.
  7. Model the Java class MyOrder and the operation overrideMethod in the same way as you modeled the previous class.
    Another way of adding attributes and operations to a modeled Java class is to double-click on the class, and enter attributes on the Attributes tab and operations on the Operations tab. Try this method so that you become familiar with it, and while it is displayed have a look at the other properties that you can enter or change in this dialog.
  8. Model a generalization between Order and MyOrder. Click on the Generalization icon Generalization icon  and click first on MyOrder and then on Order.
  9. Right-click on the OrderEntry project in the Navigator and choose New Java Class. Enter the name of the Java class as OrderEntrySystem and the package as orderEntry. Make sure that Generate default constructor and Generate main method are not selected.

    When you click OK the Java class is displayed in the Navigator, and the text of the class is displayed in the Editor window. Edit the class in the Code Editor to add a field called currentOrder of type Order and a method called getCurrentOrder with a return type of Order.

  10. Now add this Java class to the class diagram. Click the OEClassDiagram button in the Document bar to display the diagram, then click on OrderEntrySystem in the Navigator and drag it onto the diagram.
  11. Associate OrderEntrySystem to Order. Click the Directed 1..1 Association icon Directed Association 1 to 1 icon  and click first on OrderEntrySystem and then on Order.
  12. Model a Java interface called OrderListener and give it the attribute orderChanged.
  13. Model a weakly aggregated association between Order and OrderListener. Click the Directed 1..1 Association icon Directed Association 1 to 1 icon  and click first on Order then on OrderListener. With the association highlighted press Enter to display the Properties dialog. Select the Association Ends tab then select Aggregate (Weak) from the Aggregation drop-down box, then click OK. This defines the 'from' end of the association as weakly aggregated.

    Another way of displaying the Properties dialog for a diagram element is by selecting Properties from the context menu.

  14. Model a Java interface called OrderFactory with an operation OrderEntry.
  15. Model the Java classes HomeOrderFactory and BusinessOrderFactory.
  16. Model a realization between HomeOrderFactory and OrderFactory, and between BusinessOrderFactory and OrderFactory.
  17. Model an association between OrderEntrySystem and OrderFactory.
  18. Model the Java classes Cleared, Packed, Shipped and Invoiced.
  19. Model the class OrderState, and make Cleared, Packed, Shipped and Invoiced inner classes of OrderState. To make Cleared an inner class of OrderState, double-click Cleared to display the Properties dialog. Click the Select button next to the Namespace field to display the Select Element dialog.

    In the Select Element dialog, expand the Model Elements node until you find OrderState which is the owning class, and select it.

    Notice that the namespace of Cleared now appears as orderEntry::OrderState, and that Cleared appears in the inner classes compartment of OrderState.

    Repeat this for Packed, Shipped and Invoiced.

  20. Click the Directed Strong Association icon Directed Strong Association, click on Order then click on OrderState to model a strong association. Click on the * at the OrderState end of the association on the diagram and change it to 1.
  21. Model a dependency between OrderFactory and MyOrder using the Dependency icon Dependency icon.
  22. Create a note stating that OrderFactory creates MyOrder, and attach it to the dependency. To attach the note click the Attach icon Attach icon, then click on the note and then on the dependency.

If you like, spend some more time working on the class diagram to add additional attributes and operations. For example, you could add the operation isCleared to the Java class Order. You can change the return type to boolean by editing the default of void on the class diagram.

Now that you have created the class diagram, you are ready to proceed to the next task, Step 3 - Generating Java Source Code from the Class Model, which shows you how to generate Java source code from the class diagram you have created.