Introduction
I think everyone has heard in childhood such a saying as “ Measure twice, cut once .” It's the same in programming. It's always better to think about the implementation before you spend time executing it. Often during implementation you have to create classes and invent their interaction. And often a visual representation of this can help solve the problem in the most correct way. This is where UML helps us .What is UML?
If you look at pictures in search engines, it will become clear that UML is something about diagrams, arrows and squares. What is important is that UML translates to Unified Modeling Language . The word Unified is important here. That is, our pictures will be understood not only by us, but also by others who know UML. It turns out that this is an international language for drawing diagrams.
As Wikipedia says
UML is a graphical description language for object modeling in software development, business process modeling, systems design, and displaying organizational structures.The most interesting thing that not everyone thinks about or realizes is that UML has specifications. Moreover, there is even a UML2 specification. More details about the specification can be found on the Object Management Group website . Actually, this group is developing UML specifications. It is also interesting that UML is not limited to describing the structure of classes. There are many types of UML diagrams. A brief description of the types of UML diagrams can be seen in the same Wikipedia: UML diagrams or in the video by Timur Batyrshinov Review of UML diagrams . UML is also widely used to describe various processes, for example here: Single sign-on using JWT . Returning to the use of UML class diagrams, it is worth noting the book Head First: Design Patterns , in which the patterns are illustrated by those same UML diagrams. It turns out that UML is indeed being used. And it turns out that knowledge and understanding of its application is quite a useful skill.
Application
Let's look at how you can work with this same UML from the IDE. Let's take IntelliJ Idea as an IDE . If we use IntelliJ Idea Ultimate , then we will have the “ UML Support ” plugin installed out of the box. It allows you to automatically generate beautiful class diagrams. For example, using Ctrl+N or the menu item "Navigate" -> "Class" we go to the ArrayList class . Now, through the context menu for the class name, select “Diagram” -> “Show diagram popup”. As a result, we get a beautiful diagram:
@startuml
class ArrayList {
}
class LinkedList {
}
@enduml
To see the result in Idea, select "View" -> "Tool Windows" -> "PlantUML". We'll just get two squares representing classes. As we know, both of these classes implement the List interface. This class relationship is called implementation. To depict such a connection, use an arrow with a dotted line. Let's depict it:
interface List
List <|.. ArrayList
List <|.. LinkedList
List
- one of the child classes Collection
. That is, it inherits from Collection. This connection is called generalization. It looks like an arrow with a regular continuous line. Let's depict it:
interface Collection
Collection <|-- List
For the following type of connection, add to the class description ArrayList
an entry about the package private array of elements:
~Object[] elementData
Now we want to show what ArrayList
contains some objects. In this case, the connection type will be aggregation . The unit in this case is ArrayList
, because it contains other objects. We choose aggregation because objects in the list can live without the list: they are not integral parts of it. Their lifetime is not tied to the list's lifetime. Aggregate is translated from Latin as “assembled,” that is, something made up of something. For example, in life, there is a pumping unit, which consists of a pump and a motor. The unit itself can be disassembled, leaving some of its components. For example, to sell or put into another unit. So is the list. And this is expressed in the form of an empty diamond near the unit and a continuous line. Let's depict it like this:
class Object{
}
ArrayList o- Object
Now we want to show that, unlike ArrayList
, the class LinkedList
contains Node
containers that refer to stored data. In this case, Node
they are part of themselves LinkedList
and cannot live separately. Node
is not directly stored content, but only contains a link to it. For example, when we add to LinkedList
a row, we add a new Node
, which contains a link to that row, as well as a link to the previous and next Node
. This type of communication is called composition . To display a composite (one that consists of parts), a colored diamond is drawn, with a continuous line leading to it. Let's now write this as a text display of the connection:
class Node{
}
LinkedList *-- Node
And now we need to learn how to display another important type of relationship - dependency relationship. It is used when one class uses another, and the class does not contain the class being used and is not its descendant. For example, LinkedList
they ArrayList
know how to create ListIterator
. Let's display this as arrows with a dotted line:
class ListIterator
ListIterator <... ArrayList : create
ListIterator <... LinkedList : create
After all this it will look like this:
Automation
There are various ways to automatically generate PlantUML diagrams. For example, Idea has a SketchIT plugin , but it doesn’t draw them quite correctly. For example, the implementation of interfaces is drawn incorrectly (displayed as inheritance). There are also examples on the Internet of how to build this into the build life cycle of your project. Let's say there is an example for Maven using uml-java-docklet . To show how this is done, we will use Maven Archetype to quickly create a Maven project. Let's execute the command:mvn archetype:generate
When asked to select a filter ( Choose a number or apply filter ), leave default by simply pressing Enter. This will always be " maven-archetype-quickstart ". Select the latest version. Next, we answer the questions and complete the creation of the project:
mvn clean install
file is located: and mvn javadoc:javadoc
. Now, if we open the generated documentation (explorer target\site\apidocs\index.html), we will see the UML diagrams. By the way, the implementation is already displayed correctly here)
GO TO FULL VERSION