Learning Notes of UML Class Diagram
in Note with 2 comments
Learning Notes of UML Class Diagram
in Note with 2 comments

Class Diagram is used to describe the classes contained in the system and their relationships, and help people simplify their understanding of the system. It is an important product of the system analysis and design phase, and also an important model basis for system coding and testing.

class

Class, which encapsulates data and behavior, is an important part of object-oriented. It is the general name of the collection of objects with the same attributes, operations, and relationships. In the system, each class has certain responsibilities. Responsibilities refer to what functions a class should perform and what obligations it should assume. A class can have multiple responsibilities, and a well-designed class generally has only one responsibility. When defining a class, the responsibilities of the class are decomposed into the properties and operations (i.e. methods) of the class. The attribute of a class is the data responsibility of the class, and the operation of a class is the behavior responsibility of the class. Design class is the most important part of object-oriented design, and it is also the most complex and time-consuming part.

When the software system is running, the class will be instantiated into an object, which corresponds to a specific thing and is an instance of the class.

Class Diagram uses different classes that appear in the system to describe the static structure of the system. It is used to describe different classes and their relationships.

In the system analysis and design phase, classes can generally be divided into three types, namely, Entity Class, Control Class and Boundary Class. The following is a brief description of these three types:

(1) Entity class: The entity class corresponds to each entity in the system requirements. They usually need to be saved in the permanent storage, and are generally recorded using database tables or files. Entity classes include both classes that store and transfer data, and classes that operate data. Entity classes are derived from nouns in the requirement description, such as students, commodities, etc.

(2) Control class: the control class is used to reflect the execution logic of the application, provide corresponding business operations, and abstract the control class to reduce the coupling between the interface and the database. The control class is generally a noun converted from a verb object phrase (verb+noun). For example, adding a commodity corresponds to a commodity adding class, and registration corresponds to a user registration class.

(3) Boundary class: boundary class is used to abstract the interaction objects between external users and the system, mainly including interface classes, such as dialog boxes, windows, menus, etc.

In the initial stage of object-oriented analysis and design, entity classes are usually identified first and initial class diagrams are drawn. At this time, class diagrams can also be called domain models, including entity classes and their relationships.

Class relationship

Generalized relation

The inheritance structure of classes is shown in UML as: generalize and realize:

The inheritance relationship is is-a; If two objects can be represented by is-a, it is an inheritance relationship: (.. Is..)

Eg: Bicycles are cars, cats are animals

The generalization relation is directly represented by a hollow arrow; As shown in the figure below (A inherits from B);

 1gen.png

Note: In the final code, the generalization relationship is shown as inheriting non abstract classes

Realizing relationships

The implementation relationship is represented by a dotted line with a hollow arrow;

Eg: "Car" is an abstract concept, which cannot be directly used to define objects in reality; Only specific subclasses (cars or bicycles) can be used to define objects ("cars" are represented by abstract classes in C++, and interfaces are easier to understand in Java)

 1rea.png

Note: In the final code, the implementation relationship is represented as an inheritance abstract class

Aggregation relation

The aggregation relationship is represented by a straight line with a hollow diamond arrow, as shown in the figure below, indicating that A is aggregated to B, or B is composed of A

 1agg.png

Aggregation relationship is used to represent the relationship between entity objects, and to represent the semantics that the whole is composed of parts; For example, a department consists of multiple employees;

Different from the combination relationship, the whole and the part are not strongly dependent. Even if the whole does not exist, the part still exists; For example, if the department is cancelled, the personnel will not disappear, but they still exist;

synthetic relation

The combination relationship is represented by a straight line with a filled diamond arrow, as shown in the figure below, indicating that A constitutes B, or B is composed of A;

 1com.png

Like the aggregation relationship, the combination relationship also represents the semantics of the whole composed of parts; For example, the company consists of multiple departments;

However, the combination relationship is a special aggregation relationship with strong dependency. If the whole does not exist, then the part does not exist; For example, if the company does not exist, the department will not exist;

Association

The association relationship is represented by a straight line; It describes the structural relationship between objects of different classes; It is a static relationship, usually independent of the running state, and generally determined by common sense and other factors; It is generally used to define the static and natural structure between objects; Therefore, the association relationship is a "strong association" relationship;

For example, there is an association between passengers and tickets; Students and schools are related;

The association relationship does not emphasize direction by default, which means that objects know each other; If the direction is especially emphasized, as shown in the figure below, it means that A knows B, but B does not know A;

 1ass.png

Note: In the final code, associated objects are usually implemented in the form of member variables;

Dependencies

Dependencies are represented by a set of dotted lines with arrows; As shown in the figure below, A depends on B; He describes the relationship between an object and another object during operation;

 1dep.png

Different from the association relationship, it is a temporary relationship, usually generated during the run time, and changes with the run time; Dependencies may also change;

Obviously, dependency also has a direction. Bidirectional dependency is a very bad structure. We should always maintain one-way dependency and eliminate the occurrence of two-way dependency;

Note: In the final code, the dependency relationship is reflected in the class construction method and the incoming parameters of the class method, and the arrow points to the call relationship; Dependency processing temporarily knows whether to "use" the methods and attributes of the other party or not;

reference resources

design-patterns.readthedocs.org/zh_CN/latest/read_uml.html

www.ibm.com/developerworks/cn/rational/r-uml/

Responses
  1. I have also learned UML diagrams before, but I feel it is difficult. I feel that it is easier to understand the design pattern directly than to see UML diagrams.. Do you often use UML diagrams?

    Reply
    1. @Xiaoyan

      I use it for my graduation project/ flee

      Reply