Hey there,
this months article’s topic is UML-Diagram and how they could help improving our programs. Sadly I didn’t wrote an article last month and I’m very sorry about this fact, but I promise to try everything to publish every month and I’m going even further I’m trying to post up to four articles a month. Only one about my column but the others will be about different topics!
Good news, this article will be longer and it will cover all things I like to write about UML. This would be a two-month article I’ll cover here!
Now let’s get started.
What’s UML
UML stands for Unified Modeling Language and it’s a collection of different modeling techniques. It can be used for almost every type of program whether they have databases, server&client or other things and UML is totally language independent. There are a lot of different diagram typs like class, component, object, sequence or communication diagram. In this article I focus on the use of class-diagram helping to improve our programming experience!
Why UML?
Why should we use a UML-Diagram to write code?
First of all class diagrams help us visualize the structure of a project in a much better way since it’s something we draw or create with an editor and we can hand out later.
Secondly, these diagrams do not only help the designer, they also help to explain the behavior of the classes or the project to outstanding people. As a result it’s much easier for us (yeah I think sometimes we talk in a unknown language to other people^^) to talk about the features and the design of a project.
Finally, with the help of a class diagram it’s much easier to see if our project has some big problems like wrong class behavior, God-classes & data-classes and so on. If we can see such problems in the beginning of a project it’s much easier to find another solution rather then invest a week programming to find out it doesn’t work. It’s essential that we don’t use to much time to draw such diagrams because it’s not actual code and you can’t see big process, therefore you should either just make a quick draft or use an editor.
Class diagrams don’t take much time to make and help improving our projects a lot because we can find design, behavior or structural problems at the start. As a result this amount of time used to draw and think about such diagrams is well used for the future.
On the other hand you shouldn’t use UML-Diagrams blindly for everything, sometimes you’re better off using an improvised diagram, pseudo-code or just your imagination. I don’t find anything helpful in UML to come up with algorithms for example. There are still some hints, why you would use UML.
Take this checklist:
- You are working with loads of other people. If so, others can see the big picture in the diagram, even though not everyone was there when the idea was formed.
- You will have many classes. It is difficult to keep track of all the interactions and code doesn’t often cut it to explain things. UML can help you by giving you a visual help for your project. Also many UML editors let you export the diagram as a code skeleton in the language of your choice.
- You will be working on the project for a long time. Or even worse: work on it again after some time. The big picture is the argument again.
- You have no clue what else to do. One could argue that you should not work on that project, but maybe you don’t have a choice. UML diagrams can help you to come up with the responsibilities (if you use Responsibility Driven Design). Or they at least look as if you did a lot of work if you can pass them around.
UML-Basics
I’m only going to write about class diagrams in this article but you can find more informations here: UML Tutorial
Every class in a class-diagram gets a rectangle with three cells, the first cell contains the name, the second is used for member variables and the third cell is used for all the different methods of this class. In the following picture you can see the basic structure of an entry.

First of all there are a few different signs to indicate the scope of a variable or a method, these are the following.
+ = public
# = protected
~ = default
- = private (minus)
capital letters = final
underlined = static
I’m now going to explain each of this indicators with an example of a class.
public class Cell {
public static final int MAXAGE = 10;
public String name;
protected String cellType;
private int birthday;
public void giveFood(Food food){
...
}
private void mitosis(){
...
}
}

The first variable in the class is public (+), static (underlined), final (capital letters) and an int with the value 10. To continue the second is a public (+) String and the third is protected (#) String. The last variable is a private (-) int. To go on with the methods there are only two, the first is a public (+) method with a parameter. You have to pay attention to the parameters because the order is changed, the name is in front of the typ! The second method is private (-) but does not have any parameters. I don’t write the type of a parameter or method if it’s void.
In addition in Java or other programming languages there are abstract classes and they could look like that:
public class PrimalCell {
protected String cellType;
private abstract void mitosis();
}
An abstract class has {abstract} or {a} after his name and after each abstract method, have a look at the following picture to get the idea of an abstract class:
Last but not least Java as example has interface classes, interfaces do have a «Interface» or «I» in front of the class name. I think it’s clear how this would look like in a class-diagram.
On the other hand there are also dependencies we can add to our UML-class-diagram. I like to explain dependencies with one big example and two smaller examples.
public class A implements C extends D {
String name;
public B doSomething{
B temp = new B();
...
}
}

As you can see there are four types of arrows.
- full line and closed arrowhead represents a specialization therefore A is-a subclass of D and in fact A is-a D. Mind that the arrow points at the more general type.
- dull line with a diamond represents an aggregation or a composition therefore A has-a String (the diamond points at the class which has. Think of the diamond as a cell with contains the thing on the other end)
- dashed line and closed arrowhead represents an implementation. A implements C (the arrowhead points at the implemented class/interface!)
- dashed line and open arrowhead represents an usage therefore A uses-a B (the arrowhead points at the class which uses). It is not as clear as with the other arrows, what that means. A somehow needs B to fulfill its responsibilities.
To continue, sometimes we have to use an inner class, for example you can use an inner class to implement a queue, the inner class would be an object with a previous and next parameter and the stored data.
Such an inner class would look like this:

The class A would have a inner class called B. Finally I like to mention one last thing, it’s called cardinality. Often it is very useful to point out for example how many Strings the class A has. As a result there are different types of cardinalities.
- 1..* = one to many
- m..n = between m and n (for example 1 to 5 would be 1..5 )
- 0..1 = zero or one
- n = exactly n Elements
- * = any number, including 0.

How to draw class diagrams?
You came to the conclusion, that a class diagram would be helpful for your work.
Maybe you already have some code which you want to extend. In that case you should first translate that into UML. Some tools can do that for you, otherwise keep it simple. Not every method needs to be in there.
If you start with a blank sheet, there are other problems of course. I often find it hard to know where to start. While I’m a big fan of drawing things on paper to come up with ideas, there is a simple fact, that makes me use UML editors instead of it: You can edit. Of course you can kinda edit things on paper too, but it gets messy. So in using an editor like Dia , I can just put some class names in the diagram, that I think I will end up needing. No attributes, no methods, no connections yet. Then I start wondering, what they should perform and give them some methods to do that. Now I start to see, how the classes might interact and what other classes might be useful. As a result I connect the classes the way I think they interact but I don’t need to add every Getter&Setter because they are such basic methods. To go on with I rethink my current diagram and change what I think should be changed. It’s very important at this point of developing that I don’t overload my diagram, an overloaded diagram won’t help anybody and could only lead to a big mess. So delete methods with trivial use and refactor abstract or interface classes out of existing classes or even put some of the details in a diagram on its own. At the current point I often safe my diagram and start over to have a second version with probably new ideas. If you like to do this start over and if you are done with the second diagram try to compare the two files. Like I already mentioned in a previous article my second thoughts are almost the better once xD.
Now you should have a proper class-diagram for your project. At this point you should start writing code and because you can see the important connections of the different classes you should definitive start with writing tests! More about writing test in the next article “Write tests first” next month.
That’s it for this month and I hope you enjoyed the reading ^^
Please leave comments below and stay tuned for the next months article.
cheers d3orn