Introduction
I think everyone heard in childhood such a saying as " Measure seven times, cut once ." It's the same in programming. It's always best to think about the implementation before you spend time executing it. Often you have to create classes during implementation, 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 becomes clear that UML is something about diagrams, arrows and squares. What is important is that UML is translated as 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 this is such an international language for drawing diagrams.
As Wikipedia says
UML is a graphical description language for object modeling in the fields of software development, business process modeling, systems engineering, and mapping organizational structures.The most interesting thing that not everyone thinks about or guesses about is that UML has specifications. Moreover, there is even a UML2 specification. More information about the specification can be found on the Object Management Group website . Actually, this group is engaged in the development of 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 Timur Batyrshinov's video Overview of UML diagrams . UML is also widely used in describing various processes, for example here: Single sign-on using JWT . Returning to the use of UML class diagrams, it is worth noting the bookHead First: Design Patterns , in which the patterns are illustrated by the same UML diagrams. It turns out that the UML is indeed being used. And it turns out that knowing and understanding its application is quite a useful skill.
Application
Let's see how you can work with this very 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, through Ctrl+N or the menu item "Navigate" -> "Class" let's go to the ArrayList class . Now, through the context menu by the class name, select "Diagram" -> "Show diagram popup". As a result, we get a beautiful chart:
@startuml
class ArrayList {
}
class LinkedList {
}
@enduml
To see the result in Idea, select "View" -> "Tool Windows" -> "PlantUML". We will get just two squares denoting classes. As we know, both of these classes implement the List interface. This relation of classes is called implementation (realization). An arrow with a dotted line is used to depict such a relationship. Let's depict it:
interface List
List <|.. ArrayList
List <|.. LinkedList
List
is one of the child classes Collection
. That is, it inherits from Collection. This relationship is called generalization. It looks like an arrow with a regular continuous line. Let's depict it:
interface Collection
Collection <|-- List
ArrayList
For the next type of connection, add an entry about the package private array of elements to the class description :
~Object[] elementData
Now we want to show what ArrayList
contains some objects. In this case, the type of connection will be aggregation . The unit in this case is ArrayList
, because it contains other objects. We choose aggregation because the objects in the list can live without the list: they are not integral parts of it. Their lifetime is not tied to the lifetime of the list. The unit from Latin is translated as "collected", that is, something made up of something. For example, in life, there is a pumping unit, which consists of a pump and an engine. The unit itself can be disassembled, leaving some of its components. For example, to sell or put in another unit. So it is on the list. And this is expressed in the form of an empty rhombus at the unit and a continuous line. Let's picture 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 the 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 reference to it. For example, when we add to LinkedList
a line, we add a new Node
, which contains a link to that line, as well as a link to the previous and next Node
. This type of relationship is called a composition. To display a composite (one that consists of parts), a filled robic is drawn, a continuous line leads to it. We now write this as a textual display of the connection:
class Node{
}
LinkedList *-- Node
And now you need to learn how to display another important type of relationship - dependency relationship. It is used when one class uses another, while the class does not contain the used class and is not its successor. 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, 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 correctly. For example, the implementation of interfaces is drawn incorrectly (displayed as inheritance). There are also examples online of how to build this into your project's build life cycle. Let's say there is an example for Maven using uml-java-docklet . To show how this is done, let's use the Maven Archetype to quickly create a Maven project. Let's execute the command:mvn archetype:generate
On the question of choosing a filter ( Choose a number or apply filter ), leave the default by simply pressing Enter. It will always be " maven-archetype-quickstart". We select the latest version. Next, answer the questions and complete the creation of the project:
mvn clean install
and mvn javadoc:javadoc
. Now, if we open the generated documentation (explorer target\site\apidocs\index.html), we will see UML diagrams. By the way, the implementation is already displayed correctly here)
GO TO FULL VERSION