Skip to content

Class Diagram Syntax

Claudia Loitsch edited this page Dec 23, 2016 · 30 revisions

The parts of the syntax are explained in the following paragraphs. At the end of the document, there is a complete example of a valid class diagram that uses many of the mentioned features.

General elements

  • A class diagram always starts with @start-cls, ends with @end-cls, and requires to put a title in quotation marks right after the start tag, e.g. @start-cls "My Title". In the second place, the default package “RootElement” must be declared with rootPackage RootElement. So the basic structure of a class diagram looks like the following example:
   @start-cls "My Title"
   rootPackage RootElement
   @end-cls
  • Unique identifier are used to define core UML elements, for instance, classes, associations, packages, interfaces, etc.
  • Indentation are not relevant – everything can be formatted left-aligned. Though, spaces are relevant to distinguish between elements. Strings are not restricted in length and must be put in quotation marks.
  • The actual description of an element, e.g. a class desciption, is included in braces and starts right after the element identifier. It is recommended to put the opening brace on the same line as the element identifier. The closing brace shall be put on a single line to facilitate finding enclosed element blocks:
    class Classname{

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

Class

  • A class is introduced with the identifier class followed by an unique id. The id should not include empty spaces and should follow the CamelCase convention, e.g. class MyClass. The id must put in quotation marks if empty spaces are still used, e.g. class "My Class". In terms of long class names, an alias can be put right after the id - introduced with as. For instance, class "LongClassName" as Long.
  • To specify the visibility of a class, the following notations must be placed before the keyword class:
  • public class or + class for a public class (default, if visibility is not given)
  • private class or - class for a private class
  • protected class or # class for a protected class
  • ~ class for a package
  • To specify a class to be abstract, the keyword abstract is placed between the keyword class and the visibility:
  • private abstract class MyClass
  • The definition of attributes and methods is placed within braces after the definition of a class. At first the name is given, followed by a colon, followed by the type:
  • attributename: attributetype
  • The following types are supported: string, int, double, boolean, char, byte, short, long, float.
  • To specify the visability of attributes, or to recognized an attribute as "static" or "final", the corresponding keyword is placed before the attribute name, e.g.:
  • final pi:float
  • This scheme is the same for methods:
  • Visability: public static main(argument: String):void
  • Retrun parameters: methodename():returnType
  • Multiple paramaters are seperated with comma: methodename(parametername:parametertyp, param:String)

Interface

  • Interfaces are defined with the keyword interface, for instance, interface Auto.
  • Attributes and methods are equally defined as for classes.
  • Through, the keyword abstract is not valid for interfaces.

Package

  • 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 the containing elements, for instance:
   package Package1{
      class class1
   }
   package Package2{
      import package1
      class class2
      asc class2 isConnctedWith class1
   }
  • Packages can be also encapsulated, for instance:
   package Package3{
      class Test
      asc Test has Subpackage.Subclass
      package Subpackeg{
      class Subclass
   }

Relationships

The following 5 types of logical connections are described in the following paragraphs: inheritance, association, realization/implementation, notes, n-ary associations.

Inheritance

  • Inheritance means a generalization relationship and is defined by the keyword isa.
  • The inheriting class (also known as child or subclass) is placed on the left side, followed by the isa keyword, followed by the superclass. For instance: BMW isa Auto.

Realization/implementation

  • Implementing an interface of a class is introduced with the keyword impl.
  • The class that implements the interface is place first, followed by the keyword impl, followed by the interface that is implemented. For instance: MyClass impl MyInterface.

Association

  • Generally spoken, associations can link any number of classes. It can be differentiated between indefinite associations, aggregations, and compositions. The corresponding keywords are asc, agg, and com.
  • The keyword bi must be placed before the association keywords to define bi-directional associations. For instance: bi asc.
  • All associations follow the same schema: type of the association, e.g. asc, agg, and com; first involved class; name of the association; second involved class. For instance:
  • asc Driver own Car
  • agg Car consitstOf Engine
  • com Car consitstOf Wheel
  • For aggregations and compositions, the “whole” is place on the left side and the “parts” on the right side.
  • Multiplicities and roles are places right after the class name in squared bracket. Properties of the left class are separated from properties of the right class by a pipe character. Properties within a class are separated with commas. For instance: asc customer has account [1, Inhaber|*].

N-ary Assoziation

  • Associations that link more than two ends can have a complex structure. The following special syntax is introduced to keep similarity and understandability:
  • Instead of placing all associated classes with their properties and roles on a single line (compact representation), all involved classes are placed on a separate line – surrounded with braces. For instance:
   asc assoziationsname{
      Student studentenrolle *
      Arbeit 0..1 <
      Professor 0..1 
   }

Notes and Comments

Comments and Notes

  • Comments and Notes are represented as string, for instance, "This is a comment".
  • Notes can be associated with a class by appending the keyword note right after the class name. For instance: Car note "Every car has an owner".
  • Notes can also refer to an association. In this case, the keyword note is placed at the end of an association. For instance: BMW isa Car note "Class BMW inherit from class Car"

Example

@start-cls "Christmas Class Diagram"

rootPackage RootElement

abstract class Person {
	name : string
}

class "Santa Claus" as Santa
class Child
interface Present
class Toy
class ToyParts
class Clothes

Toy impl Present
Clothes impl Present
Santa isa Person
Child isa Person
asc Santa has Present [1 | *]
Toy note "Children often do like them more than clothes."
bi com Toy consistsOf ToyParts [1 | *]

@end-cls
Clone this wiki locally