Collaboration diagrams
Interaction diagrams show a series of method invocations among a group of objects.
One type of interaction diagram is a collaboration diagram, which is essentially an object diagram augmented with method invocations.
Consider the following code:
1 2 3 |
Point p = new Point(3,4); Rectangle r = new Rectangle(p, 5, 10); double a = r.getArea(); |
Here is the corresponding collaboration diagram:
Note that a constructor call is depicted by sending a create( ) message to an object that in fact comes into existence as a result of the constructor call.
Imagine a draw(Canvas) method for Rectangle and a drawRect(x, y, w, h) method in Canvas. Here is a
collaboration diagram that shows the method invocations for
drawing a Rectangle:
The corresponding code:
1 2 3 4 5 6 7 8 9 |
class Rectangle { ... public void draw(Canvas c) { double x = ULCorner.getX(); double y = ULCorner.getY(); c.drawRect(x, y, width, height); } ... } |
Sequence diagrams
The other type of UML interaction diagram is the sequence diagram.
A sequence diagram presents the same information shown on a collaboration diagram but in a different format.
Here is a sequence diagram for the rectangle drawing scenario:
The dashed vertical lines are lifelines.
The vertical boxes on the lifelines are activations.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Class diagrams—inheritance A simple example of inheritance in Java: class Clock { private Time currentTime; public void setTime(Time t) { ... } public Time getTime() { ... } } class AlarmClock extends Clock { private Time alarmTime; private boolean alarmOn; public void setAlarmTime(Time t) { ... } public void setAlarm(boolean on_off) { ... } } |
Expressed in UML:
Abstract classes and methods are indicated as being such by
italicizing the name of the class or method:
The code: (Rectangle not shown)
1 2 3 4 5 6 7 8 9 10 11 12 |
abstract class Shape { private String name; Shape(String name) { ... } public abstract double getArea(); public abstract double getPerimeter(); } class Circle extends Shape { private double radius; Circle(String name, double radius) { ... } public double getArea() { ... } public double getPerimeter() { ... } } |
If drawing by hand, add text such as “{abstract}” or just “{a}”
following the class or method name to indicate an abstract member.
Details on dependency and association
If a change in a class B may affect a class A, then it is said that A depends on B.
In Java, creating an instance of a class, or passing a class instance as a parameter creates dependency:
1 2 3 4 5 6 7 8 9 10 |
class A { void f( ) { ... B b = new B(); ...use methods in B... } void g(B b) { ...use methods in B... } } |
Class A depends on class B:
In Booch’s notation, it would be said that A uses-a B.
Two typical implications of class A depending on class B:
- A definition of B, such as a C++ header file or a Java class file, is required to compile A.
- A change in B requires A to be rebuilt
An association is unidirectional if a class C has an attribute of class type D, but D makes no use of C:
1 2 3 4 5 6 7 |
class C { ... private D d1; } class D { ...no usage of C... } |
This diagram shows that there is navigation (visibility) from C to D, but not from D to C:
Association implies dependency.
In Booch’s notation it would be said that C has-a D.
If two classes have attributes referencing objects of each other’s type, a bidirectional association exists:
1 2 3 4 5 6 7 8 |
class E { ... private F f1; } class F { ... private E e1; } |
A bidirectional association, with navigability from each class to the other, is shown by a line with no arrowheads: