Class diagrams

Class diagrams are the most common type of diagram in most object models. They show the static structure of the system, in particular, the things that exist (such as classes and types) and their internal structure. Class diagrams also depict collaborations and relationships between classes, and inheritance structures. A Class diagram may also show instances (or objects) and links between objects,

Creating and drawing Class diagrams

If you need to learn how to create new diagrams in a project, or the techniques for placing elements and drawing links, consult the User's Guide topics found under " Working with Diagrams: Basic Diagram Techniques" in the Table of Contents. See Related Topics below.

Content

Class diagrams usually contain:

Elements of Class diagrams are defined by the UML and Together provides these on the Class Diagram Toolbar. Use Tool-tips to identify the different elements on the toolbar.

Class diagrams may also contain Packages. These represent underlying physical packages that are part of your project, subsystems of a larger system, or logical groupings of information from any resource available to your project. Since Together diagrams can contain any kind of elements, diagrams can also be used as general grouping mechanism for creating different views. A diagram that is imported into another diagram is shown as a Package with a corresponding stereotype-icon. For more information, see Creating Views.

Working with Class diagrams

Outside of the basic mechanics of drawing the diagram to construct your model, there are several conceptual things you should understand about Class diagrams:

Showing project content from different packages

Packages, classes and (Java) interfaces that belong to your project display in the Model tab of the Explorer. These are defined in your project properties (File | Project Properties). If you specified a Class diagram as the default diagram when you created the project, the project package contains a Class diagram that maps out the packages that belong to the project. Depending on your specifications in the New Project dialog (and later Project Properties), the classes of these packages may be reverse-engineered into Class diagrams.

Assuming this is the case, the Class diagram having the same name as its containing package shows the classes and interfaces in the package, along with relationships and dependencies. You can optionally add classes from any package in the project in these diagrams, or you may choose to create new Class diagrams in a package to show different views of the package contents and relationships to other packages. For example, you might create different Class diagrams to show classes for components or subsystems contained in multiple packages. Assume for the moment that everything in such views resides in a package that belongs to the project and thus displays in the Model tab.

To show classes etc. from another project package in a Class diagram:

  1. In the Model tab, navigate to the package containing the element(s) you want to show.

  2. Select the desired element in the Explore and copy it to the Clipboard.

  3. Paste the element in the desired location in the diagram.

  4. After copying all elements, choose Update Package Dependencies from the diagram speedmenu.

Showing classes on the search paths

When you create a project you can define directories any number of search paths whose content you may want to show in diagrams. For example, you might want to show things that reside on the standard for full Java classpath. Such resources "exist" for the project but are not included in generated HTML documentation as belonging to the project.

To show classes etc. from one of the search paths in a Class diagram:

  1. Open or create a Class diagram.

  2. Right-click on the background and choose Add Shortcut from the speedmenu to launch the Add Shortcut dialog. This dialog shows all the content available for the diagram on the left-hand side, and all content residing outside the current package on the right.

  3. Navigate to the resource you want to add and click the Add button. Repeat until you have added all the resources you want.

  4. Click OK to close the dialog.

Tip: If the resource you are looking for is not shown, it is probably not in the search paths defined in Project Properties. You can add resources to the search paths at any time by choosing File | Project Properties | Advanced and selecting the tab for search paths. For more information, see User's Guide: Creating and opening a project- Advanced mode.

Defining inner classes

The easiest way to create an inner class is to drag it over another class and drop it. This works well when the inner class already exists. If the inner class doesn't exist you can create it and define it within its parent at the same time.

To create and define a new inner class:

  1. Select the parent class.

  2. Choose New | Inner Class from the speedmenu.

Tips:

Importing other diagrams

You can import another diagram of any type into a class diagram creating an automatic link to that diagram. A diagram that is imported into another diagram is shown as a Package with a corresponding stereotype-icon. You can open the diagram from the context menu of the package icon. (See also: Packages below).

Key elements and properties

Class

A class is the descriptor for a set of objects that have the same attributes, operations, relationships, and semantics. Classes usually implement one or more interfaces. Classes capture the vocabulary of the system.

In Together, classes have various properties in addition to the usual members. Most of these are defined by the UML, but some such as alias are provided as enhancements to communication. Some properties of interest are listed below:

Key Class properties

Property

 Description

Name

Every class has a name to distinguish it from other classes. Name is a text string. You need only enter the simple name for classes (as opposed to qualified name). Because classes are coupled to round-trip engineering, name must be a valid identifier for the target language.

Stereotype

Provides problem-specific extension of the basic UML vocabulary for a class. You can enter a new stereotype in the property Inspector (Speedmenu | Properties) as you model, or choose from a list of the stereotypes defined in your configuration.

Alias

Optional alias replaces the Name in the diagram. Because spaces and other characters aren't allowed as class names by e.g., Java, using aliases can make your class diagrams more readable.

Interface

An interface is a device that unrelated objects use to interact with one another. It defines a set of methods but does not implement them. A class that implements the interface agrees to implement all of the methods defined in the interface. Interfaces are only supported in Together products with Java language support.

Interfaces have the same properties as classes except for Implements.

Object

An Object represents an instance of a class. Objects are displayed on diagrams to facilitate communication about the model; they have no representation in source code.

Key object properties

Property

 Description

Name

Name is any text string since Objects are not coupled to round-trip engineering.

Stereotype

 You can assign Actor or Database as stereotype or enter any stereotype name you want.

Persistence

You can choose transient, static, or persistent or enter any stereotype name you want.

Links

There are several standard relationships defined by the UML and applicable in class diagrams. Together's class diagrams support:

For tips on drawing Associations, see Tips and Tricks above.

Package

A Package is a group of items of the physical model. Packages may also show logical groupings of model content.

Packages often represent physical directories. If the default diagram for the project (specified in your configuration) is a Class diagram, it will contain Package icons for each physical subpackage under the project directory... essentially it is a Package diagram.

Other diagrams of any type may be represented in a class diagram. These appear as Packages with a visual stereotype, i.e., the icon representing the diagram type. Packages are represented in the Explorer, Diagram toolbar, and element selectors in dialogs with a smaller icon of the same shape:
 

TIPS:

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.

This covers only source-code based elements. Inter-package dependencies between such elements as Objects, Notes, search-path imported classes, Use Cases, etc. are not recognized.

Note: A physical package can't be deleted if it contains source or diagram files, files of non-Together types, or if other applications share the corresponding directory.

Tips and Tricks

Creating Attributes and Operations

To add a member to a class or interface:

  1. Right-click on the class or interface

  2. Select New | Attribute or New | Operation

  3. 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 speedmenu. The new member is inserted before the selected member.

You can also add members form the Explorer. Use the speedmenu of class or interface nodes.

Editing the Attribute or Operation name

To edit a member name in place:

  1. Click the member once to select it,

  2. Click it again to activate its in-place edit field.

  3. Press ENTER when finished editing.

Note: You can enter only the name (as you would in the Name property in the properties Inspector) or you can enter the full declaration for the attribute or operation. Use full correct syntax for the target language of the class.

See also: Editing properties

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.

Showing Association, Aggregation, and Composition

Use the Association icon on the toolbar to draw all types of Association links. After drawing the Association link, select it and use the link's speedmenu to specify which type of association to show in the diagram. (You can also change the type by applying the appropriate pattern to the link.)

Tip: You can show the direction of Associations by setting the Directed property in the link's properties Inspector (Speedmenu | Properties). For more information see User's Guide: Common Customizations: Association direction.

 

How to show different Association types. The directional arrow is specified in Default or Diagram Options.

Showing Java Beans

Java Beans, including Enterprise JavaBeans (EJB), are displayed as classes 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 a Bean symbol by the class name. Display support for Beans is controlled in Default and Diagram Options.

To enable display of Java Beans-specific information:

  1. Invoke the Options dialog at default, or project level (see Getting Started Guide: Using the Options dialog, and Multi-level configuration architecture).

  2. Select the View Management tab in the Options dialog

  3. Check the option Show Beans. Optionally check the Show Accessors option if you want to show the access methods in the Bean class icon.

  4. Click Apply or OK.

Tip: You can create new Bean and EJB classes using Class by Pattern on the diagram toolbar.

Cloning diagrams and elements

Non-trivial systems will probably require many class diagrams to capture the static structures of a system. Use the Clone feature to create new diagrams with identical content in the same package (diagram speedmenu | Clone) or new elements with identical properties in the same diagram (element speedmenu | Clone).

See also:

Additional resources