UML class diagram in NetBeans
You can perform round-trip engineering in SDE for NetBeans, to keep Java source code and class model in sync. In this tutorial, we will see how to create a class model in SDE for NetBeans, and eventually generating source code from model.
Create a Java project Express Courier in NetBeans. |
Right click on the project node in Projects window, and select Open SDE EE-NB from the popup menu.
3.
SDE-NB is started. In Diagram Navigator (of SDE), right click on Class Diagram and select New Class Diagram from the popup menu.
A new diagram is created. You asked to enter a package header on top of the diagram. Enter myapp and press Enter.
You are asked to provide the diagram name. Enter Domain Model and press Enter.
Click on the down arrow button near the shape selection Class in diagram toolbar, and select Interface.
Click on the diagram to create an interface class and name it as IMailDelivery.
Create operations in IMailDelivery. Right click on the class IMailDelivery and select Add > Operation from the popup menu.
9. Enter setState(state : char) : void to create a public operation stateState with parameter state and return void.
Press Enter to create another operation. Name it printShipmentInfo() : void. Click on diagram to confirm editing.
We need to create a class for local delivery which inherits IMailDelivery. Move the mouse pointer over interface IMailDelivery, press on the resource icon Realization -> Class and drag downwards to create a new class.
Name the class as LocalDelivery and press Enter to confirm.
Asthe LocalDelivery class is implementing the interface IMailDelivery, we need to implement the operations defined in IMailDelivery. Right click on class LocalDelivery and select Related Element > Realize all Interfaces from the popup menu.
You can see that operations setState and printShipmentInfo are both inherited.
It is time to add attributes to classes. Right click on class LocalDelivery and select Add > Attribute from the popup menu.
Enter state : char to name the attribute as state, and set the type as char.
Press Enter to proceed to the next attribute. Enter postage : double as attribute name and type.
Again, press Enter and create attribute shipmentNum : int.
19. We need to have two constants for representing states. Press Enter to continue creating attribute. Name the attribute STATE_INIT : char = 'a'. This means to create an attribute named STATE_INIT, which is in char type and have 'a' as default value. Press Enter to create another constant STATE_DELIVERED : char = 'b'. Up to now, the class should look like:
In order to declare both STATE_ attribute as constants, select STATE_INIT, press the Ctrl key and select STATE_DELIVERED to make a multiple selection. Right click on them and select Model Element Propertes > Scope > classifier from the popup menu. By doing so, both attributes will be static (in code level) and are underlined.
Again, right click on the two attributes and select Code Detail > final from the popup menu. Click on the diagram to confirm editing. The class should now become:
We need to create a new class Mail with association from class LocalDelivery. Move the mouse pointer to the class LocalDelivery. Press on the resource icon Aggregation -> Class.
Drag to the right of the class LocalDelivery and release the mouse button. Name the new class Mail. Follow the previous steps to create attributes in class Mail.
Class | Attributes | Operations |
Mail | fromPerson : String fromContact : String toPerson : String toContact : String mailType : int | - |
We need to describe the attribute mailType by listing the possible values. Select mailType.
Activate the documentation pane. If you cannot find it on the screen, or if you had closed that, open it by selecting View > SDE EE-NB View > Documentation from the main menu. In the documentation pane, enter the following:
Possible types: 1 - Flat 2 - Letter 3 - Postcard 4 - Parcel |
The documentation for classes, attributes, operations and parameters will become comments in source code to be generated.
Up to now, the diagram should look like:
Select File > Save SDE EE-NB Project from the main menu to save.
Now comes the code generation. Select the menu Update Code on Diagram Navigator.
Check the Projects window. You should see a list of generated file. You can open them to fill in the code body.
This is the end of the tutorial. Instead of closing NetBeans now, you may try something more by editing the code like to add, rename or delete class, attributes and operations, and click the Update UML Model button on toolbar, and observe the changes that will make in the class model.
NB : DENIEL say's ( lets do it )
Tidak ada komentar:
Posting Komentar