Cabbage Java study room covers core knowledge

UML Class Diagrams UML Sequence diagrams

1. Overview of UML Timing Diagrams

A Sequence Diagram, also known as a Sequence Diagram, Sequence Diagram, or Sequence Diagram, is a UML interaction Diagram. It shows dynamic collaboration between multiple objects by describing the chronological order in which messages were sent between them.

A sequence diagram represents an interaction as a two-dimensional diagram. The vertical axis is time, and time goes down a vertical line. The horizontal axis represents the class meta role of the individual objects in the collaboration. Class meta roles are represented by lifelines. The role is represented by a dotted line when the object is present, and the lifeline is a double-track line when the object’s process is active. Messages are represented by arrows from one object’s lifeline to another. The arrows are arranged in chronological order from top to bottom in the diagram.

2. UML sequence diagram function

  1. Show the order in which objects interact;
  2. The interaction behavior is modeled as message passing, and the interaction between objects is dynamically displayed by describing how messages are sent and received between objects.
  3. Compared with other UML diagrams, sequence diagrams emphasize the chronological order of interactions;
  4. Can visually describe the concurrent process.

3. UML sequence diagram elements

There are seven elements involved in drawing a sequence diagram: Actors, Objects, LifeLine, control focus (Activation), Messages, self-associated messages, and composed fragments.

The first six are the more common and important elements, and the remaining one is a combination of fragment elements that are less common but more complex. Let’s cover the first six elements first, and then the combined fragment elements separately.

Role (Actor)

System roles, which can be people or other systems, subsystems. Represented by a small person icon.

Object (Object)

The object is at the top of the sequence diagram, represented by a rectangle. Objects can be named in one of three ways:

  1. Object name and class name. For example: (huawei mobile phones:) or (loginServiceObject: LoginService);
  2. If only the class name is displayed and no object is displayed, it is an anonymous class. For example: (: cell phone) or (:LoginSservice);
  3. Only the object name is displayed, not the class name. For example :(huawei mobile phone 🙂 or (loginServiceObject :);

The LifeLine (LifeLine)

Each object in the sequence diagram has a vertical dashed line at the bottom center. This is the lifeline of the object (the time line of the object). In a vertical dotted line.

Control focus (Activation)

The control focus represents the operations performed in the sequence diagram at a certain time on the object’s timeline. It’s a very narrow rectangle.

The Message (the Message)

Represents information sent between objects. There are three types of messages.

  • Synchronous Message: The sender of a Message passes control to the receiver of the Message and then stops activity, waiting for the receiver of the Message to give up or return control. Used to indicate the meaning of synchronization. It’s a solid line plus a solid arrow.
  • Asynchronous Message: The sender of a Message passes a signal to the receiver of the Message through the Message and then continues its activity without waiting for the receiver to return the Message or take control. The receiver and sender of an asynchronous message work in parallel. It’s a solid line plus a greater than sign.
  • Return Message: A Return Message represents a Return from a procedure call. It’s indicated by a less than sign plus a dotted line.

Self-associated message

Represents a method calling itself or a method calling another method within an object. Represented by a semi-closed rectangle + solid cut head below.

Combination fragment

A composite fragment is used to address the conditions and manner of interactive execution. It allows logical components to be directly represented in a sequence diagram and is used to define special conditions and child processes for any part of any lifeline by specifying the application area of the condition or child process. There are 13 kinds of combination fragments, and their names and meanings are as follows:

4. Examples of commonly used combination segments

Choice (Alt)

Choice takes place in only one sequence in any given situation. You can set a threshold in each segment to indicate the conditions under which the segment can run. The criticality of else indicates the fragment to run if none of the other criticality is True. If all criticality is False and there is no else, no fragment is executed. The Alt fragment combination can be understood as if… else… Conditional statements.

Option (Opt)

Contains a sequence that may or may not occur. Opt is equivalent to if… Statements.

Loop (Loop)

A certain number of times a fragment is repeated can indicate the condition of fragment repetition in the critical. Loop is equivalent to a for statement.

Parallel (Par)

Parallel processing, events in a fragment can be interlaced in parallel. Par is equivalent to multithreading.

5. UML timing diagram drawing

  1. Draw boundaries and identify interactive contexts

The definition of boundary refers to the determination of the scope of the sequence diagram. In the example of wechat pay, the interactive messages such as opening wechat and entering the payment amount are omitted. These are not what we need to reflect. What we mainly reflect is the payment process of users’ scanning code.

The so-called recognition of interactive context is to know the premise and background of drawing the sequence diagram. In the example of wechat Pay, the premise is that the user logs in wechat and opens the payment function. The background is that the user needs to scan to pay for something.

  1. What are the roles and objects in the carding sequence diagram?

In the wechat Pay example, there is only one role: the user. The object is Huawei mobile phone: mobile phone, Android version of wechat: wechat, : merchants.

  1. What interaction messages are there between objects?

The messages that the objects interact with are shown in the above sequence diagram.

Techniques for drawing sequence diagrams

  1. Start with the initial message, then draw the subsequent messages in turn, and assign a sequence number to each message for easy understanding.
  2. Roles and objects are nouns, and messages are verbs.
  3. Roles are placed at the beginning of the sequence diagram, and objects are arranged from left to right in importance or frequency of use. This will be considered according to the process of time, is a more subjective thing.
  4. Both ends of the control focus should be capped with message elements, and the control focus should not exceed the message elements.

UML Class Diagrams UML Sequence diagrams