8/2/2019 Wk4 UML AdvClassDiagrams[1]
1/45
www.ddss.arch.tue.nl
7M822
UML Class Diagrams
advanced concepts
15 September 2008
8/2/2019 Wk4 UML AdvClassDiagrams[1]
2/45
www.ddss.arch.tue.nl
7M822
UML Class Diagrams
The class diagram provides a static structure of all theclasses that exist within the system. Classes are arrangedin hierarchies sharing common structure and behaviourand are associated with other classes.
Class diagrams are used in:
Analysis To build a conceptual domain model with semanticassociations between concepts
Design Structural model of a design in terms of class interfaces Implementation Source code documentation, exposing the
implementation
8/2/2019 Wk4 UML AdvClassDiagrams[1]
3/45
www.ddss.arch.tue.nl
7M822
An object is a thing that can be tangible orintangible.Examples tangible: bike, car, house
intangible: order, account
Objects
An object has properties.Example
a Customerhas aname and address
Customer
NameAddress
8/2/2019 Wk4 UML AdvClassDiagrams[1]
4/45
www.ddss.arch.tue.nl
7M822
Property of object = attribute+ value
(Multiple) Objects
Adacs: Customer
Name = AdacsAddress = Enschede
Primo: Customer
Name = PrimoAddress = Grave
Multiple objects unique identity1247: Customer
Custno = 1247Name = AdacsAddress = Enschede
9378: Customer
Custno = 9378Name = PrimoAddress = Grave
8/2/2019 Wk4 UML AdvClassDiagrams[1]
5/45
www.ddss.arch.tue.nl
7M822
Link = connection between objects
Connection between Objects
1247: Customer
Custno = 1247
Name = AdacsAddress = Enschede
345T7: Order
Orderno = 345T7
dateReceived = 10/10/05Price = 1987
902U2: Order
Orderno = 902U2dateReceived = 07/04/05Price = 3789
8/2/2019 Wk4 UML AdvClassDiagrams[1]
6/45
www.ddss.arch.tue.nl
7M822
Class class is abstract concept each object is instance of a class classes classify objects
attribute has no values
Class
Customer
CustnoNameAddress
Order
OrdernodateReceivedPrice
Object analysis than values to attributes
8/2/2019 Wk4 UML AdvClassDiagrams[1]
7/45
www.ddss.arch.tue.nl
7M822
Attribute has no valuesAssociation is relation between classes
Class Diagram
Customer
CustnoNameAddress
Order
OrdernodateReceivedPrice
Link is instance of association
1 *
8/2/2019 Wk4 UML AdvClassDiagrams[1]
8/45
www.ddss.arch.tue.nl
7M822
Class diagram
A class diagram describes the type of objects in a systemand the static relationships between them.
Static relations:
Associations
Subtypes
Static structure:
Attributes
Operations
8/2/2019 Wk4 UML AdvClassDiagrams[1]
9/45
www.ddss.arch.tue.nl
7M822
Class operations
An operation is the implementation of a service that can be requestedfrom any object of the class in order to affect behaviour.
Operations are used to manipulate the attributes or to perform other
actions. Operations are normally called functions, but they are inside aclass and can be applied only to objects of that class.
An operation is described with a return-type, a name and zero or moreparameters. Together, the return-type, name, and parameters arecalled the signature of the operation.
The signature describes everything needed to use the operation.
8/2/2019 Wk4 UML AdvClassDiagrams[1]
10/45
www.ddss.arch.tue.nl
7M822
8/2/2019 Wk4 UML AdvClassDiagrams[1]
11/45
www.ddss.arch.tue.nl
7M822
Class - associations
Associations are structural relationships where instances (objects) ofone class are connected to instances (objects) of another class.
an employee works for a company
a company has a number of offices An association is normally bidirectional, which means that if an object
is associated with another object, both objects are aware of each other(navigation is bidirectional by default).
Binary association connects exactly two classes. N-ary connects many
classes. The most common association is just a connection between classes.
8/2/2019 Wk4 UML AdvClassDiagrams[1]
12/45
www.ddss.arch.tue.nl
7M822
Association adornments: name, role
The association has a name - the descriptive term, often a verb, forthe association.
Each association has two association ends; each end is attached to
one of the classes in the association. An end can be explicitly namedwith a label. This label is called a role name (association ends areoften called roles).
Person Company
employee employer
works for
Person Company
employee employer
has employment for
8/2/2019 Wk4 UML AdvClassDiagrams[1]
13/45
www.ddss.arch.tue.nl
7M822
Associations: multiplicity
Multiplicity defines the number ofobjects associated with aninstance of the association.
Default of 1 (1: 1) 0 or 1: 0..1
Zero or more (0..infinite): *
1 or more (1..infinite): 1..*
n..m; range from n to m inclusive
Car Person
transports
passenger
Car Person
transports
passenger
5
Car Persontransports
passenger
*
Car Person
transports
passenger
1..*
Car Person
transports
passenger
2..5
8/2/2019 Wk4 UML AdvClassDiagrams[1]
14/45
www.ddss.arch.tue.nl
7M822
dd h l
8/2/2019 Wk4 UML AdvClassDiagrams[1]
15/45
www.ddss.arch.tue.nl
7M822
Generalization
A specialization / generalizationrelationship, in which objects ofthe specialized element (child)
are substitutable for objects ofthe generalized element(parent). Superclass the generalization of
another class, the child.
Subclass the specialization of
another class, the parent.
Customer
Corporate
Customer
Personal
Customer
dd h l
8/2/2019 Wk4 UML AdvClassDiagrams[1]
16/45
www.ddss.arch.tue.nl
7M822
Generalization - characteristics
Identify common features concerning behaviour andknowledge. Define these common features on a higherlevel in the inheritance hierarchy.
The aim is at behaviour more than knowledge whencombining classes.
Generalization is a bottom-up process.
A superclass includes all common properties of itssubclasses.
dd h t l
8/2/2019 Wk4 UML AdvClassDiagrams[1]
17/45
www.ddss.arch.tue.nl
7M822
Specialization - characteristics
Define a new class which is a special appearance of anexisting class.
Specialization is a top-down process.
A subclass can have attributes and operations that arespecific for that sub-class.
A subclass may redefine operations of its super-class
dd h t l
8/2/2019 Wk4 UML AdvClassDiagrams[1]
18/45
www.ddss.arch.tue.nl
7M822
dd h t l
8/2/2019 Wk4 UML AdvClassDiagrams[1]
19/45
www.ddss.arch.tue.nl
7M822
Constraints
The basic constructs of association, attribute, andgeneralization do much to specify important constraints.
They cannot indicate every constraint. These constraintsstill need to be captured; the class diagram is a goodplace to do that.
The UML allows you to use anything to describeconstraints. The only rule is that you put them inside
braces { }.
dd h t l
8/2/2019 Wk4 UML AdvClassDiagrams[1]
20/45
www.ddss.arch.tue.nl
7M822
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
21/45
www.ddss.arch.tue.nl
7M822
Attribute analysis
It is not always clear which attributes belongs to which classes byfinding out the class attributes. An attribute is assigned to that classwhere it is certainly a feature.
For example: a project leader has the attributes name, departmentand age.
But what to do with the attributes project number, project duration,starting time, and budget? These attributes clarifies something aboutthe relation between project leader and type of project.
This often happens if a n:m relation refers to an association betweentwo classes. In that case, we can define a new class, for instanceproject management.
8/2/2019 Wk4 UML AdvClassDiagrams[1]
22/45
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
23/45
www.ddss.arch.tue.nl
7M822
Aggregation and Composition: example
Chess set
Board
+kleur() : {w,b}
-column : A..H
-row : 1..8
Field
-color : {w,b}
Piece
1
1
1
*
1 *64 1 0..1
Pawn Bishop Knight Rook Queen King
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
24/45
www.ddss.arch.tue.nl
7M822
Object diagram
An object diagram is a diagram that shows a set ofobjects and their relationships at a point in time; objectdiagrams address the static design view or static process
view of a system.
An object diagram is shown as a class, and the name isunderscored, although an objects name can be shown
optionally preceding the class name as: objectname:
classname. The object does not have to be named, inwhich case only the classname is shown underscored.
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
25/45
www.ddss.arch.tue.nl
7M822
Object diagram: example
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
26/45
www.ddss.arch.tue.nl
7M822
Advanced Class Features
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
27/45
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : Visibility
Visibility class members (attributes, operations) may bespecified as:{we have feature f, an attribute or operation that is defined on an object O ofclass C }
+ :public{f is publicf is visible to any object and f is inherited by the subclasses of C.}
# : protected{f is privatef is visible only to O and f is not inherited by the subclasses of C.}
- : private
(f is protectedf is visible only to objects of class Cand to objects of Cssubclasses and f is inherited by the subclasses of C.}
Restricting visibility is the same as restricting accessibility.
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
28/45
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : Scope
Individual member data (= attributes) may have:
Class scope : a single copy of an attribute is shared by allinstances of a class.
Instance scope : each instance of a class would have its own copyof the attribute.
8/2/2019 Wk4 UML AdvClassDiagrams[1]
29/45
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
30/45
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : Abstract
An operation is abstract if it has no implementation.
A abstract class cannot have any direct instances.
Abstract classes only occur in the context of aninheritance hierarchy.
Abstract operations and classes are specified by
writing its name in italics.
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
31/45
www.ddss.arch.tue.nl
7M822
Advanced Class Concepts : Abstract cont.
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
32/45
www.ddss.arch.tue.nl
7M822
Interfaces and Abstract Classes
One can vary the interfaces of classes independent ofthe implementation.
A pure interface is a class with no implementationand, therefore has operation declarations but nomethod bodies an fields.
Interfaces are often declared through abstract
classes.
www ddss arch tue nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
33/45
www.ddss.arch.tue.nl
7M822
Window as Abstract Class
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
34/45
www.ddss.arch.tue.nl
7M822
Interfaces and Abstract Class
Sub classing is not only the way to do this.
For instance Java provides an interface construct, and the compilerchecks that the implementing class provides implementations of all the
interfaces operations.
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
35/45
www.ddss.arch.tue.nl
7M822
Lollipop notation for Interfaces
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
36/45
www.ddss.arch.tue.nl
7M822
Aggregation and Composition once more
Aggregation is a special form of association that specifies awhole-part relationship between the aggregate (the whole) anda component (the part); aggregation is the part-of relationship.{it is a special form of association in which a collection of objects, each havingan independent existence, is associated with an single object} {unfilleddiamond}
Composition is a form of aggregation with strong ownershipand coincident lifetime of the parts by the whole; the part objectmay belong to only one whole the parts are usually expectedto live and die with the whole.
{usually, any deletion of the whole is considered to cascade to the parts}{filleddiamond}
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
37/45
ddss a c tue
7M822
Aggregation and Composition once more, cont.
Computer system
Monitor
Building
Floor
Computer system
Monitor
Building
Floor
1
1
1
1..*
1
1..2
1
*
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
38/45
7M822
Aggregation and Composition: Example
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
39/45
7M822
Alternative notation for Composition
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
40/45
7M822
Derived Associations and Attributes
Derived associations andattributes can be calculated froother associations andattributes, respectively, on aclass diagram.
For example, an age attributeof a Person can be derived ifyou know that Persons date of
birth.
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
41/45
7M822
Derived Associations and Attributes example
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
42/45
7M822
Qualified Associations
The qualifier specifies how a specific object at the manyend of the association is identified, and may be seen as akind of key separating all the objects in the association.
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
43/45
7M822
Relations and Constraints
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
44/45
7M822
Reflexive Associations
A class has an association to itself.
Example: A directory may contain other directories.
Directory File
0..1parent
subdirectory
0..*
1 0..*
www.ddss.arch.tue.nl
8/2/2019 Wk4 UML AdvClassDiagrams[1]
45/45
References
Sommerville, Ian (2001)
Software Engineering, 6thedition
http://www.software-engin.com
Grady Booch, James Rumbaugh, Ivar Jacobson (2005)
The Unified Modeling Language User Guide, 2ndedition
Martin Fowler (2000, 2004)
Object-Oriented Software Engineering, 2ndedition; 3rdedition
http://www.software-engin.com/http://www.software-engin.com/http://www.software-engin.com/http://www.software-engin.com/