2. Classes

Classes are the core elements in an object-oriented model. They are represented by rectangles divided into three sections that display class information in the topmost portion, attribute information in the second, and operation information on the bottom. These compartments can be hidden via the context menu, as can package and stereotype information.

2.1. Attributes

Every class can have attributes that express some of its properties. In UML, every attribute has a name and a type. The type can be any other DataType, Class or Interface that is in the model. You can select the type from a combo box of all available types. If the type you need is not in the list, you can create it elsewhere, and then select it from the list.

Figure 12.6. Properties of an attribute.

Properties of an attribute.

Attribute Properties

  • Multiplicity - The multiplicity field determines how many references the class has to this attribute.

  • Ordering - Can be set to ordered, unordered, or unspecified (default).

  • Owner - Specifies to which element the attribute belongs.

  • Visibility - The visibility of an attribute expresses which other classes can access it. The options are:

    Public+Accessible to all objects
    Protected#Accessible to instances of the implementing class and its subclasses.
    Private-Accessible to instances of the implementing class.
    Package~Accessible to instances of classes within the same package.
  • Modifiers - You can also set whether the attribute is write-only by checking the final check box. An attribute can also be static (class scope instead of instance scope) or transient or volatile. An initial value can be given as a Java expression.

  • Stereotype - Stereotypes can be added an removed via a dialog that is accessed by right-clicking the stereotype field and clicking 'Open' or clicking the ellipsis button .

  • Type - The Type dropdown sets the type of the attribute.

  • Initial Value - This optional field gives an initial value to the attribute. Multi-line entries are accepted here; to create a new line, use Ctrl-Enter. (Note that this functionality is set in the Settings tab. More information is availalbe at Section 1.7, “Key Mappings”).

  • Accessor Methods - You can create the appropriate accessor methods for this attribute with a simple click. Just hit the button 'Add Accessors' in the Properties tab of the Details pane, and in the list below you will see a list of methods. This list depends on the multiplicity and the state of the final check box. If the multiplicity is 0..1 or 1..1, one setAttribute and one getAttribute method are created. If final is checked, it is only the getAttribute method that is created. If you chose a multiplicity that has a numerical upper bound (and not 1), array access methods are displayed. If you give a multiplicity with unlimited upper bound (also known as ..* or ..n), accessors for a java.util.Collection are created.

    When you create a new attribute, these methods are created automatically if you checked 'Create accessors for new attributes' in Edit-Settings. Every time you change the name or the multiplicity of the attribute, the access methods will change accordingly.

    If you prefer to have only some accessor methods, right-click on one of the entries in the list 'Accessor Methods'. Then, select 'Delete'. The operation will be removed from the list; thus, it will not be marked as accessor any more. You will be asked if you want to remove this relation only or if you want to delete the operation completely. Whether this dialog appears or not is determined by your settings, which can be accessed in Edit-Settings. The 'Modeling' tab is displayed in Figure 9–9. Notice the 'When removing attributes' drop-down list. The default option is to ask before deleting the accessor operation. You can also choose to always keep the operations or to delete them directly without asking. This depends on your preference and your style of working with accessor methods.

Figure 12.7. 'Remove Attributes' setting

'Remove Attributes' setting

2.2. Operations

Figure 12.8. Properties of an operation.

Properties of an operation.

For every operation, you can set several UML properties. Among them are visibility, scope (class or instance), and concurrency (with designators like sequential, synchronized, and concurrent). You can set an operation to be final, be a query, abstract, or a root method (with no parent).

The two lists on the right of the Properties tab are used to refine the operation's signature. In the list of parameters, the first parameter return is always there. This defines a return type for this operation. Similarly, you can add parameters that may be given a name, a type and the modifier 'final'. The final modifier is a special case that we introduced to handle Java.

You can define a constructor by setting the stereotype <<create>>. The code generation generates a constructor signature.

Operation Properties

  • Name - This field gives a name to the operation. This field can also be changed directly in the diagram.

  • Owner - The 'Owner' field specifies the element to which the operation belongs.

  • Accessed Attribute - In the field 'Accessed Attribute', you can define whether this operation should be marked as an accessor method for an attribute. The primary use of accessor methods is to modify the attribute internally and to control access to the modifications externally. You can choose a modified attribute (if the operation is an accessor method created by Poseidon, an attribute is already selected) or select none if you want to decouple an accessor method from its attribute.

  • Visibility - Can be set to public, protected, package, or private.

  • Modifiers - One or more modifiers are set by means of the checkboxes.

  • Concurrency - Set the concurrency to sequential, synchronized, or concurrent

  • Stereotypes - Stereotypes can be added an removed via a dialog that is accessed by right-clicking the stereotype field and clicking 'Open' or clicking the ellipsis button .

  • Parameters - Lists parameters of the operation. In Poseidon, return types are considered a special type of parameter.

  • Raised Signals - The last list, 'Raised signals', is used to define whether this operation throws exceptions.

    Select 'Add...' from the context menu to insert a new exception.

    To edit this exception, select Open from the context menu, then enter a name and select a type for the thrown exception. As you know, only the type of the exception (not the name) is relevant for code generation. If you need more exception types, simply create the corresponding class in your model (e.g., MailException in your package javax.mail). Your exception type must end in ...Exception in order to be visible in the type list of exceptions.

    You may also assign the <<exception>> stereotype to an element and use that element as a raised signal. First, apply the stereotype using the stereotype dialog. Second, add an exception to the desired operation through the 'raised signal' context menu within the properties tab. Third, select the exception-stereotyped element from the dropdown 'Type' list.

2.3. Association Classes

Poseidon now supports association classes.

To create an association class:

  1. Create the two classes to be associated.

  2. Select 'Association Class' from the toolbar.

  3. Click on one of the original two classes and drag the mouse to the other class.

  4. An association class will be created between the original two classes.