The relationship between class and class (detailed explanation is not finished)

Strong and weak relationship: dependence<association<aggregation<combination<implementation<inheritance

Dependency:

Simply understand, dependency is that one class A uses another class B, and this use relationship is accidental, temporary, and very weak, but the change of class B will affect class A. For example, if someone wants to cross a river and needs to borrow a boat, the relationship between people and boat is dependence. At the code level, class B is used as a parameter by class A in a method. In the UML class diagram design, the dependency relationship is represented by a dotted line with an arrow pointing from class A to class B.

 image.png

connection relation:

  The association reflects a strong semantic level of dependency between two classes. For example, my friend and I. This relationship is stronger than dependency, there is no chance of dependency, and the relationship is not temporary, it is generally long-term. Sexual, and the relationship between the two parties is generally equal. The association can be one-way or two-way. At the code level, the associated class B appears in the associated class A in the form of class attributes, or the associated class A references a global variable of the associated class B. In the UML class diagram design, the association relationship is represented by a solid line with arrows pointing from the association class A to the associated class B, and the roles and multiplicity marks of the associated parties can be marked at both ends of the association.

 

(1) One-way association

image.png

(2) Two-way association

image.png

Aggregation relationship:

Aggregation is a special case of association relationship, which embodies the relationship between the whole and the part, that is, the has-a relationship. At this time, the whole and the parts are separable, they can have their own life cycles, and the parts can belong to multiple integral objects, or they can be shared by multiple integral objects. For example, the relationship between computers and CPUs, companies and employees, for example, an aircraft carrier formation includes sea and air carriers, destroyers, carrier-based aircraft, and nuclear-powered attack submarines. At the code level, it is consistent with the association relationship and can only be distinguished from the semantic level. In the UML class diagram design, the aggregation relationship is represented by a hollow diamond and a solid arrow. 

image.png

Combination relationship:

Combination is also a special case of association relationship, which embodies a contains-a relationship, which is stronger than aggregation and is also called strong aggregation. It also reflects the relationship between the whole and the part, but at this time the whole and the part are inseparable, and the end of the life cycle of the whole means the end of the life cycle of parts, such as people and human brains. At the code level, it is consistent with the association relationship and can only be distinguished from the semantic level. In the UML class diagram design, the combination relationship is represented by a solid diamond and a solid arrow. 

image.png

Realize the relationship:

Implementation refers to the function that a class implements the interface interface (there can be multiple), and implementation is the most common relationship between a class and an interface. In Java, this kind of relationship is clearly identified by the keyword implements, and is generally not controversial in design. In the UML class diagram design, the realization is represented by a dashed line with a hollow triangle arrow, pointing from the class to the realized interface. 

Inheritance relationship:

 Inheritance refers to the ability of a class (called subclass, subinterface) to inherit the functions of another class (called parent class, parent interface), and to add its own new functions. In Java, the inheritance relationship is clearly identified by the keyword extends, which is generally not controversial in design. In the UML class diagram design, inheritance is represented by a solid line with a hollow triangle arrow, from the subclass to the parent class, or the subinterface to the parent interface. 

image.png

Guess you like

Origin blog.csdn.net/TGB_Tom/article/details/110351041