UML Class Diagram Cheat Sheet

In UML

Unified Modeling Language (UML) is a widely used modeling language in software engineering. It is used to visualize, design, and document software systems. One of the most popular UML diagrams is the class diagram.

A class diagram is a graphical representation of the classes, interfaces, and their relationships in a system. It shows the static structure of the system and the objects that exist in the system. The class diagram is used to describe the attributes and methods of the classes and their relationships with other classes.

The class diagram consists of three main components: classes, associations, and multiplicity. A class is represented by a rectangle with the class name written inside it. The attributes and methods of the class are listed below the class name. Associations are represented by lines connecting two classes. The multiplicity of the association is shown at the ends of the line.

The class diagram is an important tool for software developers as it helps them to understand the structure of the system and the relationships between the classes. It is also useful for communication between developers and stakeholders as it provides a visual representation of the system.

In conclusion, the class diagram is a fundamental UML diagram that is used to represent the static structure of a system. It is an essential tool for software developers to design and document software systems.

Basic Elements

ElementDescription
ClassA blueprint for creating objects that defines its attributes and methods
ObjectAn instance of a class
AttributeA property of a class that describes its state
MethodA function of a class that defines its behavior
AssociationA relationship between two classes
MultiplicityThe number of instances of a class that can be associated with another class
InheritanceA relationship between a superclass and a subclass
PolymorphismThe ability of an object to take on many forms

Class Relationships

RelationshipDescription
AssociationA relationship between two classes where one class uses the other class
AggregationA special form of association where one class is a part of another class
CompositionA stronger form of aggregation where the part cannot exist without the whole
InheritanceA relationship between a superclass and a subclass where the subclass inherits the attributes and methods of the superclass
InterfaceA contract that specifies the methods that a class must implement

Class Diagram Notation

NotationDescription
Class NameThe name of the class
AttributesThe properties of the class
MethodsThe functions of the class
VisibilityThe access level of the attribute or method (public, private, protected)
Abstract ClassA class that cannot be instantiated and must be subclassed
InterfaceA contract that specifies the methods that a class must implement
InheritanceA relationship between a superclass and a subclass
MultiplicityThe number of instances of a class that can be associated with another class
AssociationA relationship between two classes
AggregationA special form of association where one class is a part of another class
CompositionA stronger form of aggregation where the part cannot exist without the whole

Examples

Class Diagram for a Bank Account

+-----------------+
|   BankAccount   |
+-----------------+
| -balance: double|
+-----------------+
| +deposit(amount: double): void|
| +withdraw(amount: double): void|
+-----------------+

Class Diagram for a Car

+---------+
|   Car   |
+---------+
| -make: string|
| -model: string|
| -year: int|
+---------+
| +start(): void|
| +stop(): void|
+---------+

Class Diagram for a Library

+-----------+
|  Library  |
+-----------+
| -books: Book[]|
+-----------+
| +addBook(book: Book): void|
| +removeBook(book: Book): void|
+-----------+

+-------+
|  Book |
+-------+
| -title: string|
| -author: string|
| -isbn: string|
+-------+
| +getTitle(): string|
| +getAuthor(): string|
| +getISBN(): string|
+-------+

Reference:

https://3dcitydb-docs.readthedocs.io/en/release-v4.1/3dcitydb/uml/

#