** UML & Object relationships**⚓︎
Let's see how to express OOP by using UML class diagram. With UML we then import the relationship between classes.
UML⚓︎
Unified Modeling Language (UML)
is a type of static structure diagram used to show the structure of a system, including:
- Classes
- Attributes (Properties)
- Methods (Operations)
- Relationships (between objects)
1.1 Classes⚓︎
How to express class
?
A class
in UML
should encapsulate the class name, state (attributes), and behavior (methods).
class BankAccount{
public:
int m_ownerID;
float m_balance;
public:
int Deposit(int amount){...};
int Withdrawal(int ID){...};
}
classDiagram
class BankAccount{
+m_ownerID : int
+m_balance : float
+Deposit(amount : int) int
+Withdrawal(amount : int) int
}
classDiagram
class BankAccount
BankAccount : +m_ownerID
BankAccount : +m_balance
BankAccount : +Deposit()
BankAccount : +Withdrawal()
1.2 Visibility⚓︎
How to set visibility of attribute and method?
We can change the symbol before the attributes or methods as below:
+
denotespublic
attributes or method-
denotesprivate
attributes or method#
denotesprotected
attributes or method
classDiagram
class BankAccount{
-m_ownerID : int
#m_balance : float
+m_age : int
}
1.3 Multiplicity on relations⚓︎
How to express multiplicity between classes?
Multiplicity or cardinality in class diagrams indicates the number of instances of one class that can be linked to an instance of the other class.
The different cardinality options are :
1
Only 10..1
Zero or One1..*
One or more*
Manyn n
0..n
zero to n1..n
one to n
classDiagram
direction TB
Customer "1" --> "*" Ticket
Student "1" --> "1..*" Course
Galaxy --> "many" Star : Contains
1.4 Annotations on classes⚓︎
How to add annotations on classes?
Some common annotations include:
<<Interface>>
To represent an Interface class<<Abstract>>
To represent an abstract class<<Service>>
To represent a service class<<Enumeration>>
To represent an enum
classDiagram
direction TB
class A{
<<interface>>
}
class B{
<<Abstract>>
}
class C{
<<Service>>
}
class D{
<<Enumeration>>
}
Object relationships⚓︎
Now we have the UML
diagram as a tool for visualizing relationships. Let's look at the relationships between objects.
How to express relationships between classes?
Below are all the relationships used among classes.
-
Intuition: The behavior of one object depends on another object.
-
Iff:
class A
depends onclass B
(The edition ofclass B
will affectclass A
). -
E.g: A car (
A
) needs to have tires, an engine, and oil (B
) to work correctly.
classDiagram
direction LR
classA ..> classB : Dependency
-
Intuition: The relationship among multiple objects.
-
Iff:
class A
depends onclass B
object A
can visit info inobject B
-
E.g: An order form (
A
) associated with one customer object and multiple product objects (B
).
classDiagram
direction LR
classA --> classB: Association
-
Intuition: The relationship between the whole and the parts. The parts can survive without the whole.
-
Iff:
class A
depends onclass B
object A
can visit info inobject B
object A
is constructed byobject B
object A
do not controls the life-circle ofobject B
(object B
can survive withoutobject A
)
-
E.g: An order form (
A
) associated with one customer object and multiple product objects (B
).
classDiagram
direction LR
classA --o classB : Aggregation
-
Intuition: The relationship between the whole and the parts. The parts cannot survive without the whole.
-
Iff:
class A
depends onclass B
object A
can visit info inobject B
object A
is constructed byobject B
object A
controls the life-circle ofobject B
(object B
can not live withoutobject A
)
-
E.g: A car (
A
) needs to have tires, an engine, and oil (B
) to work correctly.
classDiagram
direction LR
classA --* classB : Composition
-
Intuition: The relationship between the interface and class implimentation.
-
Iff:
class A
depends onclass B
object A
can be seen asobject B
method A
(Methods defined in class A) is declared byinterface B
-
E.g: An interface (
B
) defines a set of methods that an implementation class implements (A
).
classDiagram
direction LR
classA ..|> classB : Realization
-
Intuition: The relationship between the interface and class implimentation.
-
Iff:
class A
depends onclass B
object A
can be seen asobject B
class A
inherits interfaces and implementations inclass B
and can extend them.
-
E.g: An animal (
B
) as a parent, dogs and cats as a subclass (A
) inherit some of the characteristics and behaviors of animals, while having their own unique characteristics and behaviors
classDiagram
direction LR
classA --|> classB : Inheritance
Reference⚓︎
Created: April 4, 2023 08:21:07