Programmers often have the need to draw sequence diagrams and flow charts in their work, especially when writing documents. Normally we would use a tool like ProcessOn to draw, but sometimes it’s more efficient to draw in code, because no one knows code better than a programmer. Today, I recommend a drawing tool, PlantUML, which can be used with IDEA to make drawing more efficient!
SpringBoot e-commerce project mall (50K + STAR) address: github.com/macrozheng/…
PlantUML profile
PlantUML is an open source UML diagramming tool that allows you to generate graphs from text and is very efficient to use. It can support sequence diagram, class diagram, object diagram, activity diagram, mind map and other graphics drawing.
Use PlantUML to draw a flow chart, you can preview in real time, also very fast!
The installation
The most convenient way to use PlantUML is to install the PlantUML plug-in for IDEA.
- Start by searching IDEA’s plugin marketplace
PlantUML
, install the # 1 plugin;
- Sometimes the network is not good words may not download down, you can click
Plguin homepage
Button to visit the plug-in home page, and then select the appropriate version to download the package;
- After the download is successful, install it from the local PC.
use
Next, we will use the PlantUML plug-in to draw sequence diagrams, use case diagrams, class diagrams, activity diagrams and mind maps respectively to experience whether PlantUML is really useful.
Sequence diagram
Sequence Diagram is a KIND of UML interaction Diagram. It shows dynamic collaboration between multiple objects by describing the chronological order in which messages are sent between them. When we learn Oauth2, the first step is to understand the flow of Oauth2, at this time have a sequence diagram help can be great. Below we use PlantUML to plot the sequence diagram of Oauth2 using the authorization pattern to issue tokens.
- First we need to create a new PlantUML file and select the sequence diagram.
- We can use the syntax provided by PlantUML to generate the sequence diagram of Oauth2. The syntax is very simple, and the details are as follows.
@startuml title Oauth2 Authorization code mode for token issuance actor User as User Participant "User Agent" as userAgent Participant "Client" as Client Participant "Auth Login" as Login Participant "Auth Server" as Server Autonumber user->userAgent: Accesses the client activate UserAgent userAgent->login: redirects to the authorization page +clientId+redirectUrl Activate Login Login ->server: username + password +clientId+redirectUrl Activate Server server-->login: returns the authorization code login-->userAgent: redirects to redirectUrl+ authorization code deactivate Login Activate Client ->server: Activate client +clientId+clientSecret Server -->client: issues accessToken accessToken+refreshToken deactivate server client-->userAgent: returns access and refreshToken deactivate client UserAgent --> user: the token is issued deactivate userAgent@endumlCopy the code
- This code will generate the following sequence diagram, write code to draw the sequence diagram, isn’t it cool;
- The key descriptions of this sequence diagram are as follows:
title
Can be used to specify a UML diagram title;- through
actor
Humanoid participants can be declared; - through
participant
Participants of common types can be declared; - through
as
You can alias the participant; - through
->
Relationships between participants can be plotted and dotted arrows can be used-->
; - After each actor relationship, you can use
:
Add description to the relationship; - through
autonumber
We can automatically number the participant relationships; - through
activate
anddeactivate
Lifelines for participants can be specified.
- One of the magic features here is that when we right-click on a sequence diagram, we can generate a link that is accessed online;
- Go directly to this link to access and edit UML sequence diagrams online.
Use case diagram
The Usecase Diagram is the simplest representation of the user’s interaction with the system, showing the relationship between the user and the use cases associated with him. Through the use case diagram, we can easily represent the relationship between each role in the system and the use case. Let’s use PlantUML to draw a use case diagram.
- First we need to create a new PlantUML file and select the use case diagram, which represents the relationship between each use case in the customer, chef, gourmet, and restaurant as follows.
@startuml
left to right direction
actor Guest as g
package Professional {
actor Chief as c
actor "Food Critic" as fc
}
package Restaurant {
usecase "Eat Food" as uc1
usecase "Pay For Food" as uc2
usecase "Drink" as uc3
usecase "Review" as uc4
}
g--> uc1
g--> uc2
g--> uc3
fc--> uc4
@enduml
Copy the code
- This code generates the following use-case diagram;
- The key description of this use case diagram is as follows:
left to right direction
Represents drawing a use case diagram from left to right, top to bottom by default;- through
package
You can group roles and use cases; - through
actor
You can define users; - through
usecase
You can define use cases; - Relationships between roles and use cases can be used
-->
To represent.
The class diagram
The Class Diagram can represent the static structure of a Class, such as the properties and methods contained in the Class, as well as the inheritance structure of the Class. So let’s draw a class diagram using PlantUML.
- First, we need to create a new PlantUML file and select the class diagram, which is used to represent the structure of the Person, Student and Teacher classes.
@startuml
class Person {
# String name
# Integer age
+ void move()
+ void say()
}
class Student {
- String studentNo
+ void study()
}
class Teacher {
- String teacherNo
+ void teach()
}
Person <|-- Student
Person <|-- Teacher
@enduml
Copy the code
- This code will generate the following class diagram. Take a look at the code and class diagram to see if it looks similar to the class we defined in code.
- The key description of this diagram is as follows:
- through
class
You can define classes; - Visibility can be defined by adding symbols to the left of properties and methods,
-
saidprivate
.#
saidprotected
.+
saidpublic
; - through
<|--
Represents an inheritance relationship between classes.
- through
Activity diagrams
The Activity Diagram is the most commonly used UML Diagram to represent business processes, such as the ordering process in e-commerce. Now let’s use PlantUML to draw an activity diagram.
- First of all, we need to create a PlantUML file and select the activity diagram, which uses the process of generating confirmation form in the shopping cart of the Mall project. The details are as follows.
Start: Get shopping cart information and calculate discount; : Get the list of member shipping addresses from ums_member_receive_address table \n; : Obtain all coupon information of the member; Switch (judge whether each coupon is available according to use_type) case(0) : universal; If (judge whether the total amount of all goods \n meets the starting amount of use) then (no) : Get the user can not use the coupon list; Stop endif case(-1) : specifies a class; If (judge whether the total amount of the specified category goods \n meets the starting amount of the use) then (no) : The user can not use the coupon list; Stop endif case(-2) : judge whether the total amount of specified goods meets the starting amount; If (judge whether the total amount of the specified category goods \n meets the starting amount of the use) then (no) : The user can not use the coupon list; Stop endif endSwitch: Get a list of coupons available to the user; : Obtain user points; : Obtain the rules of using points; : Calculate the total amount, activity concessions, amount payable; stop @endumlCopy the code
- This code will generate the following activity diagram, which we can use in the activity diagram
if else
And can be used againswitch
And can even be usedThe while loop
, it is quite powerful;
- The key notes of this activity chart are as follows:
- through
start
andstop
Can represent the beginning and end of a process; - through
:
and;
Add text in the middle to define the activity flow node; - through
if
+then
+endif
Defining conditional judgment; - through
switch
+case
+endswitch
Define switch judgments.
- through
Mind mapping
Mind Map is an effective graphical tool to express divergent thinking. It is simple but effective, and it is a practical thinking tool. I used it a lot earlier in my mall tutorial, so let’s use PlantUML to draw a mind map.
- First of all, we need to create a new PlantUML file and select the mind Map. Here we use the outline view of mall learning route. The details are as follows.
@startMindMap +[#17ADF1] Mall Learning route ++[# lightGreen] Recommended information +[# LightBlue] Back-end stack +++_ Project framework +++_ Data storage +++ +_ O&M deployment +++_ Others ++[#orange] Build project skeleton ++[#1DBAAF] project deployment +++_ Windows deployment +++ +_ Linux deployment using Docker Compose +++_ Automatic Deployment with Jenkins under Linux --[#1DBAAF] e-commerce business -- -- authority management module -- -- Commodity module -- -- -- Order module -- -- marketing module --[#orange] Technical points --[# LightBlue] front-end technology stack --[# LightGreen] Advanced Micro Services --_ Spring Cloud Stack --_ Project Deployment --_ Technical Highlights --[# YELLOW] Development tools --[# LightGrey] Extension learning @endMindMapCopy the code
- This code will generate the following mind map. In fact, using PlantUML, we can define the style of the graph by ourselves. Here I will customize the color;
- The key instructions of this mind map are as follows:
- through
+
and-
It can represent nodes in mind map with directivity; - through
# [color]
You can define the border color of the node. - through
_
You can remove the border of the node;
- through
conclusion
Although there are many graphical tools available to draw UML diagrams, it may be more straightforward and efficient for programmers to draw using code, especially with IDEA. If you want to use code to draw, try PlantUML.
The resources
Official document: plantuml.com/zh/
In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!