Class diagrams

edit

A ClassDiagram draws classes linked by relations (Mermaid classDiagram):

var diagram = new ClassDiagram();
var animal = diagram.AddClass("Animal");
animal.Annotations.Add("abstract");
animal.AddMember("int age", ClassMemberVisibility.Public);
animal.AddMember("makeSound() void", ClassMemberVisibility.Public).IsAbstract = true;

var duck = diagram.AddClass("Duck");
duck.AddMember("String beakColor", ClassMemberVisibility.Private);
duck.AddMember("swim()", ClassMemberVisibility.Public);

var pond = diagram.AddClass("Pond");
pond.AddMember("count() int", ClassMemberVisibility.Public).IsStatic = true;

// Duck inherits from Animal; a Pond is made of Ducks
diagram.AddRelation(animal, duck, fromEnd: ClassRelationEnd.Inheritance);
var relation = diagram.AddRelation(pond, duck, fromEnd: ClassRelationEnd.Aggregation, label: "hosts");
relation.FromCardinality = "1";
relation.ToCardinality = "*";

A class diagram of animals

Classes

A ClassDiagramClass is a box of three compartments: its Annotations («interface») and its name, its attributes, and its methods. A ClassMember whose text has parentheses is a method; its Visibility is shown with the characters of UML (+, -, #, ~), a static member is underlined and an abstract one is in italic. The generic types are written List~int~, as in Mermaid, and shown List<int>; GenericType is the generic parameter of the class. HideEmptyMembersBox hides the empty compartments.

Relations

A ClassRelation has a decoration at each end: an Inheritance triangle, a Composition or an Aggregation diamond, an Arrow; a dashed line (IsDashed) makes a dependency or a realization. The multiplicities are drawn next to the ends, and the label in the middle.

MermaidRelation
A <\|-- BB inherits from A
A *-- BA is composed of B
A o-- BA aggregates B
A --> BA is associated with B
A ..> BA depends on B
A ..\|> BA realizes B
A -- B, A .. BA link

Namespaces, interfaces and notes

A ClassDiagramNamespace groups classes in a frame, and can nest. A ClassDiagramInterface is drawn as a lollipop: a small circle linked to the classes that provide it (Mermaid bar ()-- Foo). A ClassDiagramNote is alone or attached to a class:

var diagram = new ClassDiagram { Direction = DiagramDirection.LeftToRight };
var orders = diagram.AddNamespace("Orders");
var order = new ClassDiagramClass("Order") { GenericType = "TItem" };
order.AddMember("List~TItem~ items");
orders.Classes.Add(order);

var serializable = new ClassDiagramInterface("ISerializable");
diagram.Interfaces.Add(serializable);
diagram.AddRelation(order, serializable, toEnd: ClassRelationEnd.Lollipop);
diagram.AddNote("Orders are immutable once paid", order);

A class in a namespace, with an interface and a note

In Mermaid, the dotted names of the namespaces nest them (Company.Engineering is in Company), unless the configuration sets class.hierarchicalNamespaces to false.

Mermaid syntax

The parser reads the classes (class Name, class Name~T~["label"], class `Name with spaces` , with their members between braces or as Name : +member), the annotations (<<interface>> Name), the relations with their multiplicities and labels (A "1" --> "*" B : label), the lollipops, the namespaces, the notes (note "text", note for Name "text"), direction, classDef, cssClass, :::class, style, and the links of click and link.