The UML:
# Provides a standard means of expressing design that reflects the best practices of industry
# Aims to de-mystify the process of software system modelling
# Adopts the best of the Booch, Rumbaugh, & Jacobson (the three amigos) notations
# Has been accepted as a standard by the Object Management Group (OMG) - an industry-wide
body
The process view encompasses the threads and processes that form the systems concurrency and
synchronization mechanisms. This view primarily addresses the performance, scalability, and
throughput of the system.
The implementation view encompasses the components and files that are used to assemble and
release the system. The static aspects of this view are captured in component diagrams; the
dynamic aspects in interaction, statechart, and activity diagrams.
The deployment view encompasses the nodes that form the systems hardware topology. This view
addresses the distribution, delivery, and installation of the system.
UML diagrams
The UML defines various diagrams that can be used to visualize the static and dynamic parts of a
computer system:
Introduction to the UML Page 2
Diagram
Statechart Activity
Accounting System
Trading Manager
Analyze Risks
uses
uses
Valuation
Price Deal
extends
Salesperson
Limits Exceeded
Use cases
A use case describes a sequence of actions that a system performs on behalf of
an actor (see below). In the UML, a use case is represented by an ellipse Deposit money
containing the name of the use case. In the example here (taken from a banking
application), a use case named Deposit money is defined.
Introduction to the UML Page 3
A use case describes what a system does, but does not specify how it does it.
Actors
An actor is a role that a user plays when interacting with the computer system. In this
context, a user may be a human, a hardware device, or even another computer system. In
the UML, an actor is represented by a stick figure with the name of the actor beneath it. In
the example here, an actor named Customer is defined.
System boundaries
A system boundary is exactly what its name implies: the boundary of some Bank
system. In the UML, a system boundary is represented by a rectangular
box with the name of the system inside it. In the example here, the system
boundary of a bank is shown.
Associations
In a use case diagram, an association typically represents a Bank
relationship between an actor and one or more use cases.
Associations are represented in the UML by straight lines Deposit money
connecting the actor to the use cases he or she carries out. In
the example here, the customer deposits money, withdraws
money, and orders a statement all of which are within the
boundaries of the banks system.
Withdraw money
Generalization of actors
In a use case diagram, you can define general types of actors and
specialize them using generalization relationships.
on their intent:
# Extends indicates that the specialized use
case is similar to the general use case but does something more (typically, the extended use
case deals with a variation of the general use case)
Introduction to the UML Page 4
# Uses indicates that the specialized use case is a common part of several use cases and has been
factored out to avoid repetition
In the example here, Deposit money and Withdraw money both extend Money transaction. Order
statement and Money transaction both use Validate account number. Since generalization
relationships are transitive, this implies that Deposit money and Withdraw money also use Validate
account number.
The relationships between the actors and use cases are as follows:
# The Trading Manager sets limits
# The Trader analyses risks, prices deals, and captures deals
# The Salesperson also captures deals
# The Accounting System updates accounts
# Two uses cases (Analyze Risks and Price Deals) use the Valuation use case
# The use case Limits Exceeded extends Capture Deal
Class diagrams
Class diagrams help you to visualize the relationships between classes, interfaces, and
collaborations. Class diagrams show the static (structural) design of a system.
1..*
<member
* 1..*
<teaches
Student * Course *
attends>
-name : std::string -name : std::string
-id : int -id : int
*
Figure 10. Example class diagram (based on Figure 8-3 of Booch et al., 1999)
Classes
In the UML, a class is represented by a rectangle containing the
name of the class. In the example here, a class named Person is Person
defined.
// C++ example
class Person {};
Introduction to the UML Page 5
Associations
An association is a relationship in which an object of one type is connected to an object of another
type.
In many situations, one or more objects of a particular class may be associated with one or more
objects of another class. This is called the multiplicity of the association and is represented by
numbers placed at the ends of the association:
# 1 means exactly one object of this class is involved in the association
# 0..1 means either zero or one objects are involved
# 0..* means zero or more objects are involved
# 1..* means one or more objects are involved
# n (where n is an integer) means exactly that number of objects are involved
Associations that
represent whole/part or
has-a relationships Family Person
between classes are
known as aggregation.
Aggregation is
represented in the UML by adding a diamond next to the aggregate class. In the example here,
Family represents the whole and Person the part.
// C++ example
// decision to use a set is an implementation detail
#include <set>
// C++ example
class Arm {};
class Person
{
// created/destroyed at the same time as Person...
Arm left_arm;
Arm right_arm;
};
Generalization
Generalization is a relationship between classes in which one class (the derived or child class) is
a more specialized version of the other (the base or parent class). Generalization represents is-
a relationships between classes.
A class that has no parents is called a root class and one that has no children is called a leaf.
Introduction to the UML Page 7
// C++ example
class Animal
{
protected:
Animal(); // default ctor accessible to derived classes only
};
class Dog : public Animal {};
class Alsatian : public Dog {};
class Labrador : public Dog {};
Specialization is a transitive relationship: an Alsatian is-a Dog and a Dog is-a(n) Animal implies
that an Alsatian is-a(n) Animal.
// C++ example
class Cricketer {};
class Bowler :
public Cricketer {};
class Batsman :
public Cricketer {};
class AllRounder : AllRounder
public Bowler, public
Batsman {};
# Responsibilities are (optionally) shown in a separate compartment at the bottom of the class
icon
When drawing a class diagram, you may choose to show or hide any of the compartments (other
than the one that names the class, which must always be present).
// C++ example
class ClassShowingVisibilities
{
private:
int private_attribute;
void PrivateOperation();
protected:
int protected_attribute;
void ProtectedOperation();
public:
int public_attribute;
void PublicOperation();
};
// C++ example
class Person
{
private:
int age; // in years
public:
Person(); // set age to 0 years
void CelebrateBirthday(); // increase age by 1 year
void DiscoverSecretOfYouth(); // decrease age by 10 years
void GrowOldFast(int years = 10); // increase age by value of
years
int HowOld() const; // return age
};
Polymorphic operations are chosen at run-time according to the type of object for which the
operation is called. Abstract operations are incomplete, i.e. the operations signature is declared but
no body is defined; whereas concrete operations have both a signature and a body. Abstract
operations are indicated in the UML by writing their names in italics: concrete operations are
indicated by normal text.
Animal
In the example here, Speak is an abstract operation in class Animal but a concrete operation in
classes Dog, Cat, and Mouse. Thus if the operation Speak is called for an object of type Cat, it is
Cats version of Speak that gets called.
Dependencies
Animal
A dependency is a
relationship in which
an object of one
type uses an object
of another type, and
therefore depends
on it. Dependencies
Dog Cat Mouse
are represented in
the UML by a
dashed line with an
open arrow pointing
from the dependent std::ostream
class to the class it
Introduction to the UML Page 10
depends upon. In the example here, Animal, Dog, Cat, and Mouse all depend on std::ostream,
because the latter class is an argument in their operation Speak (which is not shown on this
diagram). If the specification of std::ostream should change, then all of these classes would need to
be re-compiled.
The classes University and Department has operations for adding, removing, and getting the objects
they aggregate. Departments Add() and Remove() operations take a single parameter named who
of type Instructor; the UML diagram does not indicate the return type of Departments operation
GetAllInstructors(), indicating that this is an implementation detail.
Object diagrams
Object diagrams show a set of
objects at a specific moment in
time. : Car : Engine
Objects
Objects are shown by simple
rectangles, with the name and
type of object underlined:
Vauxhall : Car
: Wheel : Wheel : Wheel : Wheel
Where the name of the object
is irrelevant, it can be omitted:
: Car
When the name of the object is relevant, but not its type, the type may be omitted:
Vauxhall :
Introduction to the UML Page 11
Links
Links are shown by solid lines joining the objects which are linked.
Objects
Objects are shown by simple
rectangles, with the name and type of : VideoStore
Look up rental()
Where the name of the object is Return rental()
irrelevant, it can be omitted: Refer to Manager()
Talk to Manager()
: VideoStore Look up tape history()
Negotiate fee()
: Diner : Waiter : Chef
used in place of the rectangle.
Pay()
Order()
Lifelines
Communicate order()
An objects lifeline represents the time
it remains in existence. During that <<create>>()
existence, there are times when the Figure 29. Example sequence diagram (Liberty, 1998, p50)
m : Meal
object will be active (denoted by a
Ready()
wide band) and times when it is
inactive (denoted by a dashed line).
Cook()
Ready()
If an object is created during the time Ready()
span covered by the sequence
diagram, then its symbol is placed at Collect()
the point it first comes into existence Serve()
(see the example showing m : Meal
coming into existence once it has Eat()
Messages
Messages are shown by solid lines
Introduction to the UML Page 12
with solid arrows pointing to the recipient of the message. The duration of a message is represented
by the vertical distance between the beginning and end of the line. Thus, an instantaneous message
is denoted by a horizontal line (e.g. Order() in the example alongside) and a message taking some
time by a sloping line (e.g. Cook()).
Each message has a name which represents the function call that implements the message.
Parameters can be shown within the brackets after the message name, if desired.
Collaboration diagrams
Collaboration diagrams are interaction 1. Report loss of tape()
diagrams that emphasize the 5. Refer to Manager()
rd() l()
a
objects and the links between them.
rn r f rent
eco
o
In addition, collaboration diagrams
6. T Nego ay()
4. R record
10. 11. P
alk
etu
to m te fee
et
2. G
between the objects.
tia
ana ()
ger
()
Objects
()
rder
1. O erve() l()
9. S uest bil 2. C
om
. Req Bill() mu
11
12. ay()
: Waiter 7. R nicate
P ead ord
13. y() er()
: Diner : Chef
8. Coll
R oo y( ()
ect()
6. . C ead are
ea k( )
5 R p
4. Pre
dy )
10
()
. Ea
3.
t( )
m : Meal
Activity diagrams
Activity diagrams show the flow from activity to activity. An activity is an on-going non-atomic
execution within a state machine. Activities ultimately result in some action, which results in a
change of state of the system or the return of a value.
Introduction to the UML Page 14
[no coffee]
Find beverage
[found coffee]
[found cola]
Put coffee in filter Add water to reservoir Get cups Get can of cola
[no cola]
Put filter in machine
Turn on machine
Brew coffee
Initial and final states are represented by black circles without or with
(respectively) a surrounding ring. In the example here, the initial state leads into
the action state Select site and Commission architect is the last action state
before the final state.
Commission architect
Transitions
A transition is represented by a solid line with an open arrow at one end.
In the example above, the activity Find beverage branches according to whether coffee is found or
not. If it is not, a further branch occurs, depending on whether cola is found or not.
Swimlanes
Sometimes it is helpful to place activities in what a re termed swimlanes which represent different
parts of the system. Swimlanes are separated from each other by vertical lines.
References
UML
nd
1. Martin Fowler with Kendal Scott (1999), UML Distilled (2 edition), Addison-Wesley, 224pp,
(ISBN 0-201-65783X), 19
2. Grady Booch, James Rumbaugh, & Ivar Jacobson (1999), The Unified Modeling Language User
Guide, Addison-Wesley, 482pp (ISBN 0-201-57168-4), 38
3. Jesse Liberty (1998), Beginning Object-Oriented Analysis and Design with C++, Wrox Press,
(ISBN 1-861001-33-9), 33
4. Pierre-Alain Muller (1997), Instant UML, Wrox Press, 343pp (ISBN 1-861000-87-1), 21
5. Rob Pooley & Perdita Stevens (1999), Using UML, Addison-Wesley, 255pp (ISBN
0-201-36067-5), 25
Prices quoted by Amazon.co.uk (January 2001)
Introduction to the UML Page 16
Class exercises
Exercise 1
# Identify the actors and use cases for a computerised version of the classic card game Pontoon.
Exercise 2
# Identify a set of classes to implement Pontoon, including the relationships between them. Draw
a class diagram corresponding to these classes and relationships.
# Develop C++ code to implement the class diagram.
Exercise 3
# Draw an object diagram representing a particular moment during a game of Pontoon, when a
player has the following card: Ace of Spades, 3 of Diamonds, and 7 of Hearts
Exercise 4
# Draw either a sequence diagram or a collaboration diagram to represent playing of a single
hand of Pontoon
Introduction to the UML Page 17
Rules of Pontoon
From The Penguin Book of Card Games, by David Parlett (1978), Penguin Books, 474pp (ISBN 0 14
046 344 5)
A banking game for 3-10 players, 4-6 best. Use a standard 52-card pack. Suits are irrelevant: all that
counts is the numerical face-value of the cards:
# From Two to Ten count 2 to 10 respectively
# Court cards count 10 each
# An ace counts either 1 or 11 at its holder's discretion
Banker...
# Shuffles the cards before his first deal, but not thereafter
# Deals one card each, face down, dealer's last
All except the banker may look at their card. Everyone gets a second card and more may be bought
thereafter. The object is to finish with a better hand than the banker's
A hand whose cards total over 21 is bust and loses. A hand totalling 16-21 beats the banker only if
the latter has a lower count or is bust. There are three special hands:
# Pontoon is 21 on two cards
# Royal Pontoon is three Sevens (punter only)
# A five card trick is a hand of five cards that is not bust
Pontoon beats the banker unless he also has a pontoon and wins double. Royal Pontoon beats the
banker and wins treble. Three Sevens for the banker counts as 21 and is beaten by pontoon. A five
card trick beats anything the banker has, except another five card trick, and wins double.
Each punter looks at his first card and stakes upon it an amount that lies within previously agreed
limits, and then gets a second card (as does the banker). If the banker has pontoon he reveals it and
wins, collecting double everyone's stakes. If a punter has pontoon, he turns the ace over and
receives no more cards.
A punter with two cards of identical rank may split them and play two hands, staking the same
amount on the second hand as on the first. The banker may not split. The punter then either:
# Sticks, provided his score is 16 or over
# Buys a card (dealt face down)
# Twists a card (dealt face up)
The punter may not buy after twisting.
When buying a card, the punter pays not less than what he paid for his previous card nor more than
his total stake so far. The fifth card of a five card trick is always dealt face up, even if bought. The
banker plays after all the punter have played and pays only those punters who have beaten his
score.
Introduction to the UML Page 18
Program 1
//----------------------------------------------------------------------------
// Civil Engineering with Computing: OOP with C++, UML, and the STL
// Introduction to the UML
// Copyright: 2001 Geocentrix Ltd. All Rights Reserved.
// Component: Program1.exe
// File: Program1.cpp
// Overview: implementation of Program1
//---------------------------------------------------------------------------
#include <iostream>
using std::cout;
using std::endl;
//---------------------------------------------------------------------------
// class Person
class Person
{
public:
Person() {
cout << "Person born" << endl;
}
~Person() {
cout << "Person dies" << endl;
}
};
//---------------------------------------------------------------------------
// main test program
int main()
{
cout << "About to create a person" << endl;
Person me;
cout << "About to return from main()" << endl;
return 0;
}
//---------------------------------------------------------------------------
// Revision history
// 1.00 29/01/01 AJB modified for Feb 2001 class
// 1.10 28/09/01 AJB modified for Sept 2001 class
//----------------------------------------------------------------------------
Program 2
//----------------------------------------------------------------------------
// Civil Engineering with Computing: OOP with C++, UML, and the STL
// Introduction to the UML
// Copyright: 2001 Geocentrix Ltd. All Rights Reserved.
// Component: Program2.exe
// File: Program2.cpp
// Overview: implementation of Program2
//---------------------------------------------------------------------------
#include <iostream>
Introduction to the UML Page 19
#include <string>
using std::cout;
using std::endl;
using std::string;
//---------------------------------------------------------------------------
// class Person
class Person
{
public:
Person(const string& a_name);
~Person();
string Name() const {
return name;
}
private:
string name;
};
Person::~Person()
{
cout << Name() << " dies" << endl;
}
//---------------------------------------------------------------------------
// main test program
int main()
{
cout << "About to create a person" << endl;
Person me("Andrew");
cout << "My name is " << me.Name() << endl;
cout << "About to return from main()" << endl;
return 0;
}
//---------------------------------------------------------------------------
// Revision history
// 1.00 29/01/01 AJB modified for Feb 2001 class
// 1.10 28/09/01 AJB modified for Sept 2001 class
//----------------------------------------------------------------------------
Program 3
//----------------------------------------------------------------------------
// Civil Engineering with Computing: OOP with C++, UML, and the STL
// Introduction to the UML
// Copyright: 2001 Geocentrix Ltd. All Rights Reserved.
Introduction to the UML Page 20
// Component: Program3.exe
// File: Program3.cpp
// Overview: implementation of Program3
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::ostream;
//---------------------------------------------------------------------------
// class Person
class Person
{
public:
Person(const string& firstname, const string& surname, bool male);
~Person();
string Name() const {
return firstname + " " + surname;
}
unsigned int Age() const {
return age;
}
void CelebrateBirthday() {
++age;
}
bool IsMale() const {
return male;
}
bool IsFemale() const {
return !IsMale();
}
void Speak(ostream& os) const;
private:
string firstname;
string surname;
bool male;
unsigned int age;
};
Person::~Person()
{
Introduction to the UML Page 21
//---------------------------------------------------------------------------
// main test program
int main()
{
cout << "Create a person" << endl;
Person me("Andrew", "Bond", true);
me.Speak(cout);
Program 4
//----------------------------------------------------------------------------
// Civil Engineering with Computing: OOP with C++, UML, and the STL
// Introduction to the UML
// Copyright: 2001 Geocentrix Ltd. All Rights Reserved.
// Component: Program4.exe
// File: Program4.cpp
// Overview: implementation of Program4
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::ostream;
Introduction to the UML Page 22
//---------------------------------------------------------------------------
// class Person
class Person
{
public:
Person(const string& firstname, const string& surname, bool male);
~Person();
string Name() const {
return firstname + " " + surname;
}
unsigned int Age() const {
return age;
}
void CelebrateBirthday() {
++age;
}
bool IsMale() const {
return male;
}
bool IsFemale() const {
return !IsMale();
}
void Speak(ostream& os) const;
bool CanMarry() const;
void Marry(Person& fiancee);
bool IsMarried() const {
return spouse;
}
private:
string firstname;
string surname;
bool male;
unsigned int age;
const Person* spouse;
};
Person::~Person()
{
cout << Name() << " dies" << endl;
}
if(IsMarried()) {
cout << Name() << " is already married!" << endl;
return false;
}
return true;
}
//---------------------------------------------------------------------------
// main test program
int main()
{
cout << "Create a person" << endl;
Person Andrew("Andrew", "Bond", true);
Andrew.Speak(cout);
Jenny.Speak(cout);
uses
Deal card Deal hand
uses
extends extends
Twist
extends Deal
Stick
extends
Play
extends
Betting
Player
Dealer
Collect lost
Place bet Pay won bets
bets