What is UML class diagram

1. What is a UML class diagram

First quote a definition from Baidu Encyclopedia

Unified Modeling Language (UML) is a visual modeling language used to design software. It is characterized by simplicity, unity, graphics, and can express dynamic and static information in software design.
A class diagram is a static view that describes the classes in the system and the relationships between them. It allows us to have a comprehensive understanding of the system before writing the code correctly. A class diagram is a model type, to be precise, a static model type. Class diagrams represent classes, interfaces, and the cooperative relationships between them.

Haha, I believe you think this definition is definitely confusing, but if it is summarized in one sentence

UML class diagram is used to describe a relationship between classes and classes, between classes and interfaces, and between member variables of classes.

If you still don’t understand, let’s take a look at the UML class diagram
insert image description here
. There are also line connections. These lines describe the relationship between these interfaces and classes to us in a graphical way.

Second, why to understand UML class diagram

1. As a qualified developer, when writing code, you must deal with objects every day, understand the relationship between classes and classes, and the relationship between classes and methods, in order to design a good code structure, how to Understand the relationship between classes, then drawing UML class diagram is definitely your first choice, no problem
insert image description here

2. However, in most development, you may not face too complicated class associations, and sometimes you can understand them without drawing. However, if you want to further improve your technology, you will definitely come into contact with design patterns . In the in-depth study, you will face a large number of UML class diagrams. If you can't even understand UML class diagrams, you should be careful to be discouraged when learning design patterns.

The following figure is a class diagram of the case of the factory method pattern in Zen of Design Patterns. Can you guess what these symbols mean?
The Zen of Design Patterns

Three, class diagram representation

3.1 Representation of classes

In the 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 of name, age and address. , and the work() method.
insert image description here
The plus and minus signs before the property/method name indicate the visibility of the property/method. There are three symbols for visibility in UML class diagrams:

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

3.2 Representation of the relationship between classes

An association relationship is a reference relationship between objects, which is used to represent the connection between one class of objects and another class of objects , such as teachers and students, masters and apprentices, husbands and wives, and so on. Association relationship is the most commonly used relationship between classes, and it is divided into general association relationship, aggregation relationship and combination relationship . We start with general associations.

3.2.1 Association relationship

1. One-way association
A one-way association is represented by a solid line with an arrow in the UML class diagram. The above figure shows that each customer has an address, which is achieved by letting the Customer class hold a member variable class of type Address, which must be associated with an address object for the customer object.
insert image description here

2. Bidirectional Association
In the UML class diagram, the bidirectional association is represented by a straight line without an arrow. In the above figure, a List is maintained in the Customer class, indicating that a customer can buy multiple products; a member variable of the Customer type is maintained in the Product class to indicate which customer the product was purchased by.
A customer can buy multiple items, and items can be purchased by customers.
insert image description here

3. Self-association
Self-association is represented by a line with an arrow pointing to itself in the UML class diagram. The above figure means that the Node class contains member variables of type Node, that is, "self-contained". A linked list, for example, is a very good example of self-association.
insert image description here

3.2.2 Aggregation relationship

Aggregation is a type of association , a strong association, and a relationship between a whole and a part.

The aggregation relationship is also realized through member objects, which are part of the overall object, but the member objects can exist independently from the overall object. For example, the school-teacher relationship, the school includes the teacher, but if the school closes, the teacher still exists.

In a UML class diagram, an aggregation relationship can be represented by a solid line with an open diamond that points to the whole. The following figure shows the relationship between universities and teachers:
insert image description here

2.3.3 Combination relationship

Composition represents a whole-part relationship between classes , but it is a stronger aggregate relationship.

In the composition relationship, the whole object can control the life cycle of some objects. Once the whole object does not exist, some objects will not exist, and some objects cannot exist without the whole object. For example, the relationship between the head and the mouth, without the head, the mouth does not exist.

In UML class diagrams, compositional relationships are represented by solid lines with solid diamonds pointing towards the whole. The following figure shows the relationship between the head and the mouth:
insert image description here

2.3.4 Dependencies

Dependency is a usage relationship, which is the weakest way of coupling between objects and is a temporary association. In code, a method of a class performs some responsibilities by accessing certain methods in another class (dependent class) through local variables, method parameters, or calls to static methods .

In UML class diagrams, dependencies are represented by dashed lines with arrows from the consuming class to the dependent class. The diagram below shows the relationship between the driver and the car, with the driver driving the car:
insert image description here

2.3.5 Inheritance relationship

Inheritance relationship is a relationship with the largest degree of coupling between objects, representing a general and special relationship, a relationship between a parent class and a subclass, and an inheritance relationship.

In UML class diagrams, generalization relationships are represented by solid lines with hollow triangular arrows from subclasses to superclasses. In the code implementation, 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

2.3.6 Realization relationship

An implementation relationship is the relationship between an interface and an 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, implementation relationships are represented by dashed lines with hollow triangular arrows from the implementing class to the interface. For example, cars and boats enable transportation
insert image description here

Guess you like

Origin blog.csdn.net/qq_45171957/article/details/122645491