The first diagram of requirements design is usually the use case diagram, and then someone asks me, what about the second diagram and the third diagram? Before starting this article, LET me list some UML views that are commonly used during the design phase:

As you can see, sequence diagrams and activity diagrams are views that are used at every design stage, from product managers to architects to low-level developers, and today is a good opportunity to talk about how to draw sequence diagrams.

An overview of the

Sequence Diagrams, the most common interaction Diagram in UML, show the dynamic collaboration state between multiple objects by describing the time Sequence of messages sent between objects.

The sequence diagram has slightly more elements than the use case diagram, such as Actors, objects, lifetimes, messages, Focus of Control, and so on.

As usual, we will not list a bunch of terms to explain, just a simple example, step by step to explain these functional modules and how to use them. For example, use our beggar version of the banking APP – see “Understanding UML Use Case Diagrams”; Due to space limitations, this article only covers login and balance query operations.

Actor/Object

The first step in a sequence diagram is to list all the roles and objects present in the interaction.

  • Roles: Usually refers to “people”, but can also be “abstract people” of organizations, machines, systems, and so on, represented by small figures, as in use case diagrams
  • Object: it is the “object” corresponding to the above “person”, including all products, services, equipment and other abstract objects

Interactions in a sequence diagram, usually emitted by the left-most Actor; In our bank APP case, the Actor is the customer; Let’s list the people and things from left to right, in the order they appear:

Your Lifeline

The second step of the sequence diagram is very mechanical: add a lifeline to each character and object. A lifeline is a dashed line that leads down from a character (or object) and represents the timeline on which the diagram exists.

Message (Message)

With the characters and lifelines, the basic framework of a sequence diagram is set up. We can then ask the customer to send the first message to the APP — log in:

Messages naturally come and go. The outgoing message is called Request, and the feedback message is called Response.

  • Request a solid arrow with a note above the arrow
  • The response is represented by a dashed arrow, and a simple return is added

Our login operation, as shown in the figure above, is a few small steps:

  1. The customer enters the user name and password and clicks login
  2. The APP sends the user name and password to Auth service for authentication
  3. The Auth service returns JWT after verification
  4. The APP displays login success to the customer

Combined Fragment

However, the login may also fail! In other words, add feedback when login fails to the sequence diagram. This is where you have to combine segments; Composite fragments are used to address conditional feedback in interactive execution. The combined fragments are relatively complex in length, as shown in the figure below:

There are more than a dozen kinds of combination fragments, and the most common one is the Alt fragment — the decision fragment, colloquially known as the if-else conditional combination. As the picture above shows:

  • The yellow area in the upper left corner will indicate the type of fragment
  • Segments are distinguished by dashed lines for different conditional subdomains
  • Then, in the upper left corner of the subfield — shaded in green — comment the conditional judgment
  • Finally, a specific response is returned on the respective conditional subdomain

Focus of Control

Control focus, also known as activation, is a thin rectangle over the lifeline, indicating that the object or role is active during this period of time. This is somewhat similar to the curly braces {} in the Java language — scope. Drawing activation is relatively mechanical and I usually save it for the end.

As can be seen from the figure above, the active state of APP is longer than that of Auth service; If you add in the timing activities related to the Bank service, you can get a better sense of the service runtime comparison. What’s the use of that? I’ll give you an answer in a later article.

summary

All right, so that’s all the basic elements, so let’s summarize a little bit. Sequence diagrams are one of the most frequently used design views among us developers. Everyone should draw a design view before implementing a new feature. Why is that? The fundamental reason is: the cost of drawing a design is much lower than code code, through the design of clear thinking, development is more convenient; In other words, we should get things done more economically. In addition, there are some dark reasons: the code is so long that the boss doesn’t have time to read it; You want to find your own sense of existence, you have to let him/her see simple and clear things, a picture is the best medium of information! All right, let’s go draw some pictures.

The article was posted simultaneously on An-Onion’s Github. Code word is not easy, welcome to like.