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:
+denotespublicattributes or method-denotesprivateattributes or method#denotesprotectedattributes 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 :
1Only 10..1Zero or One1..*One or more*Manyn n0..nzero to n1..none 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 Adepends onclass B(The edition ofclass Bwill 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 Adepends onclass Bobject Acan 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 Adepends onclass Bobject Acan visit info inobject Bobject Ais constructed byobject Bobject Ado not controls the life-circle ofobject B(object Bcan 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 Adepends onclass Bobject Acan visit info inobject Bobject Ais constructed byobject Bobject Acontrols the life-circle ofobject B(object Bcan 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 Adepends onclass Bobject Acan be seen asobject Bmethod 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 Adepends onclass Bobject Acan be seen asobject Bclass Ainherits interfaces and implementations inclass Band 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