UML Primer

Simon Lucas
Department of Computer Science
Essex University

These notes provide a very quick overview of a simple subset of the Unified Modelling Language (UML).

We include only the aspects of UML that are most relevant to modelling E-commerce applications.

Use-Case

The Use-Case diagram is very simple.

The following example describes possible use cases for a FAQ (Frequently Asked Questions) list - perhaps for a customer product support system.   Here we see that:

FAQ.gif (9014 bytes)

It is worth stressing that the most important part of UseCases are not the diagrams, but the Use-Cases themselves.  For example, the Use Case for the Search FAQs usage mode might read as follows:

"A user visits the main FAQ search page.  He enters a search string (or regular expression) and hits <enter> or the <search> button.  He is then presented with the total number of matches (e.g. 33), the 10 closest matching FAQs, and a link to the next best 10."

 

Sequences and Collaborations

These diagrams indicate how the key agents in a task interact or collaborate.  Each agent/actor is named at the top of a vertical dashed line, that shows (loosely speaking) how its thread of control unfolds in time in relation to the operations of other agents.

HTTPRequest.gif (10635 bytes)

This sequence diagram has a dual view as a collaboration diagram, which highlights the interactions of the various agents rather than their behaviour through time.  These are also called the flow of control by time and the flow of control by organisation.

 

Class Diagrams

Classes are shown in boxes, with the name at the top.   Consider the following diagram:

HelloServlet.jpg (10031 bytes)

This was derived (reverse engineered) from the following code for a simple 'Hello World' type of Servlet.  Click <here> to test it.

package servlet.test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet {

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

The diagram gives us very quick view of where an object or class fits in with the rest of the system.

Note that with the UML it is up to the modeller to choose which aspects of the system to show in a particular diagram.  In the above case, we simply show that the HelloServlet is a subclass of HTTPServlet and that it depends on the definition of the PrintWriter class in some way.  However, we omitted the superclass of HTTPServlet, and also omitted HTTPServletRequest and HTTPServletResponse.  An object of the HTTPServletClass was explicitly used by the method, and should probably have been included on the diagram.

Relationships

We've already seen some kinds of relationship.   For clarity, we repeat examples of Dependency and Generalisation here in isolation.

Dependency

The following link indicates that:

Dependency.jpg (6938 bytes)

Relating this example to the above code, if the name of the println method of PrintWriter was changed, then HelloServlet code for HelloServlet which uses it would have to be changed accordingly.

Generalisation

Again, this shows that HelloServlet is a subclass of HttpServlet. 

Generalisation.jpg (6832 bytes)

In UML terms, this is called generalisation.  This can be confusing (at least for me!!!).  Let me share this confusion with you, then try to clear it up.

Person.jpg (12258 bytes)

Exercise

Draw a class hierarchy for the following geometric terms:  Triangle, Circle, Shape, Polygon, Square.

Realisation

The realisation relationship is used to indicate that a class implements an interface.  This is drawn just like generalisation, except that a dashed line is used instead of  a solid line.  Suppose that we had the following Java code for an interface called HelloInterface, and a new version of our hello servlet:

 


package servlet.test;
import java.io.PrintWriter;
public interface HelloInterface {
  public void sayHello(PrintWriter out);
}

package servlet.test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet
  implements HelloInterface
{
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setContentType("text/plain");
    sayHello(response.getWriter());
  }

  public void sayHello(PrintWriter out) {
    out.println("Hello World");
  }
}

Please note that I'm not suggesting for a moment that this is a sensible piece of code!  It is intended as a minimal extension of the previous example that introduces the realisation relationship, which leads on to the following diagram.  Note that some UML drawings show interfaces as circles - the tool I used (MagicDraw) uses a rectangle but places a circle inside the rectangle to indicate that it is an interface.

Realisation.jpg (11524 bytes)

Association

Consider the following diagram - this is an approximate model of the relationships between a department, its lecturing staff, its students and the courses that it runs at a typical University.  This was adapted (simplified and modified) from The Unified Modelling User Guide, Booch, Rumbaugh and Jacobson, page 112.

Department.jpg (13921 bytes)

Further Reading

Building Web Applications with UML, Jim Conallen.  Nice introduction to the area with some good example UML diagrams.   Shame about the ASP stuff!

Java Server & Servlets: building portable web applications.  Peter Rossbach and Hendrik Schrieber.  This book is excellent: there are many examples that relate non-trivial server-side Java to UML Class and Sequence diagrams.  Parts of the book are a bit advanced for this course, however.

Acknowledgements

Thanks go to Norbert Voelker for many helpful comments on this guide.

The diagrams were drawn with MagicDraw.