A summary of several relationships in UML class diagrams
Reprinted: http://www.uml.org.cn/oobject/201609062.asp
U In the UML class diagram, there are the following common relationships: Generalization, Realization, Association, Aggregation, Composition, Dependency
1. Generalization
[Generalization relationship]: It is an inheritance relationship, representing a general-specific relationship, which specifies how a subclass specializes all the features and behaviors of the parent class. For example: Tiger is a kind of animal, that is, it has the characteristics of tiger and the commonality of animals.
[arrow pointing]: solid line with a triangular arrow, the arrow points to the parent class
2. Realization
[Implementation relationship]: It is a relationship between a class and an interface, indicating that the class is the implementation of all the features and behaviors of the interface.
【Arrow pointing】: Dotted line with triangular arrow, the arrow points to the interface
3. Association
[Association relationship]: It is an owned relationship, which enables one class to know the attributes and methods of another class; such as: teacher-student, husband-wife association can be bidirectional or unidirectional. Bidirectional associations can have two arrows or no arrows, and unidirectional associations have one arrow.
[code embodiment]: member variable
[Arrow and Pointing]: A solid line with a common arrow, pointing to the owner
In the above figure, the teacher and the student are two-way related. The teacher has multiple students, and the students may also have multiple teachers. However, the relationship between students and a course is a one-way association. A student may take multiple courses. The course is an abstract thing and he does not own students.
The following figure shows the self-association:
4. Aggregation
[Aggregation relationship]: It is the relationship between the whole and the part, and the part can exist independently of the whole. If the car and the tire are in a whole-part relationship, the tire can still exist without the car.
Aggregation relationship is a kind of association relationship and is a strong association relationship; association and aggregation cannot be distinguished grammatically, and specific logical relationships must be examined.
[code embodiment]: member variable
【Arrow and direction】: Solid line with hollow diamond, the diamond points to the whole
5. Composition
[Combination relationship]: It is the relationship between the whole and the parts, but the parts cannot exist independently of the whole. If a company and a department are a whole and a part, there is no department without a company.
The composition relationship is a kind of association relationship, which is stronger than the aggregation relationship. It requires the object representing the whole in the ordinary aggregation relationship to be responsible for the life cycle of the object representing the part.
[code embodiment]: member variable
【Arrow and direction】: Solid line with solid rhombus, the rhombus points to the whole
6. Dependency
[Dependency relationship]: It is a relationship of use, that is, the implementation of one class requires the assistance of another class, so try not to use two-way interdependence.
[Code performance]: local variables, method parameters, or calls to static methods
【Arrow and direction】: The dotted line with arrow points to the user
The order of strength and weakness of various relationships:
Generalization = Implementation > Composition > Aggregation > Association > Dependency
The following UML diagram vividly shows various class diagram relationships: