Design pattern learning 02 (Java implementation) — UML diagram and class diagram representation

Time:2021-6-23

Write on the front

  • Take notes on learning design patterns
  • Improve the flexible use of design patterns

Learning address

https://www.bilibili.com/vide…

https://www.bilibili.com/vide…

Reference article

http://c.biancheng.net/view/1…

Project source code
https://gitee.com/zhuang-kang/DesignPattern

2. UML diagram

Unified Modeling Language (UML) is a visual modeling language for software design. Its characteristics are simple, unified, graphical, and can express the dynamic and static information in software design.

UML defines use case diagram, class diagram, object diagram, state diagram, activity diagram, sequence diagram, collaboration diagram, component diagram, deployment diagram and so on.

2.1 overview of class diagram

Class diagram shows the static structure of the model, especially the classes existing in the model, the internal structure of the class and their relationship with other classes. Class diagrams do not show transient information. Class diagram is the main part of object-oriented modeling.

2.2 function of class diagram

  • In software engineering, class diagram is a kind of static structure diagram, which describes the collection of classes, the attributes of classes and the relationship between classes, which can simplify people’s understanding of the system;
  • Class diagram is an important product of system analysis and design, and an important model of system coding and testing.

2.3 class diagram representation

2.3.1 class representation

In UML class diagram, a class is represented by a rectangle with a dividing line, which contains class name, field and method. For example, the following figure shows an employee class, which contains three properties, name, age and address, and the work () method.

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

The plus and minus signs in front of the property / method name indicate the visibility of the property / method. There are three kinds of symbols in UML class diagram to indicate the visibility:

  • +: denotes public
  • -: denotes private
  • : indicates protected

The complete representation of attributes is as follows:Visibility Name: type [= Default]

The complete representation of method is as follows:Visibility name (parameter list) [: return type]

be careful:

1. The content in brackets is optional
                                
2. There are also ways to put the type before the variable name and the return value type before the method name

Take chestnuts

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

The demo class above defines three methods:

  • Method () method: modifier is public, no parameter, no return value.
  • Method1 () method: modifier is private, no parameter, return value type is string.
  • Method2 () method: the modifier is protected, and two parameters are received. The first parameter type is int, the second parameter type is string, and the return value type is int.

2.3.2 expression of relationship between classes

2.3.2.1 relationship

Association relationship is a kind of reference relationship between objects, which is used to express the relationship between one kind of objects and another kind of objects, such as teachers and students, masters and apprentices, husbands and wives, etc. Association relationship is the most common relationship between classes, which is divided into general association relationship, aggregation relationship and combination relationship. Let’s start with general relevance.

Association can be divided into one-way Association, two-way Association and self association.

1. One way Association

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

In UML class diagram, one-way association is represented by a solid line with an arrow. The figure above shows that each customer has an address, which is realized by having the customer class hold a member variable class of type address.

2. Two way connection

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

It is easy to see from the figure above that the so-called two-way association is that both parties hold each other’s type of member variables.

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

3. Self correlation

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

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, it contains itself.

2.3.2.2 aggregation relationship

Aggregation relation is a kind of association relation. It is a strong association relation and a relation between whole and part.

Aggregation relationship is also realized by member object, which is a part of the whole object, but the member object can exist independently from the whole object. For example, the relationship between schools and teachers, schools include teachers, but if the school is closed, teachers still exist.

In UML class diagram, aggregation relationship can be represented by solid line with hollow diamond, which points to the whole. The following figure shows the relationship between universities and teachers

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

2.3.2.3 combination relationship

Composition represents the overall and partial relationship between classes, but it is a stronger aggregation relationship.

In the combination 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 diagram, composition relationship is represented by a solid line with a solid diamond, which points to the whole. The diagram below shows the relationship between head and mouth

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

2.3.2.4 dependency

Dependency relation is a kind of usage relation, which is the weakest coupling way between objects and is a temporary association. In the code, the method of one class can access some methods of another class (dependent class) through local variables, method parameters or static method calls to complete some responsibilities.

In UML class diagram, dependencies are represented by dashed lines with arrows pointing from the using class to the dependent class. The diagram below shows the relationship between driver and car

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

2.3.2.5 inheritance

Inheritance relation is a kind of relation with the largest coupling degree between objects. It represents the general and special relation, and it is a kind of inheritance relation between parent class and child class.

In UML class diagram, the generalization relationship is represented by a solid line with a hollow triangular arrow, which points from the subclass to the parent class. In the code implementation, the object-oriented inheritance mechanism is used to realize the generalization relationship. For example, the student class and the teacher class are subclasses of the person class, and the class diagram is as follows:

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation

2.3.2.6 implementation relationship

Implementation relationship is the relationship between interface and implementation 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 diagram, the implementation relationship is represented by a dotted line with a hollow triangular arrow, which points from the implementation class to the interface. For example, cars and ships implement vehicles, and their class diagram is shown in Figure 9.

Design pattern learning 02 (Java implementation) -- UML diagram and class diagram representation
Write at the end

  • If my article is useful to you, please give me some, thank you!
  • If you have any questions, please point them out in the comments area!