“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Class diagrams in UML and the relationships between class diagrams

Introduction to unified Modeling Language

Unified Modeling Language (UML) is a visual Modeling Language used to design software blueprints. In 1997, IT was adopted by OMG as an international standard for object-oriented Modeling languages. Its characteristics are simple, unified, graphical, can express the dynamic and static information in software design.

Unified Modeling Language (UML) provides modeling and visualization support for all phases of software development. Moreover, it integrates the new ideas, new methods and new technologies in the field of software engineering, making the communication between software designers more concise, further shortening the design time and reducing the development cost. Its application field is very wide, not only suitable for general system development, but also suitable for parallel and distributed system modeling.

UML defines nine diagrams including use case diagram, class diagram, object diagram, state diagram, activity diagram, sequence diagram, collaboration diagram, component diagram and deployment diagram from different points of view of target system.

This tutorial focuses on class diagrams, which are often used in software design patterns, and the relationships between classes. In addition, the lab section briefly introduces the use of UML modeling tools, currently the most widely used in the industry, Rational Rose. Umlet is also widely used as a lightweight, open source UML modeling tool for the development and design of small software systems.

Classes, interfaces, and class diagrams

class

A Class is an abstraction of objects with the same attributes, methods and relations. It encapsulates data and behaviors. It is the basis of OOP and has three characteristics, namely encapsulation, inheritance and polymorphism. In UML, classes are represented by delimited rectangles containing the class name, attributes, and operations.

The class Name (Name) is a string, for example, Student.

② Attributes refer to the characteristics of a class, that is, the member variables of a class. UML is expressed in the following format:

[Visibility] Attribute name: Type [= default]

For example: – name: String

Note: “Visibility” indicates whether the attribute is visible to elements outside the class, including Public, Private, Protected and Friendly. In the class diagram, the attribute is represented by symbols +, -, # and ~ respectively.

Operations are member methods of a class that can be used by any instance object. UML is expressed in the following format:

[Visibility] Name (parameter list)[: return type]

Example: +display():void.

Figure 1 shows a UML representation of the student class.

Figure 1 Student class

interface

An Interface is a special class that has the structure of a class but cannot be instantiated. It can only be implemented by subclasses. It contains abstract operations, but no properties. It describes the visible actions of a class or component. In UML, interfaces are represented by a small circle with a name.

Figure 2 shows a UMDL representation of the graphical class interface.

Figure 2 Graph interface

The class diagram

A ClassDiagram is a static model that shows the classes, interfaces, collaborations in a system, and the static structures and relationships between them. It is mainly used to describe the structural design of software system and help people to simplify the understanding of software system. It is an important product in the stage of system analysis and design, and also an important model basis for system coding and testing.

Classes in a class diagram can be implemented directly in a programming language. Class diagrams are valid throughout the life cycle of software system development and are the most common diagrams in object-oriented system modeling. Figure 3 shows a class diagram for “Calculate the perimeter and area of rectangles and circles”. The graphical interface has abstract methods for calculating the area and circumference. Rectangles and circles implement these two methods for access classes to call.

Figure 3. Class diagram of “Calculating the perimeters and areas of rectangles and circles”

Relationships between classes

In a software system, classes do not exist in isolation, and there are various relationships between classes. Class diagrams in UML have the following relationships according to the coupling degree between classes from weak to strong: dependency, association, aggregation, composition, generalization, and implementation. Where the coupling degree of generalization and implementation is equal, they are the strongest.

dependencies

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

In UML class diagrams, dependencies are represented by dotted lines with arrows that point from the using 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.

Figure 4 shows an example of a dependency

correlation

Association is a reference relationship between objects, used to represent the relationship between one type of objects and another type of objects, such as teachers and students, teachers and apprentices, husbands and wives, etc. Association relation is the most commonly used relation between classes, which can be divided into general association relation, aggregate relation and combinatorial relation. Let’s start with general associations.

The association can be bidirectional or unidirectional. In UML class diagrams, bidirectional associations can be represented as solid lines with two arrows or no arrows, and one-way associations are represented as solid lines with an arrow pointing from the using class to the associated class. You can also annotate role names at both ends of the association line to represent two different roles.

Associations are often implemented in code by treating objects of one class as member variables of another class. 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 bidirectional.

Figure 5. Example of an association relationship

The aggregation relationship

Aggregation is a kind of association relation. It is a strong association relation. It is the relation between whole and part, and it is the relation of HAS-A.

Aggregation relationships are also implemented through member objects, which are part of the overall object, but can exist independently of the overall object. For example, the relationship between the school and the teacher, the school includes the teacher, but if the school closes, the teacher still exists.

In UML class diagrams, aggregation relationships can be represented by solid lines with hollow diamonds that point to the whole. Figure 6 shows the relationship between the university and the faculty.

Figure 6 shows an example of an aggregation relationship

Combination relationship

Composition is also a kind of association. It is also the whole and part of the relationship between classes, but it is a stronger aggregation of cxmTAINes-A.

In combinatorial relation, 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 would not exist.

In UML class diagrams, composition relationships are represented by solid lines with solid diamonds that point to the whole. Figure 7 shows a head and mouth diagram.

Figure 7 shows an example of a composition relationship

Generalization relationship

Generalization relationship is the relationship with the maximum coupling degree between objects, and represents the general and special relationship, the relationship between the parent class and the subclass, and the Generalization relationship, which is the RELATIONSHIP of IS-A.

In UML class diagrams, generalization relationships are represented by solid lines with hollow triangular arrows that point from subclasses to parent classes. When the code is implemented, the object-oriented inheritance mechanism is used to realize the generalization relationship. For example, the Student and Teacher classes are subclasses of the Person class, and their class diagram is shown in Figure 8.

Figure 8. Example of a generalization relationship

Realize the relationship between

A Realization relationship is the relationship between an interface and an 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 diagrams, implementation relationships are represented by dashed lines with hollow triangular arrows that point from the implementation class to the interface. For example, cars and boats implement vehicles, and their class diagram is shown in Figure 9.

Figure 9 shows an example of an implementation relationship