Introduction to the use of UML class diagrams

1. UML diagram

1. What is a UML diagram

The Unified Modeling Language (UML) is a visual modeling language for designing software. It is characterized by simplicity, unity, graphics, and the ability to express dynamic and static information in software design.

Starting from different perspectives of the target system, UML defines nine types of diagrams, including use case diagrams, class diagrams, object diagrams, state diagrams, activity diagrams, sequence diagrams, collaboration diagrams, component diagrams, and deployment diagrams.

2. Class Diagram 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.

3. The role of class diagrams

In software engineering, a class diagram is a static structural diagram that describes the collection of classes of the system, the attributes of classes and the relationship between classes, which can simplify people's understanding of the system;

The class diagram is an important product of the system analysis and design phase, and an important model for system coding and testing.

Second, the representation of the class

In a UML class diagram, a class is represented by a rectangle containing a class name, an attribute (field) and a method (method) with a dividing line. For example, the following figure represents an Employee class, which contains three attributes: name, age and address , and the work() method.
insert image description here
The plus sign and minus sign added before the attribute/method name indicate the visibility of this attribute/method. There are three symbols for visibility in UML class diagrams:

  • +: means public
  • -: Indicates private
  • #: means protected

The full representation of a property is: visibility-name:type[=default] The
full representation of a method is: visibility-name(parameter-list)[:return-type]

Note:
1. The content in the square brackets is optional.
2. There are also types that are placed in front of the variable name, and the return value type is placed in front of the method name.

give a chestnut

insert image description here
The Demo class above defines three methods:
method() method: the modifier is public, there are no parameters, and there is no return value.
method1() method: the modifier is private, there are no parameters, and the return value type is String.
method2() method: the modifier is protected, it receives two parameters, the first parameter type is int, the second parameter type is String, and the return value type is int.

3. Representation of the relationship between classes and classes

1. Relationship

An 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, husband and wife, 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

insert image description here

A unidirectional 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

insert image description here
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 above figure, 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

insert image description here
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".

2. 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:
insert image description here

3. 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:
insert image description here

The difference between composition relationship and aggregation relationship

Combination relationship: In the ancient emperor's Sangong and Liuyuan, there were many noble concubines, but each noble concubine only belonged to the emperor (with the same life cycle).
Aggregation relationship: A teacher has many students, but each student belongs to multiple teachers (with different life cycles).

4. Dependency

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 following figure shows the relationship between the driver and the car, and the driver drives the car:
insert image description here

5. Inheritance relationship

The inheritance relationship is the most coupled relationship between objects, expressing the general and special relationship, the relationship between the parent class and the subclass, and an inheritance relationship.

In a UML class diagram, a generalization relationship is represented by a solid line with a hollow triangular arrow pointing from a child class to a parent class. When implementing the code, use the object-oriented inheritance mechanism to realize the generalization relationship. For example, both the Student class and the Teacher class are subclasses of the Person class, and their class diagrams are shown in the following figure:
insert image description here

6. Achieve relationship

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 figure.

insert image description here

References

Detailed explanation of Java design patterns for UML and other
dark horse programmers, the most complete 23 Java design patterns in the entire network (diagram + framework source code analysis + actual combat)_哔哩哔哩_bilibili

Guess you like

Origin blog.csdn.net/A_art_xiang/article/details/130556158