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

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 communication between software designers more concise, further shortening 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 including 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, and 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 character 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:

[Visibility] Property name: Type [=default value]

For example: -name:String

Note: "Visibility" means whether the attribute is visible to elements outside the class, including 4 types of public (Public), private (Private), protected (Protected) and friend (Friendly). Use the symbols +, respectively in the class diagram -, #, ~ means.

(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 (parameter list)[:return type]

For example: +display():void.

The following figure shows the UML representation of the 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.

The figure below shows the UMDL representation of the graphical interface.
Insert picture description here

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 by a certain programming language. Class diagrams are effective throughout the entire life cycle of software system development. It is the most common diagram in object-oriented system modeling. The following figure 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 the access class to call.
Insert picture description here

Relationship between 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 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 use relationship, it is the weakest way of 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 the UML class diagram, the dependency relationship is represented by a dotted line with an arrow, and the arrow points from the used class to the dependent class. The figure below shows the relationship between people and mobile phones. People make calls through the voice transmission method of mobile phones.
Insert picture description here

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 composition 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. The following figure 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.
Insert picture description here

3. Aggregation

Aggregation relationship is a kind of association relationship, it is a strong association relationship, the relationship between the whole and the part, and the 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. The figure below shows the relationship between universities and teachers.
Insert picture description here

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. The figure below shows the relationship between the head and the mouth.

Examples of combination relationships
Insert picture description here

5. Generalization relationship

The Generalization relationship is the most highly coupled relationship between objects, representing the general and special relationship, the relationship between the parent class and the child class, 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. The class diagram is shown in the figure below.

Examples of generalization relations
Insert picture description here

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 dotted 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 their class diagrams are shown in the figure below.

Examples of realizing relationships
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46864744/article/details/112799163