Chapter 3 - Designing Object Model with UML Class Diagram
Chapter 3 - Designing Object Model with UML Class Diagram
DB Visual ARCHITECT (DB-VA) provides you a visual modeling environment for the object model of an application. This chapter shows you how to depict the object models by using a UML Class Diagram.
In this chapter:
- Introduction
- Creating Object Model with Class Diagram
- Defining Package for Classes
- Specifying Stereotypes
- Specifying Inheritance Strategy
- Specifying Collection Type
- Defining ORM Qualifier
- Customizing SQL
Introduction
An object is a self-contained entity with well-defined characteristics and behaviors while the characteristics and behaviors are represented by attributes and operations respectively. A class is a generic definition for a set of similar objects. Hence, an object is an instance of a class. An object model provides a static conceptual view of an application. It shows the key components (objects) and their relationships (associations) within the application system.
DB-VA supports visual modeling for object models, not only by creating a new object model, but also by transforming from a data model. As DB-VA automates object-relational mapping, DB-VA supports the generation of database, code and persistence layer for Java model API and .NET model API, which in turn streamlines the model-code-deploy software development process.
Creating Object Model with Class Diagram
A class diagram can be used to describe the objects and classes inside a system and the relationships between them; and thus, a class diagram is also known as an object model. The class diagram identifies the high-level entities of the system. DB-VA comes with a complete UML 2.0 class diagram for object modeling.
The following section describes how you can depict an object model using the class diagram. DB-VA also supports the generation of persistent code based on the object model, which will be briefly described in the DB-VA Programmer's Guide.
DB-VA provides you with two ways to create a Class Diagram:
- Drawing a Class Diagram
- Synchronizing from Data Model to Object Model
Drawing a Class Diagram
You can create a new class diagram in one of the three ways:
- On the menu, click File > New Diagram > UML Diagrams > Class Diagram.
- On the Diagram Navigator, right-click Class Diagram > Create Class Diagram.
- On the toolbar, click the New Class Diagram icon.
A new class diagram pane is displayed.
Adding Class
- On the diagram toolbar, click the Class shape icon.
- Click a location in the diagram pane.
DB-VA places an icon representing the class element on the diagram.
- Type a name for the Class element.
- You can edit the name by double-clicking the name or by pressing the F2 button.
Adding ORM-Persistable Class
ORM-Persistable class is capable of manipulating the persistent data with the relational database. In supporting the generation of persistent code, ORM-Persistable classes should be used in object modeling. Classes added to the class diagram can be stereotyped as ORM-Persistable to manipulate the database. For information on how to specify the stereotype to a class, refer to Specifying Stereotypes section.
DB-VA provides an alternative way to add the ORM-Persistable class easily.
- On the diagram toolbar, click the Class shape icon and hold for a while, a pop-up menu shows.
- Select ORM-Persistable Class from the pop-up menu.
- Click a location in the diagram pane.
DB-VA places a class shape icon which is marked with <<ORM Persistable>> on the diagram.
- Type a name for the ORM-Persistable Class.
- You can edit the name by double-clicking the name or by pressing the F2 button.
Modifying Class Specification
A class specification displays the class properties and relationships.
You can display the Class Specification in one of the two ways:
- Click on a class, click the Open Specification resource located at the top-right corner of the class.
- Right-click the class, click Open Specification...from the pop-up menu.
Class Specification dialog box is displayed, you can modify the class properties and relationships.
Adding Attribute
An attribute is a property of a class which has a name, a value and also has a type to describe the characteristic of an object.
- You can add attribute to the class in one of the three ways:
- Right-click on a class, select Add > Attribute.
A new attribute is added, type the attribute name and type in the form of "attribute_name: type". You can also edit the attribute name by double-clicking the attribute name or by pressing the F2 button.
- Click on a class, press the keyboard shortcut - Alt + Shift + A.
- Using Class specification dialog:
- Right-click the class element, click Open Specification....
- Click the Attributes Tab, then click Add.
Attribute Specification dialog box is displayed, you can modify the attribute name and properties, such as type.
Modifying Attribute Specification
An attribute specification displays the attribute properties, such as name, type, and initial value etc.
To open the Attribute Specification:
- Right-click the attribute, click Open Specification...from the pop-up menu.
The Attribute Specification dialog box is displayed, you can modify the attribute properties.
Adding Association
An association refers to the relationship specifying the type of link that exists between objects. It shows how the objects are related to each other.
You can add an association to the classes in one of the two ways:
- Using Resource-Centric Interface
- Click on a class, a group of valid editing resources is displayed around the shape.
- Mouse over the smart resource of association, select the desired resource, such as "One-to-Many Bi-directional Association - > Class".
- Drag the resource to the associated class.
 |
Smart resource is a kind of resource which groups the resources of similar purpose together and enables the last selected resource (the default resource) of the group to be visible. To see all the resources, mouse over the default resource to expand it. |
- Using Toolbar icon
- On the diagram toolbar, click the Association icon.
- Click on a class and drag to the associated class.
A connector is added between the two classes.
 |
DB-VA automatically creates the roles in an association between ORM-Persistable classes. |
Modifying Association Specification
You can edit the association specification in one of the three ways:
- Using Open Specification
- Right-click on the connection line, click Open Specification... from the pop-up menu.
Association Specification dialog box is displayed, you can modify the association properties, Roles of classes in Association End From and To, Multiplicity and Navigation etc.
- Using Pop-up Menu
- Right-click on the connection line, the property of the association specification is displayed in the pop-up menu, including Navigable, Multiplicity, Visibility, Aggregation Kind and Edit Role Name..., Qualifier....
- Select the property that you want to edit, check the desired value.
 |
If you right-click on the connection line towards a class, the pop-up window shows the properties of association specification of the respective class. If you right-click in the middle of the connection line, the pop-up window shows all properties of association specification of both classes. |
 |
Role name of the class describes how it acts in the association which will be used in the generation of persistent code. Be sure that you have given the role names to the classes in the association in order to proceed to the generation of code. |
- Using Property Pane
- On the menu, click View > Panes > Property.
The property pane will be displayed.
- Click on the connection line.
The properties of the association specification are displayed in the property pane. You can edit the property under the property pane.
Adding Operation
An operation, also called function or method is the behavior of an object relates to how it acts and reacts.
You can add operation to the class in one of the three ways:
- Right-click on a class, select Add > Operation.
A new operation is added, enter the operation in the form of "operation_name(parameter_name: type) : return_type". You can also edit the operation name by double-clicking the operation or by pressing the F2 button.
- Click on a class, press the keyboard shortcut - Alt + Shift + O.
- Using Class Specification dialog:
- Right-click the class element, click Open Specification....
- Click the Operations tab, then click Add.
Operation Specification dialog box is displayed, you can modify the operation name and properties, such as return type, parameters.
Modifying Operation Specification
An operation specification displays the operation properties, such as name, visibility, return type, and parameters etc.
To open the Operation Specification:
- Right-click the operation, click Open Specification... from the pop-up menu.
The Operation Specification dialog box is displayed, you can modify the operation properties.
Adding ORM Implementation Class
ORM-Persistable class is used to generate the persistent class which has the ability to access the database including the basic operations for add, update, delete and search. As the generated code provides the basic operations for manipulating the persistent data, you may find it insufficient and want to add extra logic to it.
DB-VA promotes the use of ORM Implementation Class to add extra logic to the ORM-Persistable class. When generating the persistent code, the ORM implementation class will also be generated, and thus you can implement the logic to the method in the generated implementation class for manipulating the persistent data.
To add an ORM implementation class:
- Create an ORM-Persistable class with an operation for adding extra logic.
- Mouse over the class and drag the resource of "Create ORM Implementation Class" to the diagram pane.
The implementation class is created and connected to the source class with generalization stereotyped as <<ORM Implementation>>. The source class becomes an abstract class with an abstract operation.
Synchronizing from Data Model to Object Model
DB-VA allows you to generate a Class Diagram from an ERD by synchronization if there is an ERD.
You can synchronize the ERD to Class Diagram in one of the three ways:
Defining Package for Classes
DB-VA provides you with two alternative ways to define the packages for the classes.
- Enter the package name to the <default package> located at the top-left corner of the class diagram by double-clicking the <default package>.
- Create a package element on the diagram:
- On the diagram toolbar, click the Package shape icon.
- Click a location in the diagram pane to create a package element on the diagram.
- Type a name for the Package element.
- Move the desired Class elements to the package
After defining the packages to the classes, the classes are inside the package and depicted on the class repository.
Specifying Stereotypes
Stereotype extends the semantics of the UML metamodel. It classifies the element in what respects it behaves as an instance of metamodel. In order to enable the mapping between object model and relational database, the class has to be stereotyped as ORM Persistable.
To specify the stereotypes of a class:
- Right-click a class, select Stereotypes > Stereotypes....
The Class Specification dialog box is shown with Stereotypes Tab
- Select ORM Persistable, then > button and OK.
Specifying Inheritance Strategy
In a generalization, the subclass inherits all the features of the superclass. DB-VA provides two inheritance strategies - table per class hierarchy and table per subclass to transform the generalization hierarchy to relational model. By default, table per class hierarchy is used for the generalization.
When transforming generalization into relational model, DB-VA transforms the generalization according to the inheritance strategy applied. For more information on the transformation, refer to the description of Mapping Inheritance/Generalization section.
You can specify the inheritance strategy in one of the two ways:
- Specifying from Superclass
- Right-click the superclass, select ORM > ORM Class Details...from the pop-up menu. The Class Specification dialog showing the ORM Class Detail tab is displayed.
- Click Subclasses... to open the Inheritance Strategy dialog box.
- Select the desired subclass from the generalization tree, select the Inheritance Strategy from the drop-down menu, and then click Apply.
- Specifying from Subclass
- Right-click the subclass, select ORM > ORM Class Details... from the pop-up menu. The Class Specification dialog box showing the ORM Class Detail tab is displayed.
- Select the Inheritance strategy from the drop-down menu.
 |
These two inheritance strategies can be applied to different subclasses within a generalization hierarchy in Java project. Applying two strategies to different subclasses within a generalization in .NET project will result in error when the generation of code and database. |
Specifying Collection Type
If one end of an association contains a multiplicity of many, a collection class will be generated for handling the multiple instances. DB-VA allows you to specify the type of collection, including set, bag, list and map.
Set is an unordered collection that does not allow duplication of objects. Bag is an unordered collection that may contain duplicate objects. List is an ordered collection that allows duplication of objects. Map is an ordered collection that maps keys to values while each key can map to exactly one value.
You can specify the collection type in one of the two ways:
- Right-click on the connection line, select the desired collection type from the pop-up menu of ORM > Collection Type.
- Using Association Specification dialog:
- Right-click on the connection line, click Open Specification...from the pop-up menu.
- Click the ORM Association Detail tab, select the Collection Type from the drop-down menu.
Defining ORM Qualifier
ORM Qualifier is used to specify the extra retrieval rules of the generated persistent class for querying the database. DB-VA allows you to define the ORM Qualifiers of the classes in the class diagram before the generation of persistent code. For more information on the usage of ORM Qualifier, refer to the DB-VA Programmer's Guide.
- Open the ORM Qualifier tab inside the Class Specification dialog box in one of the two ways:
- Right-click on the ORM-Persistable class, select ORM > ORM Query... from the pop-up menu. The Class Specification dialog box showing the ORM Query tab is displayed.
- Edit ORM Qualifier in Class Specification dialog:
- Right-click on an ORM-Persistable class that you want to add extra retrieval rules, click Open Specification.
- Click the ORM Query tab.
- Click Add, the ORM Qualifier Specification dialog box is displayed with a list of attributes of the selected class.
- Enter the name of the ORM Qualifier, place a check mark for the Key column of the attribute that will be used in querying the database.
- Click OK to confirm adding the ORM Qualifier. The newly added ORM Qualifier is listed on the ORM Query tab.
Customizing SQL
DB-VA generates SQL statements which are ready-to-use for accessing the database. In some cases, you may find the generated SQL statements not appropriate for your needs. DB-VA allows you to override the generated SQL statements, including the Insert, Update and Delete statements whenever you want to.
To customize the generated SQL statements:
- Right-click on an ORM-Persistable class that you want to customize the SQL statements, select ORM > ORM Class Details...from the pop-up menu. The Class Specification dialog box showing the ORM Class Detail tab is displayed.
- Select the type of SQL statement that you want to customize.
- Click Generate SQL to generate the ready-to-use SQL statement.
The SQL statement is generated based on the property of class.
- Modify the SQL statement to the desired one.
|
|
| Visual Paradigm International Limited |
| Website: |
www.visual-paradigm.com |
| E-mail: |
support@visual-paradigm.com |
|