UML, short for Unified Modeling Language. UML diagrams are divided into 9 kinds of diagrams, such as use case diagram, class diagram, object diagram, state diagram, activity diagram, sequence diagram, collaboration diagram, component diagram and deployment diagram.
In object-oriented languages, we often see UML class diagrams to represent various interfaces and relationships between classes. However, every time I looked at the clouds, I couldn’t figure out what the dotted lines and arrows meant. Today, let’s find out.
There are six kinds of relations in UML class diagram, which are dependence relation, association relation, aggregation relation, composition relation, realization relation and generalization relation respectively.
Through my own understanding, I have drawn sample diagrams of six relationships. Class member variables and methods are preceded by public, private, protected, default, which are represented in UML class diagrams by +, -, #, ~, respectively.
1. Dependence
A dependency is a usage relationship that indicates that a class depends on another class, usually in the form of a method parameter of a class that uses an object of another class.
In UML class diagrams, dependencies are represented by dotted lines with arrows that point from the using class to the dependent class. As shown in the figure below, programmers rely on computers to write code.
2. Relevance
An association relationship is a reference relationship between objects, representing a relationship between one class and another class, such as teacher and student, husband and wife, etc.
Associations can be unidirectional or bidirectional. In UML class diagrams, one-way associations are represented by a solid line with an arrow pointing from the using class to the associated class, and bidirectional associations are represented by a solid line with or without an arrow.
The diagram above shows that the relationship between teacher and student is two-way. One teacher can have multiple students, and one student can have multiple teachers. There is a one-way relationship between the student and the course, a student will learn more than one course, while the course is an abstract concept, it does not own the student.
3. Aggregation relationship
Aggregation is a type of relational relationship, representing the relationship between whole and part, such as school and teacher, car and tire.
The aggregation relation in the class is reflected through the member object, the member is a part of the whole, the member can also exist without the whole. For example, the teacher is a part of the school, while the teacher is also an independent individual, can exist alone.
In UML class diagrams, aggregate relationships are represented by solid lines with hollow diamonds that point to the whole.
4. Combinatorial relations
Combinatorial relation is the relation between whole and part, and it is also a kind of correlation relation, which is stronger than aggregation relation. Some objects cannot exist independently of the whole object, such as the relationship between the human body and the brain, which cannot exist independently of the body.
In UML class diagrams, composition relationships are represented by solid lines with solid diamonds that point to the whole.
5. Realize relationships
An implementation relationship is a relationship between an interface and an implementation class. Class implements abstract methods in interfaces.
In UML class diagrams, implementation relationships are represented by dashed lines with hollow triangular arrows that point from the implementation class to the interface.
As shown in the figure above, Peking University and Tsinghua University have implemented university interfaces respectively.
6. Generalization
A generalization relationship is an inheritance relationship between parent and child classes. It represents the general and special relationships and specifies how subclasses specialize the characteristics and behavior of their parent classes.
In UML class diagrams, generalization relationships are represented by solid lines with hollow triangular arrows that point from the subclass to the parent class.
As shown in the picture above, the parent animals have a way of eating. Birds and lions have their own way of flying, while lions have their own way of running.
Among the six relationships, the order from weak to strong is: dependence < association < aggregation < combination < realization = generalization