UML stands for Unified Modeling Language, and is a way to visualize and design classes without getting mired in all the details of code. When designing an object-oriented system, we want some way to talk about what different classes represent without having to fully write them. This allows us to sketch out a design before getting into the code. It also serves as documentation for how an existing system works.
UML is a visual language. Like any language, it has rules about what things go where, and what different things mean. But a piece of UML is a diagram. UML is also independent of any particular programming language. The class in a UML diagram could in theory be implemented in any language. As we'll see sometimes language-specific things leak in though.
There are many types of UML diagrams. The one we will look at here is the class diagram which represents the instance variables and methods of the classes that make up a system.
Below is an example of a UML class diagram:
This diagram represents the
Family class from one of our recent labs.
The top of the diagram is labelled with the name of the class, in this case "Family".
After that come two sections, separated with a line.
The first of these sections contains the variables of the class. Here there are three. All of them are marked with a "-" sign. In a UML diagram that indicates that they are all private. After the - sign comes the name of the variable. Then we have a colon, and then finally the type of the variable. In Java we put types first, then names. In many other languages (including UML) names come first, then types.
The other thing to note in the variable section is that the random number generator
variable is underlines. In UML, that means that there is only one of them that's shared
amongst all instances of the Family class. In Java terms, that means that
should be static. This goes for methods too. If this class had a static method, it would
be underlined too.
The bottom section of a UML class diagram contains the methods of the class. This class
has just three methods. They begin with a "+" sign indicating that they are public. Like
with variables, the name of the method comes first, followed by a colon, followed by the type.
Here "type" means the return type of the method. For the second two methods, the type is "int".
The first method listed,
haveChildren, has no type listed because it's a void
method. Sometimes people leave the return type off like this, and sometimes they explicitly
write ": void".
If any of these methods had parameters, they would be listed in the parentheses of the method.
The nice things about UML diagrams is that, once you get used to looking at them, they contain a lot of information laid out in a relatively simple way.
Copyright © 2022 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.