Introduction To UML
Unified Modeling Language (UML) is a modeling language that helps us to represent how the software is structured, and how different modules, classes and object interact with each other, and the relation between them.
UML is frequently used in association with object-oriented design, but it has a much broader scope.
In UML, we can define the structure and behavior of a system, and we can visualize the model or parts of it through diagrams. There are two types of diagrams.
- Structural Diagrams
- Behavior Diagrams
Structure diagrams are used to represent the structure of a system. There are many types of structure diagrams, but we are only interested in class diagrams. object, package, and component diagrams are similar to class diagrams.
Behavior diagrams are used to describe the behavior of a system. Interaction diagrams are a subset of behavior diagrams and are used to describe the flow of control and data among different components of a system. Among the behavior diagrams, the sequence diagram is used extensively in object-oriented design.
Class diagrams are the type of diagrams used most in object-oriented design and development stages. They are a type of structure diagram, and are used to illustrate the structure of classes and the relations among them.
classDiagram
class Vehicle {
+String number
+drive()
}
class Car {
+drive()
}
Vehicle <|-- Car
Class diagrams are useful for describing how the classes are structured in an application. Most of the time, just looking at the structure can be enough to be able to understand how the classes interact, but sometimes this is not enough. For those cases, we can use behavior and interaction diagrams, of which the sequence diagram is used to describe class and object interaction.
Example of Sequence Diagram
sequenceDiagram
objectA->>+objectB: getUser()
objectB-->>-objectA: user
Read more about UML Diagrams
Class Relations
In Object-Oriented Programming different notations are used to represent the relation between the classes entities.
Few are listed below
- Generalization
- Realization
- Dependency
- Association
- Aggregation
- Composition
Read more about UML Conventions
Generalization
Convention used to specify the inheritance is called generalization. Generalization is also called as is a
relation.
Generalization Example
classDiagram
Vehicle <|-- Car
Vehicle <|-- Truck
Realization
Convention used to specify the implementation of an interface.
Realization Example
classDiagram
Vehicle <|.. Car
Vehicle <|.. Truck
Dependency
When a class is dependent of another class or abstract class or interface one or the otherway, it is represented with dependency notation. The dependency can be a passing dependent object as parameter to the method of a class.
Dependency Example
classDiagram
Vehicle <.. Car
Association
Associate represents the relations between two class entities.
There are two types of entities.
Association Example
classDiagram
Driver <-- Car
Aggregation
When a class ClassA
has an instance of another class ClassB
, and classB
can exist without classA
then the relation is represented as aggregation. Aggregation is considered to be the has a
relationship while inheritance is considered to be the is a
relationship.
Aggregation Example
classDiagram
Department o-- Teacher
From the above class diagram, Department
class has Teacher
and Teacher
can exist without Department
.
Composition
When a class ClassA
has an instance of another class ClassB
, and classB
can not exist without classA
then the relation is represented as aggregation.
Composition Example
classDiagram
House *-- Room
From the above class diagram, House
class has Room
and Room
can not exist without House
.
Read more about UML Conventions