Class diagram (relationships between classes)

I. Overview

        A class diagram (Class diagram) shows the static structure of the model, especially the classes that exist in the model, the internal structure of the classes, and their relationship with other classes. Class diagrams do not display transient information. Class diagrams are a major component of object-oriented modeling. In software engineering, a class diagram is a static structural diagram that describes the collection of classes of the system, the attributes of the classes and the relationship between classes, which can simplify people's understanding of the system; the class diagram is the system analysis and design phase. An important product of the system is an important model for system coding and testing.

 1. class

       A class refers to the abstraction of objects with the same attributes, methods, and relationships. It encapsulates data and behavior and is the basis of object-oriented programming (OOP). It has three major characteristics: encapsulation, inheritance, and polymorphism. In UML, a class is represented by a rectangle containing the class name, attributes, and operations with separator lines.
(1) The class name (Name) is a string, for example, Student.
(2) Attribute (Attribute) refers to the characteristics of the class, that is, the member variables of the class. UML is represented in the following format:
     [Visibility]PropertyName:Type[=default]
For example: -name:String
Note: "Visibility" indicates whether the attribute is visible to elements outside the class, including public (Public), private (Private), protected (Protected) and friend (Friendly), respectively in the class diagram with symbols +, -, #, ~ represent.
(3) Operations are behaviors that can be used by any instance object of the class, and are member methods of the class. UML is represented in the following format:
[visibility] name(argument-list)[:return-type]
For example: +display(): void.
UML representation of the Student class.
2. Interface
Interface (Interface) is a special class that has a class structure but cannot be instantiated and can only be implemented by subclasses. It contains abstract operations, but no attributes. It describes the externally visible actions of a class or component. In UML, an interface is represented by a small circle with a name.
UML representation of a graphical class interface

 2. The relationship between classes and classes

In a software system, classes do not exist in isolation, and there are various relationships between classes. According to the degree of coupling between classes and classes from weak to strong, class diagrams in UML have the following relationships: dependency relationship, association relationship, aggregation relationship, composition relationship, generalization relationship and implementation relationship. Where generalization and implementation are equally coupled, they are the strongest.
1. Dependencies
     Dependency relationship is a usage relationship, which is the weakest coupling between objects, and it is a temporary relationship. In the code, a method of a class accesses some methods in another class (dependent class) through local variables, method parameters, or calls to static methods to complete some responsibilities. In UML class diagrams, dependencies are represented by dashed lines with arrows pointing from consuming classes to dependent classes. The figure below shows the relationship between people and mobile phones. People make calls through the voice transmission method of mobile phones.

 2. Relationship

       Association relationship is a reference relationship between objects, which is used to represent the connection between one type of object and another type of object, such as teacher and student, master and apprentice, etc. Association relationship is the most commonly used relationship between classes, which can be divided into general association relationship, aggregation relationship and combination relationship. We start with general associations. Association can be divided into one-way association, two-way association, self-association.
(1) One-way association
A one-way association is represented by a solid line with an arrow in a UML class diagram. The figure above shows that each customer has an address, which is realized by letting the Customer class hold a member variable class of type Address.
(2) Two-way association

 From the above figure, we can easily see that the so-called two-way association means that both parties hold member variables of each other's type.

In a UML class diagram, a bidirectional association is represented by a straight line without an arrow. In the figure above, a List<Product> is maintained in the Customer class, indicating that a customer can purchase multiple products; a member variable of the Customer type is maintained in the Product class to indicate which customer purchased the product.

(3) Self-association

 A self-association is represented in a UML class diagram by a line with an arrow pointing towards itself. The above picture means that the Node class contains member variables of type Node, that is, "self-contains itself".

3. Aggregation relationship

        An aggregation relationship is a kind of association relationship, a strong association relationship, and a relationship between a whole and a part. Aggregation is also implemented through member objects, where member objects are part of the overall object, but member objects can exist independently from the overall object. For example, the relationship between the school and the teacher, the school contains the teacher, but if the school is closed, the teacher still exists.
In a UML class diagram, an aggregation relationship can be represented by a solid line with a hollow diamond pointing towards the whole.
The following figure shows the relationship between universities and teachers:

4. Combination relationship 

        Composition represents a whole-part relationship between classes, but it is a stronger aggregation relationship. In the combination relationship, the overall object can control the life cycle of the partial object. Once the overall object does not exist, the partial object will also disappear, and the partial object cannot exist without the overall object. For example, the relationship between the head and the mouth, without the head, the mouth would not exist. In a UML class diagram, a composition relationship is represented by a solid line with a solid diamond pointing towards the whole.
The following figure shows the relationship between the head and the mouth:

5. Inheritance relationship 

        Inheritance relationship is the most coupled relationship between objects, expressing the general and special relationship, the relationship between the parent class and the child class, an inheritance relationship, and an is-a relationship. In UML class diagrams, inheritance relationships are represented by solid lines with hollow triangular arrows pointing from child classes to parent classes. When the code is implemented, use the object-oriented inheritance mechanism to realize the inheritance relationship. For example,
Both the Student class and the Teacher class are subclasses of the Person class, and their class diagram is shown in the figure below.

6. Realize relationships 

        The implementation relationship is the relationship between the interface and the implementing class. In this relationship, the class implements the interface, and the operations in the class implement all the abstract operations declared in the interface. In UML class diagrams, the implementation relationship is represented by a dashed line with a hollow triangular arrow pointing from the implementing class to the interface. For example, cars and boats implement vehicles, and their class diagrams are shown in the following figure. 

 

Guess you like

Origin blog.csdn.net/weixin_71243923/article/details/130301178