The Six Types of Class relationships
There are six main types of relationships between classes: inheritance , realization / implementation , composition , aggregation , association, and dependency . The arrows for the six relationships are as follows:
Then we come to understand the specific content of the class relationship.
Six types of relationships
In the six types of relationships, the code structure of the three types of relationships such as composition , aggregation , and association is the same as using attributes to store the references of another class. Therefore, they must be distinguished by the relationship between the contents.
Inheritance is also called generalization and is used to describe the relationship between parent and child classes. A parent class is also called a base class, and a subclass is also called a derived class.
In the inheritance relationship, the subclass inherits all the functions of the parent class, and the parent class has all the attributes, methods, and subclasses. Subclasses contain additional information in addition to the same information as the parent class.
For example: buses, taxis, and cars are cars, they all have names, and they can all be on the road.
Realization / Implementation
Implementation (Implementation) is mainly used to specify the relationship between interfaces and implementation classes .
An interface (including an abstract class ) is a collection of methods. In an implementation relationship, a class implements an interface, and methods in the class implement all methods of the interface declaration.
For example: cars and ships are vehicles, and the vehicle is just an abstract concept of a mobile tool, and the ship and the vehicle realize the specific mobile functions.
Composition: The relationship between the whole and the part, but the whole and the part cannot be separated .
The combination relationship represents the relationship between the whole and part of the class, and the overall and part have a consistent lifetime. Once the overall object does not exist, some of the objects will not exist, and they will all die in the same life.For example, a person is composed of a head and a body. The two are inseparable and coexist.
Aggregation: The relationship between the whole and part, and the whole and part can be separated.
Aggregate relations also represent the relationship between the whole and part of the class, member objects are part of the overall object, but the member object can exist independently from the overall object.
For example, bus drivers and work clothes and hats are part of the overall relationship, but they can be separated. Work clothes and hats can be worn on other drivers. Bus drivers can also wear other work clothes and hats.
Association: Indicates that a property of a class holds a reference to an instance (or instances) of another class .
Association is the most commonly used relationship between a class and a class, which means that there is a connection between one type of object and another type of object. Combinations and aggregations also belong to associative relations , but relations between classes of affiliations are weaker than the other two.
There are four kinds of associations : two-way associations , one-way associations , self-association , and multiple-number associations .
For example: cars and drivers, one car corresponds to a particular driver, and one driver can drive multiple cars.
In UML diagrams, bidirectional associations can have two arrows or no arrows , and one-way associations or self-associations have an arrow .
In a multiplicity relationship, you can add a number directly to the associated line to indicate the number of objects in the corresponding class.
m..n: at least m, at most n (m<=n)
Dependence: Assume that a change in class A causes a change in class B, then say that class B depends on class A.
In most cases, dependencies are reflected in methods of a class that use another class’s object as a parameter .
A dependency relationship is a “use” relationship. A change in a particular thing may affect other things that use it, and use a dependency when it is necessary to indicate that one thing uses another.For example: The car relies on gasoline. If there is no gasoline, the car will not be able to drive.
Class Diagram – Order System
The class diagram below models a customer order from a retail catalog. The central class is the Order. Associated with it are the Customer making the purchase and the Payment. A Payment is one of four kinds: Cash, Check, Credit or Wire Transfer. The order contains OrderDetails (line items), each with its associated Item.
Class Diagram with User-Defined Constraint
Edit this Template
Among the six types of relationships, the code structure of combination, aggregation, and association is the same, and it can be understood from the strength of the relationship. The order from strong to weak is: inheritance → implementation → composition → aggregation → association → dependency . The following is a complete UML diagram.