You are on page 1of 49

Analyse et Mthodologie Informatiques

UML Class Diagrams


Roel Wuyts
ULB 2005/2006 http://decomp.ulb.ac.be/education/ami0506/

Class Diagrams
Static model type

A view of the system in terms of classes and relationships

Classes not only describe attributes but also behaviour ! Description of object types .

Attributes and behaviour of a type of objects All objects are instances of a certain class

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

A Class in UML
A rectangle divided into 3 compartments :

name, attributes, operations


Name attributes operations

Lets have a look at these three compartments...

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

A Class in UML
The name compartment

Starts with an uppercase Boldface Most of the time a noun

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

A Class in UML
The attributes compartment

Starts with a lowercase Has a type (String, Integer, ) Can have a visibility
public (+), private (-), protected (#)

Default values can be specied Allowed values can be specied ({...}) Class scope is possible (underlined)
5

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

A Class in UML

Invoice + amount : Real + date : Date = Current date + customer : String - administrator : String = "Unspecied" - number of invoices : Integer + status : Status = unpaid {unpaid, paid}

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

A Class in UML
The operation compartment :

Contains the signature of the operation


a return type a name zero or more parameters

Can have a visibility


public (+), private (-), protected (#)

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

A Class in UML

Figure # size : Size # pos : Position + draw() + scaleFigure(percent : Integer = 25) + returnPos() : Position

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

Relationships Between Classes


An association is a connection between classes

usage

A generalization is a relationship between a more general and a more specic element

inheritance

A renement is a relationship between two descriptions of the same thing but at different levels of abstraction A realization is a relationship between elements where one carries out what the other species
Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 9

Associations
Specify structural relationships Species that objects are interconnected Can be implemented in a lot of ways

through instance variables through arguments of methods

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

10

Association Relationship
Drawn as line between classes By default bidirectional

but particular direction can be indicated

Can contain multiplicities

a range that tells us how many objects are linked

9 examples to show different aspects and give some additional information...

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

11

Example 1
Model the following:

An author can use a computer.

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

12

Example 1 Solution
association direction
uses Computer

association name
Author

association

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

13

On association directions...

Author

uses

Computer

Author

uses

Computer

Author

uses

Computer

Author

uses >

Computer

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

14

Example 2
Model the following:

A user can own 0 or more cars. Each car is owned by 1 or more persons

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

15

Solution Example 2
Multiplicities

Person 1..*

Owns Owned By

0..*

Car

A Person Owns 0 or many Cars A Car is Owned by 1 or many Persons


Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 16

Common Multiplicities
Multiplicity optional zero or more at least one exactly one Notation 0..1 0..* or * 1..* 1 or left blank

(See primitive types for all possibilities)


Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 17

Example 3
Model the following:

Nodes can be connected to other nodes.

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

18

Recursive Associations
Recursive association: connecting a class to itself

Node *

Connects

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

19

Example 4
Model the following:

A Canvas contains many Figures which are identied by an identier

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

20

Qualied Associations
Species how a certain object at the many end is identied (+/- a key)

Canvas

gure id

Figure

A Canvas contains many Figures which are identied by a gure id


Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 21

Qualied associations as restrictions


Qualied association subdivides the referenced set of objects into partitions where, viewed from the initial object, each partition may occur only once Example: employees are partitioned: all employees with the same initials belong to one partition
Company name address
initials

employs > 1
initials RW TM TM

Employee name *
employee Roel Wuyts

company ULB ULB UMH

Thierry Massart Tom Mens

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

22

Example 5
Model the following:

An insurance company has insurance contracts, which refer to one or more customers.
A customer has insurance contracts (zero or more), which refer to one insurance company. An insurance contract is between an insurance company and one or more customers. The insurance contract refers to both a customer (or customers) and an insurance company. The insurance contract is expressed in an (zero or one) insurance policy (a written contract of insurance). The insurance policy refers to the insurance contract.

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

23

For example, 5 solution Example the model in Figure 7 can be expressed in natural language:
Insurance policy
is expressed in #

has"

$expresses an

Insurance company

!refers to

Insurance contract
# has

refers to $

Customer

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

24

Example 6
Model the following:

An Insurance contract belongs to a Person or to a Company

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

25

Example 6: or associations
Put constraint between the associations
Insurance company Insurance contract
0..* {or} 1..* 1..*

constraint
0..*

Person

Company

A contract belongs to a Person or a Company


Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 26

Example 7
Model the following:

A customer can have multiple insurances. An insurance has an ordered collection of 1 or more customers.

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

27

Example 7: ordered association


Insurance contract
0..* {ordered}

constraint

1..*

Customer

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

28

Other constraints
Some widely used constraints:
{ordereded}, {or} (same as {xor}) {implicit} : species that the relationship is not manifest but, rather, is only conceptual {changeable}: Links between objects may be added, removed, and changed freely {addonly}: New links may be added from an object on the opposite end of the association {frozen}: A link, once added from an object on the opposite end of the association, may not be modied or deleted

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 29

Example 8
Model the following:

An elevator control manipulates four elevators. On each link between the elevators and the elevator conrol, there is a queue. Each queue stores the requests from both the elevator control and the elevator itself. When the elevator control chooses an elevator to perform a request from a passenger outside the elevator, the elevator control reads each queue and choses the elevator that has the shortest queue. The choice could also be made using some clever algorithm.
30

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

Example 8: association classes


Queue

class Queue is needed for the association (can have state/methods needed to implement the algorithm)
4 Elevator

Elevator control

*
Button

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

31

Example 9
A reservation on a passanger train consists of a passanger (for whom the seat is reserved), a seat (which is being reserved), and a train (time of reservation). Besides such simple reservations, group reservations are also allowed. Who will actually occupy which seat is left over to the group.

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

32

Example 9: ternary associations


ternary association
Train date number Reservation 1 Seat 1..* CarriageNumber Number

1..* Passenger
name title

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

33

n-ary associations
Associations in general relate n classes

Most of them are binary, but three (as in previous example) or more are possible

Can usually be transformed in binary associations:


Train date number 1 * Reservation * * Seat 1..* CarriageNumber Number

1..* Passenger
name title

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

34

Aggregation
A whole-part association (whole owns the part) Describes different levels of abstraction aggregation

Navy
Contains

Warship

whole

part Note: Diamond can only be at one end!


35

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

Composition Aggregation`
The parts can only exist if the whole exists

they are destroyed with the whole


*
Text

*
Window

Listbox

Button

Menu

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

36

Composition Aggregation
Other notations expressing the same:
Window

Text
Text

*
Window
Contains

Listbox

*
Listbox

Button
Button

Menu

*
Menu

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

37

Shared Aggregation

Shared aggregation is a special case of a normal aggregation. A shared aggregation is one in which the parts may be parts in any wholes, as shown in Figure 18. That an aggregation is * shared is shown by its mul tiplicity. * aggregation is shared The Team Part may be part on several wholes other than one (1). if the multiplicity of the wholeside is Person Members Shared aggregation is a special case of a normal aggregation. * : shared aggregation Figure 18 A Shared Aggregate Relationship * *
Team
Members

Person

Figure 19 shows a remix is composed of many sound clips; the same sound track could be a part of many remixes.

Figure 18 A Shared Aggregate Relationship


*
{ordered}

Remix Figure 19 shows a remix is composed of Sound clips clips; the many sound Contains same sound track could be a part of many remixes.
Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 38

Association or Aggregation?
The decision to use association or aggregation is a matter of judgment and is often arbitrary E.g. What type of relationship should be used to model a car with its tires?

If the application is a service center, the only reason you care about the tire is because it is part of the car, so use an aggregation If the application is a tire store, you will care about the tire independent of the car, then the relationship should be an association.
39

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

Recap: associations
Connect classes

between 1, 2, 3, ... classes

Can contain multiplicities, directions, role names Constraints can be used to express additional requirements (more on constraints later) Association classes can be used for more difcult associations that have behaviour from their own Aggregation: part-whole composition

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

40

Generalization
Inheritance, is-a relationship The more specic may be used where the more general is allowed
Vehicle

Car

Boat

Truck

Remember : Using Inheritance for code reuse, or just because it looks nice is a dangerous practice !
Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 41

Generalization
Abstract classes & Polymorphism

Works because of late binding of self/this


Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 42

Renement
A relationship that represents a fuller specication of something that has already been specied at a certain level of detail. Usefull for modelling

a relation between the analysis version and the design version a relation between a clean implementation and an optimized but potentially difcult variation

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

43

Class renement examples


Segment

That is to say, B = A + ({}, {opk}). On the other hand the Class B can be obtained from Class A=(A opk,..., opn}) by replacing an operation opk by its refinement, that can be one or more new operati opkl. That is to say, B= A[opk | opk1, ..., opkl]. For example, figure 1c shows that the operation stre Segment is refined by the operations moveXini and moveXfin through the mapping <stretch(w) = w/2) ; moveXfin(w/2)>.
A
length : Number stretch(k : Integer)

mapping=...
<<refine>>

length = xfinal - xinitial

<<refine>>

Segment'

xinitial : Number xfinal : Number stretch(k : Integer)

(a)

(b)
Segment
length : Number stretch(k : Integer)

stretch(k)::pre:k>0 post:length=length@pre+k

<<refine>>

stretch(k) = moveXini(-k/2) ; moveXfin(k/2)

Segment'
xinitial : Number xfinal : Number moveXini(w) moveXfin(w)

moveXini(w)::post:xinitial=xinitial@pre+w moveXfin(w)::post:xfinal=xfinal@pre+w

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

Figure 1: Abstraction/Refinement relationship between Classes. (a) UML notation. (b) Attribute refinement.

44

Realization
A semantic relationship between classiers in which one classier species a contract that another classier guarantees to carry out service description
IRuleAgent addRule() changeRule() explainAction() AccountBusinessRules

implementor

realization
Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006 45

Implementing realization
In Java: implemented with interfaces
<<interface>> IRuleAgent addRule() changeRule() explainAction() AccountBusinessRules

In C++: implemented with fully abstract classes


IRuleAgent addRule() changeRule() explainAction() AccountBusinessRules

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

46

Take care
In older versions of UML, the notation of renement was the notation which is now used for realization

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

47

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

48

Wrap-up
Classes have 3 parts: name, attributes, operations They can have relationships

Roel Wuyts - ULB - Analyse et Mthodologie Informatiques - 2005/2006

49