Class Diagrams

Content

A class diagram always starts with @start-cls, ends with @end-cls, and requires a title in quotation marks right after the start tag. In second place, the package can be declared with the keyword rootPackage. The default package is "RootElement" and if this is used, the entry will be omitted.

@start-clsd "My Title"
rootPackage RootElement
@end-clsd

Order of Elements

The order of elements within a class diagram is pre-defined and must be kept. At first imports are defined, followed by classifiers, associations, and packages. Details of each element description are given in the subsequent sections.

The actual description of an element, e.g. a class description, is included in brackets and starts right after the element identifier. It is recommended to put the opening bracket on the same line as the element identifier and the closing bracket on its own line to help when searching for enclosed blocks.

Class Definition

A class is defined by the keyword class and a unique id. The id cannot include empty spaces, except if it is placed in quotation marks. It is recommended to use the camelCase convention. In terms of long class names, an alias can be put right after the id - introduced with as.

class Long as "LongClassName"

Visibilities

To specify the visibility of a class, one of the following notations must be placed before the keyword class: public or +, private or -, protected or # and ~ for a package. If the visibility is not specified it is assumed to be public.

public class MyClass

To define the class as abstract the keyword abstract is placed before class, but after the visibility keyword in case it was defined.

private abstract class MyClass

Methods and Attributes

The definition of attributes and methods is placed within brackets after the definition of a class. At first the name is given, followed by a colon, followed by the type.

class MyClass{
    attributeName: attributeType
}

The following types are supported: string, int, double, boolean, char, byte, short, long, float.

To specify the visibility of attributes or to recognize an attribute as static or final, the corresponding keyword is placed before the attribute name.

final pi:float

Passing Parameters

The same scheme is used to pass parameters for methods. Several parameters are seperated by commas. A method without a parameter still has to have 2 brackets defined to declare the ID as a method. The return parameter of a method is placed behind the method separated by a colon.

method():returntype
methodname(parametername:parametertype, param:string)

The visibility and the keywords abstract, final or static can also be placed in front of the method name.

public static main(argument: String)

Interfaces

Interfaces are defined exactly like classes except with the keyword interface.

Attributes and methods are equally defined as for classes, however, the keyword abstract is not valid for interfaces.

interface Animal

Package Definition

To define a package the keyword package in combination with a unique ID is used. An alias is not allowed for the package name. A package requires to define a block, which is indicated by curly brackets.

package MyPackageName {
}

Packages can describe elements from other packages with the keyword import and the corresponding reference id.

package Package1{
    class Class1
}
package Package2{
    import Package1
    class Class2
}

Packages can be encapsulated. If a class from an underlying package is to be used, this has to be facilitated with the entire reference to said class.

package Package{
    class Test
    asc has (Test,UnderlyingP.UnderlyingC)
    package UnerlyingP {
        class UnderlyingC
    }
}

Associations

A simple association is introduced by the keyword asc.

@start-clsd "Simple association between classes A and B"
    class A
    class B
    asc name(A,B)
@end-clsd

The direction always follows the order of operands.

asc has(zoo,lion)
asc livesIn(lion,zoo)

There are also bidirectional associations that are introduced with the keyword bi.

@start-clsd "Bidirectional association between classes A and B"
    class A
    class B
    bi asc name (A,B)
    @end-clsd

Compositions and Aggregations

Compositions and aggregations are introduced with the keywords com and agg respectively. The class with the composition or aggregation end is named first.

com consistsOf(Zoo,Gehege)
agg has(Zoo,Gehege)

Inheritance

The inheritance between classes is introduced with the keyword isa followed by the operands. Inheritance is the only exception where no id is given.

isa(animal,tiger)

Roles and Cardinalities

Associations can be specified further with roles or cardinalities. Here the attributes are written in square brackets and separated by a comma. The order of the operands decides which attribute corresponds to which operand. Roles are introduced with the keyword role, cardinalities with the keyword card.

asc care (animal, vet) role[patient, doctor]

If there is no attribute to specify for one operand the underscore character _ is used.

asc care (animal, vet) role[patient,_]

The description of cardinalities is analogous to that of roles, only that they aren't seperated by comma but by colon. The default cardinality is 0..1

asc care (animal, vet) card[*:0..1]

Roles and cardinalities can be combined by chaining these attributes behind one another.

asc care (animal, vet) role[patient, doctor] card[*:0..1]

Realization/Implementation

Implementing an interface of a class is introduced with the keyword impl. The class that implements the interface is placed first followed by the interface that is implemented.

asc has (chimp, swing) card[1:1] role[Owner, _]

N-ary Connections

N-ary connections are described analogous to the rules introduced above.

asc descriptor (A,B,C) role[RoleA, RoleB, RoleC] card[CardA:CardB:CardC]

Notes

Notes are the same for associations, interfaces and classes and are introduced with the keyword note, followed by square brackets and the note in quotation marks.

class A note["Note for a class"]
interface A note["Note for an interface"]

If a class or the interface contains attributes or methods, the notes are given on the same level as the attributes and methods.

class A {
    name: string
    note ["Note for the class"]
}

Example

Example Class Diagram
@start-clsd "class"

rootPackage RootElement

class Animal {
	+ name : string
}

- class Giraffe
- class Zebra
class Zoo
class Zookeeper {
	- feed ( animal : Animal )
}

isa (Giraffe, Animal)
isa (Zebra, Animal)
asc employed (Zookeeper, Zoo) card [1..* : 1..1]
asc inhabitant (Animal, Zoo) card [0..100 : 1..1]

@end-clsd