TOP3. Class diagrams in UML and the relationship between class diagrams

                                  Class diagrams in UML and the relationship between class diagrams

Introduction to Unified Modeling Language

Classes, interfaces and class diagrams

1. Class

2. Interface

3. Class diagram

Relationship between classes

1. Dependency

2. Association

3. Aggregation

4. Combination relationship

5. Generalization

6. Realize the relationship


Introduction to Unified Modeling Language


Unified Modeling Language (UML) is a visual modeling language used to design software blueprints. It was adopted by the International Object Management Group (OMG) in 1997 as an international standard for object-oriented modeling language. Its characteristics are simple, unified, graphical, and capable of expressing dynamic and static information in software design.

The unified modeling language can provide modeling and visualization support for all stages of software development. Moreover, it incorporates new ideas, new methods and new technologies in the field of software engineering to make the communication between software designers more concise, further shortening the design time and reducing development costs. It has a wide range of applications, not only suitable for the development of general systems, but also for modeling parallel and distributed systems.

From different perspectives of the target system, UML defines 9 diagrams such as use case diagrams, class diagrams, object diagrams, state diagrams, activity diagrams, sequence diagrams, collaboration diagrams, component diagrams, and deployment diagrams.

This tutorial mainly introduces the commonly used class diagrams in software design patterns and the relationships between classes. In addition, the experimental part will briefly introduce the use of UML modeling tools. The most widely used in the industry is Rational Rose. There are many people who use Umlet, it is a lightweight open source UML modeling tool, simple and practical, often used in the development and design of small software systems.

Classes, interfaces and class diagrams


1. Class

Class refers to the abstraction of objects with the same attributes, methods and relationships. It encapsulates data and behavior. It 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 and separated lines.

(1) The name of the class (Name) is a string, for example, Student.

(2) Attribute refers to the characteristics of the class, that is, the member variables of the class. UML is represented in the following format:

[可见性]属性名:类型[=默认值]
例如:-name:String

注意:“可见性”表示该属性对类外的元素是否可见,包括公有(Public)、私有(Private)、受保护(Protected)和朋友(Friendly)4 种,在类图中分别用符号+、-、#、~表示。

(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:

[可见性]名称(参数列表)[:返回类型]
例如:+display():void。

图 1 所示是学生类的 UML 表示。

Student class


                                                                                        Figure 1 Student class

2. Interface

Interface (Interface) is a special class that has the structure of a class but cannot be instantiated and can only be implemented by subclasses. It contains abstract operations, but not attributes. It describes the externally visible actions of a class or component. In UML, the interface is represented by a small circle with a name.

Figure 2 shows the UMDL representation of the graphical interface.


                                                                                        Figure 2 Graph interface

3. Class diagram

Class Diagram (ClassDiagram) is a static model used to display the classes, interfaces, collaborations, and their static structure and relationships in the system. It is mainly used to describe the structured design of a software system and help people simplify the understanding of the software system. It is an important product of the system analysis and design stage and an important model basis for system coding and testing.

The classes in the class diagram can be implemented directly in a certain programming language. Class diagrams are effective throughout the life cycle of software system development, and they are the most common diagram in object-oriented system modeling. Figure 3 shows the class diagram of "Calculating Perimeter and Area of ​​Rectangle and Circle". The graphic interface has abstract methods for calculating area and perimeter. Rectangle and circle implement these two methods for accessing classes to call.
 

Class diagram for "calculating perimeter and area of ​​rectangles and circles"


                                                              Figure 3 Class diagram for "calculating the perimeter and area of ​​rectangles and circles"

Relationship between classes


In a software system, classes do not exist in isolation, and there are various relationships between classes. According to the arrangement of the coupling degree between classes from weak to strong, the class diagram in UML has the following relationships: dependency, association, aggregation, composition, generalization, and realization. Among them, the degree of coupling between generalization and implementation is equal, and they are the strongest.

1. Dependency

Dependency (Dependency) relationship is a kind of use relationship, it is a kind of association method with the weakest coupling between objects, it is a temporary association. In the code, a method of a certain class uses local variables, method parameters, or calls to static methods to access certain methods in another class (dependent class) to perform some responsibilities.

In UML class diagrams, dependencies are represented by dashed lines with arrows, and the arrows point from the used class to the dependent class. Figure 4 shows the relationship between people and mobile phones. People make calls through the voice transmission method of mobile phones.
 

Examples of dependencies


                                                                               Figure 4 Examples of dependencies

2. Association

Association (Association) relationship is a kind of reference relationship between objects, used to represent the connection between one type of object and another type of object, such as teacher and student, teacher and apprentice, husband and wife, etc. Association relationship is the most commonly used relationship between classes, which is divided into general association relationship, aggregation relationship and combination relationship. We first introduce general associations.

The association can be bidirectional or unidirectional. In UML class diagrams, two-way associations can be represented by a solid line with two arrows or no arrows, and unidirectional associations are represented by a solid line with an arrow, and the arrow points from the used class to the associated class. You can also mark role names at both ends of the association line to represent two different roles.

In the code, objects of one class are usually used as member variables of another class to realize the association relationship. Figure 5 shows the relationship between teachers and students. Each teacher can teach multiple students, and each student can learn from multiple teachers. They are two-way related.
 

Examples of association


                                                                              Figure 5 Examples of association relationships

3. Aggregation

Aggregation relationship is a kind of association relationship, a strong association relationship, a relationship between a whole and a part, and a has-a relationship.

The aggregation relationship is also realized through member objects. The member objects are part of the overall object, but the member objects can exist independently of the overall object. For example, in the relationship between a school and a teacher, the school includes teachers, but if the school is closed, the teachers still exist.

In the UML class diagram, the aggregation relationship can be represented by a solid line with a hollow diamond, and the diamond points to the whole. Figure 6 shows the relationship between universities and teachers.
 

Examples of aggregation relationships


                                                                             Figure 6 Examples of aggregation relationships

4. Combination relationship

The composition relationship is also a kind of association relationship, and it also represents the relationship between the whole and the part between classes, but it is a stronger aggregation relationship, which is the cxmtains-a 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 will no longer exist.

In the UML class diagram, the combination relationship is represented by a solid line with a solid diamond, and the diamond points to the whole. Figure 7 shows the relationship between the head and the mouth.
 

Examples of combination relationships


                                                                             Figure 7 Example of combination relationship

5. Generalization

Generalization (Generalization) relationship is the most coupled relationship between objects, representing the general and special relationship, the relationship between the parent class and the subclass, the inheritance relationship, and the is-a relationship.

In the UML class diagram, the generalization relationship is represented by a solid line with a hollow triangle arrow, and the arrow points from the child class 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 both subclasses of the Person class, and the class diagram is shown in Figure 8.
 

Examples of generalization relations


                                                                              Figure 8 Examples of generalization relations

6. Realize the relationship

The Realization relationship is the relationship between the interface and the 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 the UML class diagram, the realization relationship is represented by a dashed line with a hollow triangle arrow, and the arrow points from the realization class to the interface. For example, cars and boats implement vehicles, and the class diagram is shown in Figure 9.
 

Examples of realizing relationships


                                                                       Figure 9 Example of the realization relationship

<GOF's 23 design modes                                                                                                                                                        opening and closing principles>

Guess you like

Origin blog.csdn.net/m0_38023584/article/details/106343695