Professional Documents
Culture Documents
Class Diagrams
Static model type
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
A Class in UML
A rectangle divided into 3 compartments :
A Class in UML
The name compartment
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
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}
A Class in UML
The operation compartment :
A Class in UML
Figure # size : Size # pos : Position + draw() + scaleFigure(percent : Integer = 25) + returnPos() : Position
usage
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
10
Association Relationship
Drawn as line between classes By default bidirectional
11
Example 1
Model the following:
12
Example 1 Solution
association direction
uses Computer
association name
Author
association
13
On association directions...
Author
uses
Computer
Author
uses
Computer
Author
uses
Computer
Author
uses >
Computer
14
Example 2
Model the following:
A user can own 0 or more cars. Each car is owned by 1 or more persons
15
Solution Example 2
Multiplicities
Person 1..*
Owns Owned By
0..*
Car
Common Multiplicities
Multiplicity optional zero or more at least one exactly one Notation 0..1 0..* or * 1..* 1 or left blank
Example 3
Model the following:
18
Recursive Associations
Recursive association: connecting a class to itself
Node *
Connects
19
Example 4
Model the following:
20
Qualied Associations
Species how a certain object at the many end is identied (+/- a key)
Canvas
gure id
Figure
employs > 1
initials RW TM TM
Employee name *
employee Roel Wuyts
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.
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
24
Example 6
Model the following:
25
Example 6: or associations
Put constraint between the associations
Insurance company Insurance contract
0..* {or} 1..* 1..*
constraint
0..*
Person
Company
Example 7
Model the following:
A customer can have multiple insurances. An insurance has an ordered collection of 1 or more customers.
27
constraint
1..*
Customer
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
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
class Queue is needed for the association (can have state/methods needed to implement the algorithm)
4 Elevator
Elevator control
*
Button
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.
32
1..* Passenger
name title
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
1..* Passenger
name title
34
Aggregation
A whole-part association (whole owns the part) Describes different levels of abstraction aggregation
Navy
Contains
Warship
whole
Composition Aggregation`
The parts can only exist if the whole exists
*
Window
Listbox
Button
Menu
36
Composition Aggregation
Other notations expressing the same:
Window
Text
Text
*
Window
Contains
Listbox
*
Listbox
Button
Button
Menu
*
Menu
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.
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
Recap: associations
Connect 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
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
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
43
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>>
<<refine>>
Segment'
(a)
(b)
Segment
length : Number stretch(k : Integer)
stretch(k)::pre:k>0 post:length=length@pre+k
<<refine>>
Segment'
xinitial : Number xfinal : Number moveXini(w) moveXfin(w)
moveXini(w)::post:xinitial=xinitial@pre+w moveXfin(w)::post:xfinal=xfinal@pre+w
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
46
Take care
In older versions of UML, the notation of renement was the notation which is now used for realization
47
48
Wrap-up
Classes have 3 parts: name, attributes, operations They can have relationships
49