JavaRush /Java Blog /Random EN /UML: from theory to practice
Viacheslav
Level 3

UML: from theory to practice

Published in the Random EN group

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.

UML: from theory to practice - 1

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:
UML: from theory to practice - 2
But what if you want to draw it yourself, and you don’t even have the Ultimate version of Idea? If we use IntelliJ Idea Community Edition, then we have no other choice. To do this, you need to understand how such a UML diagram is structured. First, we'll need to install Graphviz . This is a set of utilities for visualizing graphs. It is used by the plugin that we will use. After installation, you need to add the bin directory from the Graphviz installation directory to your PATH environment variable . After that, in IntelliJ Idea, select File -> Settings from the menu. In the "Settings" window, select the "Plugins" category, click the "Browse repositories" button and install the PlantUML integration plugin . Why is PlantUML so good ? It uses a graph description language called " dot " to describe UML and this allows it to be more universal, because... This language is used not only by PlantUML. Moreover, everything we do below can be done not only in the IDE, but also in the online service planttext.com . After installing the PlantUML plugin, we will be able to create UML diagrams through “File” -> “New”. Let's create a diagram of the "UML class" type. During this process, a template with an example is automatically generated. Let's delete its contents and create our own, armed with an article from Habr: Class relationships - from UML to code . And to understand how to depict this in the text, let’s take the PlantUML manual: plantuml class-diagram . At the very beginning there is a table showing how connections should be described:
UML: from theory to practice - 3
We can also look at the connections themselves here: “ Relationships between classes in UML. Examples .” Based on these materials, let's start creating our UML diagram. Let's add the following content describing the two classes:

@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 ArrayListan entry about the package private array of elements:
~Object[] elementData
Now we want to show what ArrayListcontains 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 LinkedListcontains Nodecontainers that refer to stored data. In this case, Nodethey are part of themselves LinkedListand cannot live separately. Nodeis not directly stored content, but only contains a link to it. For example, when we add to LinkedLista 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, LinkedListthey ArrayListknow 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:
UML: from theory to practice - 4
You can go into as much detail as necessary. All designations are indicated here: " PlantUML - Class Diagram ". In addition, there is nothing supernatural in drawing such a diagram, and when working on your tasks, you can quickly draw it by hand. This will develop your skills in thinking through application architecture and help you identify class structure flaws early on, rather than after you've spent the day implementing the wrong model. I think that's a good reason to give it a try? )
UML: from theory to practice - 5

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:
UML: from theory to practice - 6
Since Maven is not the focus of this article, answers to your Maven questions can be found in the Maven Users Center . In the generated project, open the project description file, pom.xml, for editing . Let's copy the contents from the description of uml-java-docklet installing into it . The artifact used in the description could not be found in the Maven Central repository. But it worked for me with this: https://mvnrepository.com/artifact/com.chfourie/uml-java-doclet/1.0.0 . That is, in that description you just need to replace the groupId from “ info.leadinglight ” with “ com.chfourie ” and set the version to “ 1.0.0 ”. After this, we can execute these commands in the directory where the pom.xmlmvn 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)

Conclusion

As you can see, UML allows you to visualize the structure of your application. Moreover, UML is not limited to just this. Using UML, you can describe various processes within your company or describe the business process within which the function you are writing operates. How useful UML is for you personally is up to you to decide, but taking the time to read it in more detail will be useful in any case. #Viacheslav
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION