UML with Class
Sometimes you want to describe your program to other coders, or maybe you're just designing your code but need some way to structure your thoughts. It's possible you just write an algorithm and that is enough. Other times it may be really important that you have a structure that let yourself and others know what parts of a program will be accessible and what parts should be closed off. With UML (Unified Modeling Language) a coder gets this structure. UML is a way to describe the design of a program and is understood (or easily interpreted) by coders across the spectrum, as it is standardized: ISO/IEC 19501:2005. The latest specification version for UML is 2.5.1.
With UML we can use diagrams to describe a program as well as a database. A diagram describes either a static or a dynamic part of a program. A static diagram can be a simple description of a class, while a dynamic diagram can describe how objects of that class interacts with each other and other parts of the program. In this post I will focus on static diagram as a way to describe a class in Java.
A classy Diagram
A class in Java is often described as a blueprint for objects of that class. Of course, there can be classes that are static in the Java-sense; the class doesn't have instances of it self. A diagram of a class needs to be able to describe such parts as what are static variables vs instance variables. It needs also to be able to describe the accessibility of these variables as well as their type. Classes also have methods! These should be described in a similar way: what parameters do they take and what are their return types?
Let's have a look:
In the diagram above I have described a simple class for a car. This class only has instance variables and methods. Instance components are distinguished from static components by its lack of underlines. If fuel were a static variable it would be underlined. But let's contine to look at fuel. It's in the second cell of the diagram (the first cell is the name of the class). All variables of the class is written in this second cell. fuel is preceded by - sign. This is the access modifier and indicates that this is a private variable.
Following each variable is a colon then the type of variable. fuel has a type of FuelType, meaning that this is a reference variable for a FuelType object. We can see that the other variables are String and double.
In the third cell of the diagram are the methods of the class. First off is a method that is a bit special. We can describe why it's special inside a stereotype with >. The first method is Car, it's a constructor, and it takes one argument of type FuleType. It has no return type. Look at the next method: getFuel(). It takes no argument and it has a return type of FuleType.
We now have the basics for describing Java classes with UML. So let's describe a class for some simple calculations. Since this one will only do arithmetic on values passed to its methods we can have it all static.
I hope you've got a better look on how to describe your Java classes now - the UML way. Stay classy ? Tweet