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 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.

UML: from theory to practice - 1

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:
UML: from theory to practice - 2
But what if you want to draw yourself, and even there is no Ultimate version of Idea? If we are using IntelliJ Idea Community Edition, then we have no other choice. To do this, you need to understand how such a UML scheme works. First we need to install Graphviz . It is a set of graph visualization utilities. 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 the 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 . What is so good about this PlantUML? It uses a graph description language called " dot " to describe the UML, and this allows it to be more versatile, because this language is used not only by PlantUML. Moreover, everything that we will do below we can do not only in the IDE, but also in the planttext.com online service . After installing the PlantUML plugin, we will be able to create UML diagrams through "File" -> "New". Let's create a "UML class" diagram. During this, 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. In it, at the very beginning, there is a plate with how to describe the connections:
UML: from theory to practice - 3
About the connections themselves, we can still peep here: " Relationships between classes in UML. Examples ". Based on these materials, let's start creating our UML diagram. 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 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
Listis 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
ArrayListFor 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 ArrayListcontains 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 LinkedListcontains Node- containers that refer to the stored data. In this case Node, they are part of themselves LinkedListand cannot live separately. Nodeis not directly stored content, but only contains a reference to it. For example, when we add to LinkedLista 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, 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, it will look like this:
UML: from theory to practice - 4
You can detail as much as you need. All designations are listed here: " PlantUML - Class Diagram ". In addition, there is nothing supernatural in drawing such a scheme, and when working on your tasks, you can quickly draw it by hand. This will develop the skills to think through the application architecture and help to identify class structure flaws early on, and not after you have already spent a 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 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:
UML: from theory to practice - 6
Since Maven is not the focus of this article, answers to your questions about Maven can be found in the Maven Users Center . In the generated project, open the project description file, pom.xml , for editing . We will 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, simply replace the groupId from " info.leadinglight " to " com.chfourie " and set the version to " 1.0.0 ". After that, we can execute in the directory where the file is locatedpom.xml these commands are: mvn clean installand 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)

Conclusion

As you can see, UML allows you to visualize the structure of your application. Also, UML is not limited to just that. With the help of UML, you can describe various processes within your company or describe the business process within which the function that you write works. How much UML is useful for you personally is up to you, but finding the time and getting to know it in more detail will be useful anyway. #Viacheslav
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION