Krishna Nayak
Posted on October 18, 2024
Designing systems clearly and accurately is crucial, and one of the most commonly used tools for this purpose is UML (Unified Modeling Language) diagrams.
Among various types of UML diagrams, Class Diagrams are essential for visualizing the structure of a system.
So our first question should be what is UML Class Diagram?
UML Class Diagram
A UML Class Diagram is, basically a blueprint that describes the structure of a system by showing its classes, attributes, operations (or methods), and relationships among objects.
In simpler terms, it a visual representation of how objects and classes within a system interact with each other.
Now that we have an understanding of what a UML Class Diagram is, the next step is to know about Key Elements of a Class Diagram.
Key Elements of a Class Diagram
The Key Elements of class diagram include:
Class
The Class is the most fundamental element in a class diagram. It represents a blueprint for objects. Each class has three parts:
- Class Name: The name of the class.
- Attributes: The data stored in the class, often variables.
- Operations (Methods): Functions or actions that the class can perform.
Attributes
The properties or data of a class, typically shown with their visibility (public, private, etc.).
[visibility] name: type [multiplicity] = defaultValue
-
name
: The name of the attribute. type
: The data type of the attribute.-
Visibility defines the access level of attributes and methods within a class.
-
Public (+)
: The attributes and methods accessible from outside the class. -
Private (-)
: The attributes and methods accessible only within the class. -
Protected (#)
: The attributes and methods accessible within the class and its subclasses. -
Package (~)
: The attribute or method is accessible within the same package.
-
-
Multiplicity define, how many objects can participate in a relationship, such as "one-to-many" or "one-to-one."
-
1..*
means one or more objects. -
0..1
means zero or one object. -
1
Exactly one instance -
0..1
Zero or one instance -
1..*
One or more instances -
0..*
: Zero or more instances
-
default Value: (Optional) The initial value of the attribute.
Methods/Operations
Methods (or Operations) define the behavior of a class. They are the actions that the class can perform. In simple terms, it is the functions of the class.
[visibility] name(parameterList): returnType
-
name
: The name of the method/operation. -
parameterList
: A comma-separated list of parameters, each specified as name: type. -
returnType
: The data type returned by the method.
Relationships in UML Class Diagrams
Relationships show how classes are connected and interact with one another within a system.
There are six main types of relationships between classes:
- Association
- Aggregation
- Composition
- Inheritance
- Implementation
- Dependency.
Association
Association represents a general relationship between two classes where objects of one class interact with objects of another. It’s just like saying, "these two things are connected in some way."
Aggregation
Aggregation is a special type of association that represents a "whole-part" relationship. This is used when one class (the whole) is made up of other classes (the parts) that can exist independently. In other words, the parts (other classes) can exist without the whole (that one class), so removing the whole doesn’t destroy the parts.
Composition
Composition is a stronger form of aggregation and represents a "whole-part" relationship as well. However, in composition, the parts cannot exist without the whole. This means that if the whole is deleted, the parts are also deleted because they depend on the whole for their existence.
Inheritance (Generalization)
Inheritance, also known as Generalization, like a “parent-child” relationship between two classes. In this relationship, one class (the child or subclass) inherits attributes and behaviors from another class (the parent or superclass). This helps reuse code and avoid duplication, as the child class automatically gets the characteristics of the parent class.
Implementation
Implementation is a relationship between a class and an interface. An interface defines abstract methods that a class must implement, but it doesn’t provide the actual code. When a class implements an interface, it agrees to provide specific functionality defined by that interface.
Dependency
Dependency is a relationship where one class relies on another class to function. It’s often temporary, meaning the dependency exists only when the operation is happening and doesn’t form a permanent relationship between classes.
Summary Table for Relationship in UML Class Diagrams:
Relationship | Description | Notation |
---|---|---|
Association | General connection between classes, without ownership. | Solid line |
Aggregation | #Whole-part relationship where parts can exist independently of the whole. | Dashed line with an open diamond on the “whole” side |
Composition | #Whole-part relationship where parts cannot exist independently of the whole. | Solid line with a filled diamond on the “whole” side |
Inheritance | Parent-child relationship where the child inherits properties and behaviors from parent. | Solid line with an filled arrow to the parent class |
Implementation | Class-interface relationship, where the class agrees to fulfill the interface's actions. | Dashed line with a filled arrow to the interface |
Dependency | Temporary relationship where one class depends on another for a specific action. | Dashed line with an arrow from the dependent class |
# Whole refer to one class and Part refer to the made up of other class/classes. For example, A
House class
contains aRoom class
in a composition relationship — if the House is demolished, the Room is also demolished.
UML Class Diagrams also support some special elements like Enums
, Interfaces
, and Abstract Classes
. These are denoted as show in the image.
Combined Example [library system UML class]
Element | Type | Relationships |
---|---|---|
User | Class | - Generalization: Member and Librarian are specialized versions of User. |
Member | Class | - Implements Borrowable interface. - Aggregation with Book (A member can borrow multiple books). |
Librarian | Class | - Association with Library (A librarian manages one library). |
Book | Class | - Aggregation with Genre (Each book belongs to a specific genre). - Association with Author (Each book has one or more authors). |
Author | Class | - Association with Book (An author writes one or more books). |
Library | Class | - Composition with Catalog (A library contains a catalog). - Aggregation with Book (A library contains many books). - Aggregation with Member (A library can have multiple members). |
Catalog | Class | - Composition with Library (The catalog is part of the library and can't exist without it). - Aggregation with Book (The catalog manages a collection of books). |
Borrowable | Interface | - Implemented by Member (A member can borrow books). |
Genre | Enumeration | - Aggregation with Book (Each book has a genre). |
As become more familiar with UML Class Diagrams, we can gain a toolset to design more complex systems and communicate these designs effectively with other developers. With the user UML, we can create a blueprint that is clear and organized, making it easier to adapt in the future and supporting software that can grow and be maintained over time.
Posted on October 18, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.