Examples: UML: Simple Hello World

Most programming language tutorials start with a very simple “Hello World!” program. This post discusses a very simple “Hello World!” UML model.

  1. UML is a modelling language (Unified Modelling Language). It is a graphical language that is designed for communication about software (though can and is also used for other things).
  2. A model is an abstraction of the real thing, and as such, a UML model is an abstraction of the software which it is describing.
  3. An abstraction of something hides, or simply doesn’t show, certain detail about that something. UML can be used to communicate a variety of different abstractions (views) of the software.
  4. UML is an Object-Oriented modelling languages, hence is best used to model Object-Oriented software. (Although various approaches for using UML to model non Object-Oriented software do exist.)

The Hello World program is, to quote wikipedia,

“used to illustrate to beginners the most basic syntax of a programming language. It is also used to verify that a language or system is operating correctly”

Similarly, I am using it here to illustrate some basic UML syntax. There a many different approaches to using UML. I show here an approach that I like and find to work.

First, let us look at the typical “Hello World!” program written in a number of different programming languages (many others listed here), thus helping us to form a UML abstraction that could be implemented in any of them. [The normal design process would involve constructing the UML model first, but this is not a normal design project.]

Java
public class HelloWorldApp {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
C++
int main() {
  std::cout << "Hello World!" << std::endl;
  return 0;
}
C#
class HelloWorldApp {
  public static void Main(string[] args) {
    System.Console.WriteLine("Hello World!");
  }
}

Once these programs have been compiled, an executable (binary) is formed and this is what the user executes in order to run the program. Our first UML model is a representation of this executable:

The different things that make up a UML model are known as “model elements”. This model element is an called Artifact and has had the stereotype <<executable>> applied to it. [Follow the links for more detail about artifacts or stereotypes.]

This is a simple model, in UML. It abstracts away all the programming detail, and simply represents the executable. I think, however, that a little more model would be useful, perhaps something that shows a bit about the program that is manifest by the executable.

An option would be to add to our model a package, containing a class, which contains a static method,

Picture_of_Code

and we can show that the executable Artifact manifests this package,

Artifacts

However, although this is a perfectly valid UML model, there are a number things about this model that make it unsatisfactory to me,

  • It does not really form much of an abstraction from the code.
  • Neither is it a true representation of the code for each of the three programs shown above (C++, Java C#).
    • the primitive type String is named differently in each programming language
    • there is no class in the C++ version, nor does the code function require parameters

Looking back at our Hello World programs from above, these are all quite acceptable as a first HelloWorld program. However, although they are all written in Object-Oriented (OO) languages, they cannot really be said to be Object-Oriented programs.  They are all very similar to the C version of the program and thus not very Object-Oriented.

C
int main() {
  printf("Hello, world!n");
  return 0;
}

The (static) “main” procedure/method in each of the OO languages is there as an entry point to the program. If we are to execute a true OO program, this entry point procedure should be used to enter the program’s world of objects. In other words it needs to instantiate an initial (entry point) object and start the object executing.

Following this approach, the above 3 examples could be rewritten as follows:

Java
class Greeter {
  public void start() {
    System.out.println("Hello World!");
  }
}

public class HelloWorldApp {
  public static void main(String[] args) {
    Greeter greeter = new Greeter();
    greeter.start();
  }
}
C++
class Greeter {
  public: void start() {
    std::cout << "Hello World!" << std::endl;
  }
};

int main() {
  Greeter greeter;
  greeter.start();
  return 0;
}
C#
class Greeter {
  public void start() {
    System.Console.WriteLine("Hello, world!");
  }
}

class HelloWorldApp {
  public static void Main(string[] args) {
    Greeter greeter = new Greeter();
    greeter.start();
  }
}

Now all three of these follow a similar structure and a better abstraction can be formed.

We add a property to the executable Artifact which indicates the initial object that is instantiated when the program runs,

and we can now provide a class that is a better abstraction of all three programs,

ClassDiagram

This UML model is an abstraction that is only showing two aspects of the Hello World program,

  1. A view of the binary, or executable, artifact that is produced by a program compiler
  2. A class diagram that shows the class and method that is executed.

All other aspects of the program are not shown.

In a future post I will continue to use the Hello World example in order to illustrate the use of other parts of the UML.