In our daily study/work, we often face the following scenarios:

  1. Read other people’s code
  2. Read the framework source code
  3. Read the code you wrote a long time ago.

Don’t think of your job as just writing code, you’ll find that most of your time is actually spent reading and understanding existing code.

I often use sequence diagrams to get a faster and clearer picture of how objects are called. Hand-animated sequence diagrams are still cumbersome and time consuming, but IDEA provides a plugin called SequenceDiagram to help us solve this problem. With the SequenceDiagram plugin, we can generate sequence diagrams with one click.

What are sequence diagrams?

There are many definitions of sequence diagrams on the Internet, but I think they are too abstract to understand. The most amazing thing is that most of the articles have exactly the same definition of sequence diagrams, which seems to give full play to the “essence” of writing code.

Let me briefly explain my understanding. However, to be honest, I am not very clear about the Sequence Diagram myself. Please also point out any problems or improvements in the following description.

Sequence Diagram, also known as Sequence Diagram, is a KIND of UML behavior Diagram. Represents the sequential invocation relationship between objects when the system performs a method/operation, such as a login operation.

The sequential invocation relationship can be understood as follows: you need to execute the method/operation login provided by object A in the system, but this object relies on the method getUser provided by object B. Therefore, there is a relationship between a -> B calls.

Two more examples!

Below is the business process sequence diagram of wechat Payment. This chart describes the relevant roles of wechat Pay (customers, merchants…). In the wechat payment scenario, the sequential call relationship between basic payment and payment.

 

Below is a sequence diagram I wrote for executing a method in an HTTP framework. This diagram describes the class relationships involved when calling the loadInterceptors() method of the InterceptorFactory class.

 

In addition, sequence diagrams are generally called “sequence diagrams” in China.

  • If you look at it purely in terms of translation, the word sequence does not mean “time”, but only sequence, so some people say that the term “sequence diagram” is not accurate.
  • From a definition point of view, this description is fine. Because the Sequence Diagram does execute the trigger timing of each message in chronological order.

I think it’s ok to call a Sequence Diagram or Sequence Diagram.

In what scenarios do you need to view a sequence diagram of a class?

We need sequence diagrams in many scenarios, such as:

  1. Read the source codeWhen reading the source code, you may want to look at the call relationships of the related classes involved in calling the target method. This is especially useful for understanding source code when the code has many invocation levels. (Off-topic: In practice, most of our time is actually spent reading and understanding existing code.
  2. Technical documentation: In order to make your code easier to understand when writing the project documentation, you need to generate sequence diagrams for the related classes based on the core methods to show how they call each other.
  3. Sorting out business processes: When our system business processes are more complex, we can use sequence diagrams to visualize the relationships between important roles and objects involved in the system.
  4. .

How to use IDEA to generate sequence diagrams based on methods in a class?

With the SequenceDiagram plugin, we can generate sequence diagrams with one click.

Also, you can:

  1. Click on the class/method in the sequence diagram to jump to the corresponding location.
  2. Removes the corresponding class or method from the sequence diagram.
  3. Export the generated sequence diagram as a PNG image.

The installation

We can find this plugin directly in the plugin market of IDEA. I have it installed here.

If you cannot use the plugin market of IDEA due to network problems, you can also download and install the plugin market manually from the official website of IDEA.

 

Simple to use

  1. Select the method name (be careful not to select the class name), then right click and select the Sequence Diagram option!

 

  1. After configuring some of the basic parameters of the generated sequence diagram, such as the call depth, we click OK!

 

You can also use the generated sequence diagram to locate the relevant code, which is especially helpful when reading the source code!

 

Once the sequence diagram is generated, you can also choose to export it as an image.