Design Pattern (2) — UML Class Diagram Introduction _ Von Jungle’s personal blog -CSDN blog _QT class diagram

Common relationships in UML class diagrams

In UML class diagrams, there are usually several relationships: Generalization, Realization, Association, Aggregation, Composition, and Dependency. In order of strength of relationships: generalization ≥ implementation > association > Aggregation > Composition > dependence.

Representation of UML classes

As shown in the figure, the class is represented by a rectangular box, which is divided into three layers:

  • Level 1: the name of the class; If the class is abstract, use italics;
  • Level 2: Class attributes, that is, member variables, [visibility] name: type [= default];
  • Layer 3: Class methods, that is, member functions, [visibility] names ([argument list]) [: return type].

Member variables and member methods are preceded by permission modifiers:

  • – : private: private
  • “+” : public: public
  • “#” : protected — protected

2. Relationships between classes

2.1. The Generalization (Generalization)

  • Meaning: Class inheritance relationship.
  • Representation: use hollow triangle and solid line, hollow triangle pointing to parent class.
  • Dog and cat are both animals.

2.2 implementation (Realization)

  • Meaning: The relationship between a class and an interface, indicating that the class is the implementation of all the characteristics and behaviors of the interface.
  • Represents: with hollow triangle and dotted line, hollow triangle points to interface.
  • For example, dogs and cats implement the animal interfaces “eat()” and “run()”.

2.3. Connection (Association)

An association is an ownership relationship (HAS) in which one class can call the public properties and methods of another class. Represented in a class as a member variable. For example, teachers have their own students, know the students’ names, student numbers and grades; Students have their own teachers and know their names and the subjects they teach. Associations can be classified into unidirectional association, bidirectional association and self-association.

2.3.1. One-way association

  • Meaning: a property
  • Representation: a straight line with an arrow.
  • Example: The Teacher class has its own Address.

2.3.2. Bidirectional association

  • Meaning: Both parties are aware of each other’s existence and can invoke each other’s public properties and methods.
  • Representation: Use a straight line to connect two classes, or use a two-way arrow.
  • For example, a Teacher has her own Student, and the Student has her own Teacher.

2.3.3. Since the link

  • Meaning: Quote yourself
  • Represents: a straight line with an arrow pointing at itself.
  • Example: binary tree structure

2.3.4. Multiplicity association

  • There are multiple associations between objects, as shown in the following table:

  • Representation: A straight line with an arrow pointing to a related object, which can be represented by a number or range of numbers on the associated line.
  • Example: A form interface object has multiple button objects

2.4. The Aggregation (Aggregation)

  • Meaning: The relationship between the whole and the parts, which can exist without the whole. Often used as a member variable of a class.
  • Represents: a straight line (or no arrow) with a hollow diamond and an arrow, with the diamond next to the whole and the arrow pointing to the part.
  • For example: a car and a tire. A tire is a part of a car, but a tire can also exist on its own.

2.5. Combination (Composition)

  • Meaning: The relationship between the whole and the part. The part cannot exist alone after leaving the whole. The object representing the whole is responsible for the life cycle of the object representing the part. Often used as a member variable of a class.
  • Represents: a straight line (or no arrow) with a solid diamond and an arrow, with the diamond next to the whole and the arrow pointing to the part.
  • For example, a company and a department. A department is a part of a company, but cannot exist independently.

2.6. Rely on (Dependency)

  • Meaning: a usage relation, that is, the implementation of one class needs the assistance of another class, often used for class method local variables, method parameters, etc.
  • Represents: a dotted line with an arrow pointing to the assisting class (in the example below, the arrow points to the Food class).
  • Example: Animal’s eat() method takes the parameter Food

2.7. Precautions

Note the distinction between association, aggregation and composition, and dependencies

  • Association represents the relationship between classes;
  • Aggregation and combination represent the relationship between the whole and the parts;
  • A dependency is a weak relationship, indicating that the implementation of a method of one class requires the use of another class, but there is no obvious relationship between the two.