Some topics in software engineering are classics of the field and always show up in every curriculum and agenda. The subject of today's text definitely belongs to that group: the UML notation, better known as "UML diagrams". Some people equate those with a description of classes and the relations between them, but that is a mistake. We will start by setting it straight.
What is UML?
UML (Unified Modeling Language) is a standardized notation used in software modeling and, colloquially, a name for a number of diagrams describing the structure and behavior of an IT system being designed. Many concepts known from UML are used (sometimes unknowingly) in the everyday sketches drawn while building software.
UML 2.0 and later defines more than a dozen types of diagrams, divided into two main categories:
- structure diagrams: their main job is to show the structure of entities and their relations to one another. This is the most commonly used type of diagram, often equated with UML itself, and a very popular representative is the class diagram.
- behavior diagrams: they focus on the behavior of objects, the functionality of the system and the flow of information (the last aspect is sometimes treated as a separate category, interaction diagrams). The most common examples are use case, activity, state and sequence diagrams.
Is UML really used?
This is a fairly common question about the classics of software engineering: does a given technique have practical value and is it actually used in real projects, or is it something only keen formalists and university professors bother with? In the case of UML the answer is somewhere in between, but even so it is worth knowing.
The fact is that in most projects formal diagrams are not used, or only in a very trimmed-down form, mainly because of the time it takes to prepare them. In mid-sized projects, parts of the system are designed this way, though at a more general level of abstraction. There are companies where UML is certainly used, especially for very large systems, often safety-critical ones, where a mistake at the design stage and an unnoticed risk matter much more than usual.
But whether or not we use UML in practice, I think it is worth learning anyway. Let's go back to the diagrams on a more general level of detail: they usually do not use a formal notation. Yet a person who knows UML will almost unconsciously use some of its elements, which help in reading them. And whatever notation we use, and to whatever degree, they all share one goal: to show a concept clearly and unambiguously, so that other project members grasp it quickly and painlessly. It is not true that only pure UML diagrams are well understood in the IT world. UML is just a standard that helps with this, but it is not mandatory.
A refresher on object orientation
Since structure diagrams (the most popular form of UML) rely on the principles of object orientation, it is worth recalling what hides behind that term. Object-oriented programming (OOP for short) is a way of coding (a paradigm) in which a program is organized into classes (made up of fields and methods), from which objects are created. The aim is to carry the real world over into a software model in a more intuitive way. But object orientation is not just classes, fields and methods. It also includes many other techniques that build on them and that you use naturally, as long as you know them. That is why it is worth recalling the features sometimes called the pillars of object orientation:
- Encapsulation: some elements of a class should be protected from direct access from other areas of the program, to keep control over modification of and access to data. This rule mainly applies to fields (variables). An example showing the point of this rule is a class
Biblioteka(Library), whose resources (books) should be reachable only through special methods (simulating aBibliotekarz, a librarian, the person who serves the reader), and not directly (which would be like a reader walking into the library, taking a book on their own and leaving). - Inheritance: definitions of certain entities can be refined (specialized) by defining other entities that inherit from them. As a result, subclasses do not have to define certain methods again. They can use the ones introduced for the parent class and, at most, fix them (more on that below).
- Polymorphism: objects can be stored as instances of the same general class while being instances of different, more specific classes, which gives them new properties or makes them behave in a way characteristic of the subclass. For example, we can have a collection with two objects of the class
Żołnierz(Soldier), and both have a methodstrzelaj()(shoot) at their disposal, but one of these objects is actually an instance of the classSnajper(Sniper, inheriting fromŻołnierz), where calling shoot produces a slightly different effect. - Abstraction: a property that lets entities in a program communicate and work without knowing the implementation details of other entities. In programming languages this principle is realized mainly through abstract classes and methods, and also interfaces. We had an example above: we can call the method
strzelaj()on a given object of the classŻołnierzwithout necessarily knowing the implementation details, that is, how exactly the shooting works.
The class diagram
Let's move on to the description of by far the most popular kind of diagram associated with UML (and sometimes, wrongly, equated with it): the class diagram. Its purpose is fairly intuitive. Sometimes we need to design and discuss the structure of the classes of the software we are building, to avoid excessive refactoring later or so-called code smells. In addition, when building a model we can already think through certain choices "on paper" and judge whether they will bring benefits or the opposite.
A class diagram is used to design the structure of classes and their relations to one another. It is also worth mentioning that it should not be confused with the entity-relationship diagram (Entity-Relationship Diagram/Model, ER), which is used to design entities and is often the basis for designing relational database schemas. Despite many similarities, the two notations differ considerably, for instance in how they mark relations between entities ("crow's feet" in ER, "diamonds" in UML).
We describe a class as follows, separating three main sections: the class name, the fields and the methods:
Before we go on, a small practical tip. Since we often start designing this kind of diagram on paper, it is worth getting into the habit of drawing only the top and left edges of the rectangle. As you can see, the definitions of elements can be very long (it is worth wrapping them onto further lines) and there can be many of them, so it usually makes sense to close the rectangle only after writing all the entries.
We also need to recall one very important thing: a UML class model does not correspond one to one to the implementation of that class in real code. It is only a notation and a model. For example, something shown as a field may in the actual software be a method (or even a set of methods). It all depends on the capabilities of the particular programming language, the technology, the adopted conventions and later ideas. So a UML model is really a model, a pattern, but at the same time only a hint (albeit a very specific one) for the programmers carrying out a particular project.
Let's briefly go through each part of such a class. Each of them (except the name) can be left out. The exception is leaving out the fields section when the methods section is present: then an empty rectangle has to stay (you will see this in the example under relation types later in this article).
The class name section
This is where the name of the class or interface goes. And the part between two angle brackets serves, among other things, the latter: it holds an extra mark classifying the entity. It can be an interface, an enumeration type (enum) or something else. And here again we see that classes need not be described strictly by the UML standard alone, but that it can be extended with internal notations that are clear and helpful to a given team.
The fields section
As you can guess, the fields of the class, that is, its variables, are entered here. Each of them starts with a visibility modifier, which defines the scope of the element. These are features known from most object-oriented languages: + means a public element, - a private one, and # a protected one, that is, visible to subclasses. Sometimes ~ is also used, meaning a public element visible only within a given package. Because of the encapsulation principle, private and protected modifiers will probably be the most common for fields.
Then comes the name of the field and, after a colon, the specific type. It can be a simple type (for example string) or a complex one (the name of another class). Also, if the element is a collection (an array, a list and so on), square brackets are added to the type (for example int[]).
While we are at it, here are other elements you can use when describing classes:
- static elements are marked by underlining the whole line,
- if a slash ("/", we wrote about how to tell the slashes apart here) is used, we are dealing with a so-called derived attribute, which means not a field but a specific method returning a calculated value. Most often a note is added to it, as an extra rectangle connected to the element with a dashed line.
- attributes that are read-only get the suffix
{readOnly}, - you can mark constants by assigning a specific value after an equals sign at the end,
- if one class contains another (relations come in a moment), an additional field is usually not added to the class definitions, so that the diagram does not grow needlessly.
As you can see, there are many options, and some of them can be very useful during implementation later.
The methods section
Here the notation is very similar to that of fields (including, for example, the rules about visibility modifiers), but one new element appears: the parameter list. It describes the arguments (separated by commas) passed to the method, following the notation nazwaArgumentu: typ (argument name: type). Here too the type can be simple or complex.
For methods there are also a few additional marks that let you convey the designer's intent better in the diagram.
- abstract methods (and the class itself) can be recognized by the use of italics (slanted text),
- constructors and destructors can be placed on the diagram, but this is recommended only when the constructor takes some parameters and, for destructors, when they run some logic besides ordinary memory cleanup. Otherwise there is no need to "clutter" the diagram. Note that these special methods have no return type.
- if needed, additional modifiers can be placed before a method's parameters to show the nature of that parameter. These are
in(input parameter, the default),out(output parameter) andinout(input-output parameter). Example:run(in dist : float) : boolean.
We can borrow the remaining rules from fields, since they apply here too (for example static methods).
Relations
Man does not live by classes alone, and the relations between them matter just as much. It is a complex topic that causes plenty of trouble for people who do not know the notation or mix it up and mark some things the wrong way around.
First, let's show how to model the inheritance of classes or the implementation of interfaces. In both cases it is done with an arrow whose head is an unfilled triangle. This matters, because otherwise we would be dealing with an ordinary dependency between classes. As the attached examples show, with many subclasses you can join the arrows into one, though that is not required.
Interfaces are a little different: this is not strictly inheritance but implementation. To tell them apart, a very similar arrow is used, but the line is dashed. See the examples above.
That was the easy part. Now we move on to ordinary relations between classes, and first we will show how notes next to the arrows can tell the programmer, for example, that they are dealing with a collection.
Relations can be directional or not. This means that objects of one class may be related to another (for example contain it, more on that a bit later), but not necessarily the other way around. Inheritance is in fact a directional relation: it is the subclass that "uses" the parent, not the reverse. The marking is very simple: just show an arrowhead on one side. In the example above, the relation between Book and TableOfContents is directional (the first has the second), while the link between TocItem and Chapter goes both ways.
Sometimes we want to show that objects of one class can connect to many objects (or to some specific number) of another. For that we have multiplicities, which are the source of the... most mistakes students make in class. What matters most is where a multiplicity is placed and remembering one fact: a multiplicity m means that for one object of the given class there are m objects of the other class. As for placement, if class A can contain some number of elements of class B, the multiplicity goes next to class B. Going back to the example: an object of the class TableOfContents can contain from one to many objects of the class TocItem. It gets even more interesting in two-way relations: here we see that TocItem always points to one Chapter object, but each Chapter object can (but does not have to) have one object of the class TocItem. The available (or rather the most popular) multiplicity types are:
- 1: the default multiplicity (you do not have to write it, though we did once in the example), meaning exactly one object is held.
- 0..1: optionality, meaning at most one object may be held (but none may be assigned).
- 1..*: a mandatory collection, meaning the object holds a non-empty collection of other objects (at least one element).
- *: an optional collection, meaning the object holds a collection of other objects (it may hold none). Equivalent to
0..*.
We can make a relation more specific by giving it a label or name. Here we see that TocItem contains a link to Chapter (so this time we are on the side of the class that initiates the connection). In practice this often turns into the name of a field, or identifies the links when there is more than one between a given pair of classes. It is also possible to create a relation from a class to itself, in which case the arrow leaves and enters the same entity.
Are you still with me? Because the types of relations are still ahead of us.
There are four of them. A dependency is the weakest type and shows that one entity uses another in some way, or that a change in it affects the other. This can matter when you plan the order of work later and calculate various metrics, which look at whether changes in one class may require checking other parts of the project. A dependency is marked with a dashed line and usually carries a label showing its nature. Example labels are: <<call>> (calls), <<use>> (uses, needed for implementation), <<instantiate>> (is an instance of), <<realize>> (realizes, for example an interface) and <<create>> (creates). As you can see, this is a form we have already seen with interface implementation.
In the example above, Wing is in this relation: in at least one place it uses an object of the class Color, but that is all. It is not tied to it for longer.
An association, in turn, is stronger and used more often. It is usually used to show a reference (ownership) of objects of one class by objects of another. Remember, though, that both classes are equal partners in this relation: removing objects of one does not affect objects of the other, since they are independent. It is a way to do something we mentioned earlier: describing fields of a class that are not of simple types but objects of another class, in which case they are not written again in the fields section. An association can certainly be two-way, though one-way is more common.
In the example, this is the relation between Airplane and Pilot: each airplane has at least one pilot, but not exclusively, since both classes are equal partners.
We are getting greedier: an aggregation (so-called partial aggregation) is a strong link in which objects of one class not only hold objects of another, but do so as the parent class (not in the sense of inheritance). In other words, objects of the child class make no "sense" existing on their own, because they are merely a component of the parent's objects. They do not belong exclusively to one parent object, though (a part can belong to different wholes). Usually the parent's objects also do not manage (create and delete) the child objects. An aggregation is marked with an unfilled diamond at the parent class.
In the example we see this in the relation between Pilot and PilotUniform. A uniform has little practical meaning without someone to wear it, but it is not exclusive to one pilot. Well, even airlines have to save money sometimes.
And finally the strongest type of relation: composition (also called strong or complete aggregation). It extends aggregation. Again we have a parent and a child class (the "whole and part" concept), but here they are inseparably bound together. The part cannot exist without the whole and vice versa. Moreover, child objects are usually managed by objects of the parent class. As if that were not enough, the parts belong exclusively to one whole. A composition is marked with a filled diamond at the parent class.
There is no denying that an object of the class Wing not only makes no sense without the Airplane object that owns it, but it is also hard to imagine one wing shared between several airplanes. That is a typical composition.
While drawing relations, two more practical points are worth noting, as they make the diagram easier to analyze later. First, avoid crossing lines like the plague. It is allowed, but if at all possible it is worth taking a longer route rather than creating a so-called intersection. This not only makes the design easier to read but also prevents mistakes from misreading overlapping lines. Second, it is worth drawing all connections with right angles. Again, it is not a requirement, but it makes the diagram look neater and more technical.
Tools
You are surely asking yourselves which tool works best for drawing UML diagrams (apart, of course, from paper and pencil). Many programs are available and everyone has their own preferences. Some will point to Visual Paradigm, for example. My view is that an application should be as simple, as fast and as trouble-free as possible. So I will point out not necessarily the best programs, but the ones I use or have used and can recommend.
For a very long time I used a simple Java tool, UMLet. It is no longer developed, but that does not change the fact that it is worth a look. It includes components for all (or almost all) UML diagrams, including class diagrams. Its drawback may be an interface that is not intuitive at first and the need to code, for example, relations in a special way. But that inconvenience is also its strength: once you learn what UMLet can do, you can create even complex diagrams very quickly. I realize, though, that it is not a perfect application. Still, it is worth a look, because you might just like it.
I dropped UMLet in favor of the web and desktop application diagrams.net (formerly draw.io). I did it especially because this tool lets you draw not only UML diagrams but many others too. I much more often prepare all kinds of context diagrams, design components and show dependencies between whole systems, and the large library of graphics here makes that possible. Sometimes the application can be annoying (especially when moving elements and lines), but I can recommend it with confidence.
Summary
This article was meant to introduce you to UML and explain what it actually is. We need to be aware that it is a standard covering many aspects of software design. Still, there is no hiding that class diagrams are the most commonly used, and we covered those thoroughly as well. Remember that the overriding principle is that a model should be clear to everyone. If for some reason using UML seems too labor-intensive, or we are not sure we will do it right, it is worth simply following our common sense and making sure the readers of the diagram have as few doubts as possible.
Best regards and thank you, Jakub Rojek.


