Cabbage Java study room covers core knowledge

UML Class Diagrams UML Sequence diagrams

1. Overview of UML class diagram

The Class Diagram is the most commonly used and important Diagram in object-oriented system modeling. It is the basis for defining other diagrams. The class diagram is a static model to show the classes and interfaces in the system and the static structure and relationship between them. A class diagram shows a collection of classes, interfaces, associations, collaborations, and constraints. It is also known as a structure diagram.

Class diagrams are used not only to visually describe and document different aspects of a system, but also for building executable code for software applications. A class diagram describes a class of properties and operations, as well as constraints on the system. Are widely used in object-oriented systems for modeling class diagrams because they are unique and can map directly to UML diagrams in object-oriented languages.

2. UML class diagram target

The purpose of a class diagram is to provide a static view of an application of the model. Class diagrams are the only diagrams that map directly to object-oriented languages and are therefore widely used in the programming phase. Therefore, the purpose of class diagrams can be summarized as:

  • Analyze and design a static view of the application.
  • Describe the responsibilities of a system.
  • Base component diagram and deployment diagram.
  • Forward and reverse engineering.

3. How to draw a UML class diagram

UML class diagrams are a skill often needed in the software industry. Many project approval documents, requirements analysis and other documents, will be involved in the UML class diagram, so it is very important to learn how to draw the UML class diagram. There are many properties to consider when drawing a class diagram, and the diagram here will be viewed from the top level view. A class diagram is basically a graphical representation of a static view of a system, representing different aspects of the application. Thus, the collection class diagram represents the entire system. Keep the following points in mind when drawing class diagrams:

  • The names in the class diagram should be meaningful descriptions and be system-oriented.
  • The relationship between each element should be determined before drawing a class diagram.
  • Each class responsibility (attribute and method) in the class diagram should be clearly identified.
  • The minimum number of attributes for each class should be specified; unnecessary attributes will complicate the diagram.
  • Are the following comments required to describe certain aspects of the diagram? Because of the graphic above, it should be understandable to the developer/coder.
  • Finally, before the final version, the drawing should be drawn on plain paper as many times as possible to allow it to be corrected and reworked.

4. Actual operation of UML class diagram

Relationships between classes

In UML class diagrams, the following relationships are common: Generalization, Realization, Association, Aggregation, Composition, and Dependency.

When drawing class diagrams, it is important to clarify the relationships between classes. Let’s understand these relationships with examples.

Generalization

Generalization relationship: an inheritance relationship, representing a general versus special relationship, that specifies how a subclass specializes all characteristics and behaviors of its parent class. The tiger is a kind of animal, which has both the characteristics of the tiger and the commonness of the animal.

[Arrow point] : Solid line with triangular arrows pointing to the superclass.

Realization

[Implementation relationship] : It is the relationship between a class and an interface. The representation class is the implementation of all characteristics and behaviors of the interface.

[Arrow point] : the dotted line with a triangular arrow pointing to the interface.

Connection (Association)

Association: An ownership relationship that allows one class to know the properties and methods of another class. For example, the relationship between teacher and student, wife and husband can be two-way or one-way. A bidirectional association can have two or no arrows, and a unidirectional association has only one arrow.

[Arrow point] : A solid line with a normal arrow pointing to the owner.

In the picture above, the relationship between teacher and student is bi-directional. The teacher has multiple students, and the students may have multiple teachers. But the relationship between a student and a course is a one-way relationship, a student may take multiple courses, the course is an abstract thing he does not own the students.

Special situations may also be self-related, such as the self-related relationship of a bachelor

Aggregation

Aggregation: is the relationship between the whole and the parts, and the parts can leave the whole and exist alone. If the car and the tire are part and whole, the tire can still exist without the car. Aggregation relationship is a kind of association relationship, which is a strong association relationship. Association and aggregation are syntactically indistinguishable, and the specific logical relationship must be examined.

[Arrow pointing] : solid line with hollow diamond, diamond pointing to the whole.

Composition

Combinatorial relation: it is the relation between the whole and the parts, but the parts cannot exist alone without the whole. If the company and the department are the whole and part of the relationship, there is no company, there is no department. A composite relationship is a type of association relationship that is stronger than an aggregation relationship. It requires that an object representing the whole in a common aggregation relationship be responsible for representing the life cycle of a portion of the object.

[Arrow pointing] : solid line with solid diamond, the diamond pointing to the whole.

The difference between aggregation and composition

These two are difficult to understand, so let me focus on them. The difference between aggregation and composition is that the aggregation relationship is “has-a” and the composition relationship is “contains-a”. The aggregation relation means that the relation between whole and part is weak, while the combination relation is strong. The object that represents part of the object in the aggregation relationship has nothing to do with the lifetime of the object that represents the aggregate. Once the aggregate object is deleted, the object that represents part of the object is not necessarily deleted. Once the composition object is removed from the composition, the object representing the part of the thing is also removed.

Dependency

Dependency: A use relationship in which the implementation of one class requires the assistance of another class, so try not to use bidirectional interdependence.

[Arrow point] : dotted line with arrow, pointing to the user.

The order of strength of various relationships:

Generalization = Implementation > Composition > Aggregation > Association > Dependency

The following UML diagram shows the various class diagram relationships:

UML Class Diagrams UML Sequence diagrams