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

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

Don’t think your job is just writing code. When you do, you will find that most of your time is spent reading and understanding the code you already have.

I often use sequence diagrams in order to understand the calling relationships between objects more quickly and clearly. Hand animated sequence diagrams can be cumbersome and time-consuming, but IDEA provides a plugin called SequenceDiagram to help solve this problem. With the SequenceDiagram plug-in, we can generate sequence diagrams with one click.

What is a sequence diagram?

There are many definitions of sequence diagrams on the Internet, but I think they are not easy to understand because they are too abstract. What’s amazing is that most of the articles have exactly the same definition of sequence diagrams.

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

A Sequence Diagram, also known as a Sequence Diagram, is a 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 perform the method/operation login (login) provided by object a in the system, but that object in turn relies on the method getUser(getUser) provided by object b. Therefore, there is a call relationship between a -> B.

Here are two more examples!

The following figure is the business process sequence diagram of wechat Pay. This graph depicts the roles related to wechat Pay (customer, merchant…). In the wechat payment scenario, the sequential call relationship between the basic payment and the payment.

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

In addition, domestic generally prefer to call the sequence diagram “sequence diagram”.

  • If you think about it purely as a translation, the word sequence doesn’t mean time, it means sequence, order, etc., so some people say that the term sequence diagram is inaccurate.
  • The description of a sequence diagram is fine by definition. Because in the Sequence Diagram, the trigger time of each message is indeed executed in chronological order.

I think it’s ok to call a Sequence Diagram either a Sequence Diagram or a Sequence Diagram, so don’t bother.

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

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 calling relationships of the related classes involved in calling the target method. Especially in the code call hierarchy is more time, for us to understand the source code is very useful. (Aside: Most of the time we actually work is spent reading and understanding existing code.)
  2. Technical documentation: When we wrote the project introduction documentation, in order to make your code easier for others to understand, you needed to generate sequence diagrams for related classes based on the core methods to show the calling relationships between them.
  3. Sorting out business processes: When our system business processes are complex, we can use sequence diagrams to visualize the relationships between the important roles and objects involved in the system.
  4. .

How do I use IDEA to generate a sequence diagram from a method in a class?

With the SequenceDiagram plug-in, 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 place.
  2. Removes the corresponding class or method from the sequence diagram.
  3. Export the generated sequence diagram as a PNG image format.

The installation

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

If you are unable to use the IDEA plugin market due to network problems, you can also download and install the IDEA plugin market via the official website.

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 the basic parameters of the generated sequence diagram, such as the call depth, click OK!

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

Once the sequence diagram is generated, you have the option to export it as an image.

My open source project recommendation

  1. JavaGuide: “Java Learning + Interview Guide” covers the core knowledge most Java programmers need to know. Prepare for a Java interview with JavaGuide first!
  2. Guide xml-rpc – framework: Implemented by Netty+Kyro+Zookeeper. A Custom RPC Framework implemented by Netty+Kyro+Zookeeper.
  3. Jsoncat: A lightweight HTTP framework that mimics But differs from Spring Boot
  4. A programmer should have some good habits + interview must know things!
  5. Springboot – Guide: Not only SpringBoot but also important knowledge of Spring
  6. Awesome Java: Collection of Awesome Java Projects on Github