Chapter
2 |
Class
Diagrams |
|
CHAPTER
CONTENTS
|
About
Class diagrams
Class diagrams show the static structure of the model, in particular,
the things that exist (such as classes and types), their internal structure,
and their relationships to other things. Class diagrams do not show temporal
information, although they may contain reified occurrences of things that
have (or things that describe) temporal behavior. An object diagram shows
instances compatible with a particular class diagram. A class diagram is
a graphic view of the static structural model. The individual class diagrams
do not represent divisions in the underlying model.
A class diagram is a collection of (static) declarative model elements,
such as classes, interfaces, and their relationships, connected as a graph
to each other and to their contents. Class diagrams may be organized into
packages, either with their underlying models or as separate packages,
that build upon the underlying model packages.
Main Class diagram
elements
This section discusses the major elements that can be part of a Class
diagram. In addition to these, a Class diagram may contain elements such
as Notes that are common to all diagram types. For information see Chapter
1: Diagram Basics.
Packages
A package contains groupings of
the physical model of the Project and may also contain logical packages.
It can also contain other packages. When you create or open a Project,
the Object Model Browser displays the Default.vfPackage file as
a class diagram in the Diagram pane, and the composition of the top-level
Package in the Navigation pane. Together automatically scans the
Project directory updating the view to include any new source files and
directories (i.e., subpackages). The view can also contain nodes imported
from other packages and logical packages. See Importing
below.
Creating and naming
packages
You create a package by placing a Package node on the diagram using
the Diagram Toolbar (see Figure 2.1).
A directory (i.e. a package) is created one level down from the current
package directory and named with the Together default name. The
directory is renamed when you change the name property of the
Package node. A Default.vfPackage file for the directory is
automatically created and stored at the package's level.
|
Figure 2.1. Icon for a Package
node. Place on diagram using Toolbar, edit name in place. |
After you create a new package, you need to navigate into it using the
Navigation pane (see Getting
Started: Object Model Browser). You can then create nodes for packages,
classes, interfaces, etc. in the top-level Class diagram for this package.
You can also import packages or classes.
For information on working with Packages see Working
with Class Diagrams: Packages in this chapter.
Logical Packages
A logical package contains
groupings of the logical model of the Project. When you create a
new class diagram you create a logical package icon on the class diagram
of the package for which it was created. When you browse a logical
package it only parses the nodes in the view and updates them accordingly;
new source files do not show until you import
them. For more information see Working
with Class Diagrams: Logical Packages in this chapter.
Classes
and Interfaces
A class is the descriptor for a set of objects having similar structure,
behavior, and relationships. Some modeling elements that are similar in
form to classes (such as interfaces, signals, or utilities) are annotated
using keywords on class symbols. Some of these are separate meta-model
classes and some are stereotypes of Class. Classes are declared in class
diagrams and used in most other diagrams.
Creating Classes
and Interfaces
You create a class by placing a Class node on the diagram; you create
an interface by placing an Interface node (use the appropriate Toolbar
button). For each of these you create, Together creates a new source
code file in the current package and names it with the default name (e.g.,
Class1). The source file is renamed when you change the name
property of the Class or Interface node.
Note: You can control whether or not source code files are automatically
renamed in C++ and COBOL projects via system configuration properties
in the config.properties file. Search this file for:
-
(C++): codegen.cpp.rename_declaration_files and codegen.cpp.rename_definition_files
-
(COBOL): codegen.ocobol.rename_files.
For information on config.properties, see Getting
Started, Chapter 5.
|
Figure 2.2. Icons for a Class
and Interface nodes. Place on diagram using Toolbar, edit name in place
(except imported). |
When adding a new class or interface to a diagram, you can directly type
a new name in the edit box that appears around the node's name. This
is called 'in-place' editing. You can also use 'in-place' editing
to change the name at any time by double clicking on the displayed name.
Code
generation
Whenever a node or member is added to the diagram, code or property tag
comments are generated in the relevant source file. If these elements
change or are deleted, the relevant source code is updated. Conversely,
if you change the code, the relevant visual element in the diagram is updated.
Both source code and visual diagrams are continuously in-sync.
Property tag comments
Where properties of the icons cannot be directly converted to source code
in the source file they are stored as tag comments above their declaration.
For example, if you give a class a stereotype by editing the stereotype
property, the code could look like this:
/*@stereotype controller*/
public class Class1 {
}
Objects
An object represents a particular instance
of a class. It has identity and attribute values. The same notation also
represents a role within a collaboration because roles have instance-like
characteristics.
The Object node icon on the diagram has two sections (see Figure 2.3).
The top section displays the object's name, the second displays the name
of the class from which the object is instantiated. When you select
an object icon, its properties are displayed in the Inspector. If you select
the class name, the class properties display in the Inspector and the Text
pane displays the source code for the class. By right-clicking
the class name in the object icon you can access the Class
context menu.
Objects are the properties of the logical view of diagrams; they have
no relevant (program) source code. Objects are not shared among different
diagrams and therefore can't be imported.
|
Figure 2.3. Icon for an Object
node. Place on diagram using Toolbar, edit name in place. |
Linking to a class
through the object's class property
To link a class to the object:
-
Select the Object node's icon in the diagram
-
In the Inspector pane, click on the object's class property
-
Display the property editor for object's class (see Type
Selection dialog). From this you can select a class from the
currently configured source or class path.
-
Select a class and click OK.
You can also create an instantiation link from the object to the class.
In this case the object's class property is automatically updated.
Links
A Link is a tuple (list) of object
references. Most commonly, it is a pair of object references. It is an
instance of an association. Other association adornments (aggregation,
navigation) may be shown on the link roles.
Generalization is the taxonomic relationship between a more general
element and a more specific element that is fully consistent with the first
element and that adds additional information. It is used for classes, packages,
use cases, and other elements. Generalization is shown as a solid-line
path from the more specific element (such as a subclass) to the more general
element (such as a superclass), with a large hollow triangle at the end
of the path where it meets the more general element.
A dependency indicates a semantic relationship between two (or more)
model elements. It relates the model elements themselves and does not require
a set of instances for its meaning. It indicates a situation in which a
change to the target element may require a change to the source element
in the dependency.
Association and Aggregation
link properties
Selecting an Association or Aggregation link icon on the diagram adds a
tab, called Link,
to the Inspector pane. The Link tab enables you to view and edit the properties
of the selected link. The Link tab disappears when the link icon is deselected.
Association and Aggregation
link adornments
The UML adornments for Associations and Aggregations can be quickly added
and edited on the diagram using link's
context menu. Right-click a link line near the source class to invoke
menu with client's adornments or right-click it closer to the destination
class to invoke menu with supplier's adornments.
To change an adornment, click it on the diagram once, then click it
once more to activate in-place edit box.
Choose pattern for
a link
Once you created a link, some its implementation is immediately generated.
If you wish to change the way the relationship between classes is implemented,
Together provides a number of implementation patterns, defined in the
config.properties file. The first
property in the Link tab serves to invoke the Choose
pattern dialog, which enables you to alter the source code of the link.
Particularly you can switch between Aggregation and Association, and
between those and Dependency links which are implemented as comments (Links-as-comment,
also known as "pure links").
Link
connecting rules
Here is a list of links and nodes they can connect:
Type of link: |
Connection(s): |
Aggregation |
Class - Class
Class - Interface*
Object - Object |
Association |
Class - Class
Class - Interface*
Object - Object |
Generalization |
Class - Class
Interface - Interface* |
Implementation |
Class - Interface* |
Instantiation |
Object - Class
Object - Interface* |
Dependency |
Between any pair of such nodes as
Package, Class, Interface and Object |
*(Java only)
Links
appear as Associations not Aggregations
Together automatically recognizes all attributes with names
beginning with the prefix 'lnk' as being Association links.
Aggregation links are recognized by the shapetype tag comment:
/*@shapeType AggregationLink*/ <--
the tag comment
LinkClass testAttribute;
If Together finds a link attribute having neither a name
beginning with 'lnk' nor the shapetype tag comment, it assumes
that the attribute is an Association link and displays it
as such. To change the link type to an Aggregation, insert the shapetype
tag comment above the attribute's declaration in the source file as shown
in this example.
For example, if you draw an association/aggregation link from a class,
a link as attribute is generated: private %Dst% %Name%; If you
draw an association/aggregation link from a Java interface, a link-as-comment
is generated: /*# %Dst% %Name%*/. Since an Interface cannot be
instantiated and thus can not have any attributes except for static ones,
a link-as-attribute does not have much sense in interface.
Working with Class
diagrams
This section provides information about things you may need to do while
working with a Class diagram or its elements. Topics in this section include
importing classes, showing Java Beans, and manipulating elements in the
diagram. Subsequent sections cover specifics for working with different
diagram elements such as Packages, nodes and Links.
Importing
Classes and Interfaces from other Packages
Some
classes or interfaces are referred to as source, compiled
or imported:
|
Source classes are the classes declared in source files.
They are contained in the project directory or in its subdirectories. |
|
Compiled classes (Java only) are displayed on the diagram
with the <<compiled>> label above the class name. Paths to
these classes must be defined in the class
path. Compiled classes on the paths outside the project are not automatically
available... you must import them. |
|
Imported classes are classes that are imported from other
packages into the diagram. Icons for imported classes are denoted by a
cut top right hand corner (see Figure 2.2).
For Java, you can import compiled classes as well as ordinary source
classes. |
To
import classes or interfaces:
-
Choose Import... from the Diagram context menu to display the Import
dialog.
-
Select the desired packages and/or classes in this dialog (see ) and click
OK.
To remove imported classes or interfaces:
-
Select the icon on the diagram.
-
Choose Unlink from the node icon's context menu.
Showing Java Beans
(Available in Together editions with Java
support)
To
show Java Beans-specific information:
-
Right click on diagram background
-
Select Diagram Options...
-
Click on the Classes Tab and check
the Beans Support checkbox.
-
Click on OK to accept the change
Java Beans are displayed with two additional sections, one for properties,
and the other for events to which the Java Bean can respond. When you select,
the Beans support option all the classes on the diagram are rescanned.
Those classes with get and set methods are displayed as Java
Bean nodes on the diagram denoted by the Beans symbol by the class name.
Hiding,
showing and filtering visual elements
Individual Package, Class, and Interface nodes can be hidden on the class
diagram by choosing Hide from the node icon's context menu. You can be
later re-display hidden nodes using the Show Hidden... command on the Diagram
context menu. (See Show Hidden dialog
for more information).
The
Inspector for class diagrams has a special tab for hiding or showing groups
of icons: the Display tab. To learn more about showing and
hiding types of Class diagram elements, see Getting Started, Chapter
4: Showing and
Hiding Diagram Elements.
Moving
and copying visual elements
You can visually move nodes into different packages or members
into different nodes, by dragging them on top of the destination node's
icon and dropping them. You can use the same technique to copy members
to different classes and interfaces by pressing CTRL before initiating
the drag. When you move an element, it is deleted from the source package
or node and moved into the destination package or node. Copied elements
are not deleted from their source.
Drop target focus
When you drag nodes or members around on a class diagram, the appearance
of other node icons changes when the dragged element crosses their boundaries.
This indicates that the icon being "crossed" has received focus and
is therefore a valid drop target. For Class, Interface and
Package nodes, the drop focus is represented by a blue rectangle around
the icon border. Members are highlighted in blue when focused.
For example, if you drag a Class icon to a Package icon, the Package
icon receives focus. If you then drop the Class on the Package icon,
the class is added to the package. If you drag a member from one class
over the Name section of a different Class node icon, the second Class
icon becomes focused and dropping places the dragged member at the bottom
of the appropriate section.
If you drag a member onto another member of the same type in the destination
class, then you can position it within the section before dropping it.
Showing different levels
of the diagram
Besides
selecting the different groups to display on the diagram, you use the UML
details options to create different levels of detail on the class diagram.
To create different levels:
-
Right click on the background of the diagram.
-
Select Diagram Options... to display the Diagram Options... dialog.
-
On the Diagram tab, in the Details
section, select the desired level of detail for the diagram
-
Click on OK to accept the changes
Class Diagram properties
When you click on the background
of a class diagram, the Class diagram properties display in the Inspector
pane. There are two tabs at the top of the Inspector:
-
Diagram - Package Properties
-
Display - Filters set for the diagram
Working with Packages
Creating nodes in
Packages
To create classes or interfaces
in a package, either use the diagram toolbar to place a new node on the
diagram, or right click on an existing package node of the type you want
to create and choose New Class or New Interface (depending on the selected
node type) from the context menu.
You can also use drag and drop to move existing classes or interfaces
into a package.
Select a class name displayed in a package icon to display the corresponding
Class properties in the Inspector.
Browsing
a Package on the diagram
-
Right click on the package node in the Navigation pane.
-
Choose Browse to open the diagram in the current Object Model Browser window,
or Browse in New Window to open it in a new browser window.
Usage links
When you choose Update Package
Links on the diagram context menu, Together scans each package on
the diagram. If it finds an element within the package that is linked
to an element of another package, then it creates a usage link between
those package icons on the diagram.
This covers only source-code based elements. Such elements as Objects,
Notes, and elements of proper diagrams (e.g. Imported class or Use Case),
even if they have interpackage links, do not result in recognizing Usage
links between the corresponding packages.
NOTE: Usage links are filtered off by default. Go to the Inspector,
and switch in the Display tab (upper), Link tab (lower) usage dependency
to show. If you draw a Dependency link between packages it is shown independently
of this filter. Manual dependency links are shown/hidden by another filter
(manual dependency).
Moving nodes
into Packages
Classes
and interfaces can be visually moved to other Packages by dragging from
the diagram to the target package. For more information, see Moving
elements. Other packages, imported or compiled
classes or interfaces cannot be moved in this way however. Pressing
the CTRL key and dragging a class into a package imports the
class into that package. The class name in italic appears in the package
icon.
By default the package icon on the diagram displays its updated contents.
You can use the class/interface context menu and
add attributes and operations directly. The diagram option Subpackages
controls how the packages are displayed in the diagram.
Moving nodes from
Packages
By
dragging the class or interface name from the package element to the diagram
background, you can move that class or interface to the current diagram,
deleting it from its current package and moving it to its new package.
Pressing the CTRL key and dragging a class from a package imports the class
to the current diagram. The imported class icon appears.
Renaming a Package
You
can rename a package using the Inspector or in-place. This change is propagated
to all the source files in the opened packages. If a class in another package
is linked with a class in the package being renamed, the link is kept.
However, the propagation is not performed in the sources in the packages
that are not opened at the moment of rename. You should correct there the
package statements and/or class qualified names manually or use Tools |
Scan all diagrams command to enforce opening all the diagrams and packages
in the project.
You can only delete a package using its context menu if all of its
contents have been removed first.
Deleting a Package
You
can only delete a package using its context menu if all of its contents
have been removed first.
Working with Logical
Packages
Creating a logical package
-
In the Navigation pane, select the physical package for in which
you want to create a new logical package and choose New Diagram from its
context menu.
-
In the New Diagram dialog choose the Logical Package option and specify
a name for the logical package.
-
Browse into the newly created logical package to begin working on its diagram
(this is a .vfClass type diagram).
Creating nodes in
the Logical Package
This
works in the same way as the physical package, but when you add a node
to the .vfClass diagram, it also appears on the top-level diagram of the
package for which it was created. If you try to create a package
to the .vfClass diagram, it appears on both diagrams as well.
Source code is always generated at the physical level where the logical
package's .vfClass file resides.
Moving nodes
into Logical Packages
When
you drag a class or interface onto the logical package icon, it is hidden
on that diagram and displayed on the logical view. It can be shown
by using the Show Hidden... command on the diagram context menu.
Unlike the Package, you can drag imported nodes
onto the logical package icon.
Moving nodes
from Logical Packages
When
you drag a class or interface name from the Logical Package icon, it is
removed from the view and displays on the diagram, but it is not deleted.
Deleting a Logical
Package
Unlike
the Package, a Logical view can be deleted even if it has contents because
its contents are only views of the classes, etc.
Working with
Classes and Interfaces
This section covers a number of topics you may need to know about when
working with class and interface icons in Class diagrams.
Class/Interface
properties
You view and edit Class and Interface properties in the Inspector
pane. The implements and extends properties have property
editor dialogs. For information see Getting
Started: Chapter 4, Editing Properties, and Type
Selection dialog.
Class
context menu
When you right click on a Class icon in a diagram, the Class
context menu displays. Right clicking an Interface icon displays the
Interface context menu .
Whenever you use a command that creates or adds an icon to a Class
diagram, code generation or deletion takes place. For example, creating
a class node on the diagram creates a new source file in the current package
containing the diagram's .vf file (e.g., Default.vfPackage).
Attributes and Operations
An attribute is a named property of a type or class. An operation is a
service that an instance of a class may be requested to perform. It has
a name and a list of arguments.
Creating
Attributes and Operations
To add a member to a class or interface:
-
Right click on the class or interface
-
Select New attribute or New operation
-
The member is inserted after the last attribute or operation, respectively
If a class or interface already has attributes or operations, you can right
click on these directly to create the additional member using the existing
member's context menu. The new member
is inserted before the selected member.
You can reorder members at any time using drag-and-drop.
Editing
the Attribute or Operation name
To edit a member name 'in-place':
-
click the member once to select it,
-
click it again to activate its in-place edit field.
-
Press Enter when finished editing.
With in-place editing of Attribute and Operation
names, you have a choice:
-
you can enter only the name (as you would
in the name property in the Inspector)
-
you can enter the full declaration for the attribute
or operation.
For the latter, Together requires that the
in-place entry be full correct syntax for the target language of
the project.
Note also that if a value is specified for the
alias property, that value displays in the icon. However, in-place
editing displays and operates on the value of the name property.
Editing the other
Attribute and Operation properties
To edit other properties:
-
Select the attribute or operation
-
The Inspector pane displays either the attribute properties or the operation
properties
-
Change the desired properties
-
Click Apply to update the diagram
Rearranging the order
of Attributes and Operations
Use
drag and drop to reorder members within a class icon. The members are reordered
in source code simultaneously.
Dropping one member on another member name positions the dropped member
before the target member. Dropping a member on the class name moves it
to the last position in the attribute or operation list respectively.
Moving/Copying
Attributes and Operations
Members
can be visually moved or copied to other classes and interfaces.
For more information, see Moving elements.
Working with Links
Showing
direction of Association links
Association links can be configured to show the direction of the link.
To configure for link direction:
-
Right click on diagram background and select Diagram Options... from the
diagram context menu.
-
Click on Classes tab
-
Click on Associations - Draw Directed
-
Click on OK
This causes association link icons to display an arrow head indicating
the direction of the link.
Using Vector and other
containers as links
This
is an example of how to create a link that represents a Vector that contains
only one type of class:
-
Create the type of link you want between the two classes
-
Select the link
-
In the Inspector, click on Properties tab:
-
Change the associates property to the link's destination class
name, or use the ellipsis button to display the associates dialog (from
there you can also select the class)
-
Change the type property to Vector.
-
Click on the value column and click on the ellipsis button to display the
type dialog where you select a standard type (click on the package drop-down
list and select <standard types>). Select Vector and
click OK.
-
Click the Apply button.
NOTE: If you use C++ templates, Together recognizes destination class by
the template argument instead of the associates property.
Alternatively, choose the special pattern, which uses the naming convention
for attribute name. However, use this feature carefully since patterns
for parser and code generator should be in correspondence. The blueprints
are defined in the config.properties file.
-
In the Inspector, click on Link tab:
-
Click on the "choose pattern..."
-
In the pattern dialog, select Association
as Vector (or Aggregation as Vector). The source declaration
is displayed in the Preview box. Click Apply.
-
Note the source code and diagram are updated without clicking Apply in
the Inspector.
This way you can create links using any predefined containers, or containers
that you have created.
Chapter contents
About Class diagrams
Main Class diagram
elements
Packages
Creating and naming
packages
Logical Packages
Classes and Interfaces
Creating Classes
and Interfaces
Code generation
Objects
Linking to a class
through the object's class property
Links
Association and Aggregation
link properties
Association and
Aggregation link adornments
Link connecting
rules
Links appear
as Associations not Aggregations
Working with Class
diagrams
Importing Classes
and Interfaces from other Packages
Hiding and showing visual
elements
Moving and copying
visual elements
Showing different levels
of the diagram
Class Diagram properties
Working with Packages
Creating nodes in
Packages
Browsing a Package
on the diagram
Usage links
Moving nodes
into Packages
Moving nodes from
Packages
Renaming a Package
Deleting a Package
Working with Logical
Packages
Creating nodes in
the Logical Package
Moving nodes
into Logical Packages
Moving nodes
from Logical Packages
Deleting a Logical
Package
Working with
Classes and Interfaces
Class/Interface
properties
Class context menu
Attributes and Operations
Creating Attributes
and Operations
Editing the Attribute
or Operation name
Editing the other
Attribute and Operation properties
Rearranging the order
of Attributes and Operations
Moving/Copying
Attributes and Operations
Working with Links
Showing direction
of Association links
Using Vector and other
containers as links
This page is hosted by the Chair for Applied Software Engineering of the Technische Universität München.
Imprint (Impressum)