Lecture 3 Analysis Class Diagrams
Lecture 3 Analysis Class Diagrams
Dr Ilir Gashi
Centre for Software Reliability
School of Mathematics, Computer
Science and Engineering
City University London
2nd – 4th May, 2014
Acknowledgment
Slides and tutorials are based on materials
prepared by Jim Arlow (Clear View Training)
and Dr Peter Popov (City University London)
2
Objectives
5.5
Objects & abstraction
"An abstraction denotes
the essential
characteristics of an
object that distinguish it
from all other kinds of
objects and thus provide
crisply defined conceptual
boundaries, relative to
the perspective of the
viewer"
(Grady Booch 1994)
5.6
7.2
Encapsulation
operations
attribute values
Data is hidden inside the
object. The only way to
deposit()
access the data is via one
of the operations
This is encapsulation or number = "1243"
withdraw()
data hiding and it is a very
owner = "Jim Arlow"
powerful idea. It leads to
balance = 300.00
more robust software and getOwner()
reusable code.
setOwner()
An Account Object
Messaging
In OO systems, objects send messages to each other over
links
These messages cause an object to invoke an operation
Bank Object Account Object
message
withdraw( 150.00 )
All objects of a particular class have the same set of operations. They are not shown
on the object diagram, they are shown on the class diagram (see later)
Attribute types are often omitted to simplify the diagram
Naming:
object and attribute names in lowerCamelCase
class names in UpperCamelCase
© Clear View Training 2010 v2.6 5.10
7.4
name Window
{author = Jim,
compartment status = tested}
+size : Area=(100,100) initial
#visibility : Boolean = false values
attribute +defaultSize: Rectangle
compartment #maximumSize : Rectangle
-xptr : XWindow*
visibility
adornment +create() class scope (static)
+hide() operation
operation
+display( location : Point )
compartment -attachXWindow( xwin : XWindow*)
java.util
BankAccount BankAccount
name -name:String
«trace»
number -number:String
balance -balance:double = 0
deposit()
withdraw() +BankAccount(name:String, number:String)
constructor +deposit(m:double):void
calculateInterest()
+withdraw(m:double):boolean
A design class must have: +calculateInterest():double
+getName():String
A complete set of operations +setName(n:String):void
including parameter lists, return +getAddress():String
types, visibility, exceptions, set +setAddress(a:String):void
and get operations, constructors +getBalance():double
and destructors
A complete set of attributes
including types and default
values
© Clear View Training 2010 v2.6 5.17
8.3.2
Finding classes
Perform noun/verb analysis on documents:
Nouns are candidate classes
Verbs are candidate responsibilities
Other methods exist (e.g. Class-responsibility-collaboration
(CRC) cards)
Beware of spurious classes:
Look for synonyms - different words that mean the same
Look for homonyms - the same word meaning different things
Look for "hidden" classes!
Classes that don't appear as nouns in the specifications
Use patterns – often domain neutral and reusable
Tangible objects:
one packet of herbal tea
Role objects:
plant operator
quality controller
Interaction objects:
a purchasing transaction
Event objects:
processing plant breaks down
Organisational objects:
Marketing Department
5.22
Worked example
We will look at the vision statement for an
example case study, Town Map
Note that we could also use other documents
as input to this activity, and we would process
them in exactly the same way:
Use cases
Project glossary
Initial statement of the requirements (i.e. an
Informal descriptions of the system)
5.23
Town map example
The system you are analysing is an electronic town map in a public car
park that is designed to show the key points of the town.
The map has a light to identify each key location, such as the hospital and
the library. Below the map is a panel containing buttons labelled with the
location names. When a visitor pushes the button on the panel the light at
the related location lights up for 10 seconds.
A visitor who wants a printed copy of the map can push the "print map"
button, and a hard copy map is released through the map slot.
The system keeps a tally of the number of printed maps requested. If the
machine runs out of paper, a sign saying "Sorry, no printed maps available"
is lit up, until an operator comes and restocks with paper. If the paper gets
low (less than about 100 sheets) an internal "paper low" light illuminates.
When an operator comes to maintain the TownMap they perform two
tasks:
1) Restock with paper if necessary.
2) Press the internal "test lights" button to illuminate all the lights for 1
minute. If any of the lights have burned out, the operator replaces them
5.24
Highlight nouns & verbs
The system you are analysing is an electronic town map in a public car
park that is designed to show the key points of the town.
The map has a light to identify each key location, such as the hospital and
the library. Below the map is a panel containing buttons labelled with the
location names. When a visitor pushes the button on the panel the light at
the related location lights up for 10 seconds.
A visitor who wants a printed copy of the map can push the "print map"
button, and a hard copy map is released through the map slot.
The system keeps a tally of the number of printed maps requested. If the
machine runs out of paper, a sign saying "Sorry, no printed maps available"
is lit up, until an operator comes and restocks with paper. If the paper gets
low (less than about 100 sheets) an internal "paper low" light illuminates.
When an operator comes to maintain the town map they perform two
tasks:
1) Restock with paper if necessary.
2) Press the internal "test lights" button to illuminate all the lights for 1
minute. If any of the lights have burned out, the operator replaces them.
5.25
Noun list
system panel
town map light
public car park location
map map slot
light sign saying "Sorry, no printed maps available"
key location operator
hospital paper
library internal "paper low" light
map operator
panel tally
buttons labelled with the location town map
names lights
visitor operator
button
Printer
5.28
Organizing the candidate classes
The next step
is to create a
class diagram
showing the
candidate
classes and
how they
might be
related
5.29
Find attributes
LocationButton LocationLight
locationName:String locationName:String
The map has a light to identify each key location, such as the hospital and the library.
5.30
Finding operations
LocationLight
turnOn()
turnOff()
You must resolve each verb phrase into one or more operations
on your candidate classes
These operations must be distributed amongst the candidate
classes in a way that makes sense from a business perspective
You may have to introduce new candidate classes to do this!
5.31
Verb list
show the key points of the town
location lights up for 10 seconds
push the "print map" button
a hard copy map is released through the map slot
system keeps a tally of the number of printed maps requested
the machine runs out of paper
sign saying "Sorry, no printed maps available" is lit up
until an operator comes and restocks with paper
paper gets low (less than about 100 sheets)
internal "paper low" light illuminates
Restock with paper if necessary
Press the internal "test lights" button to illuminate all the lights for 1
minute
lights have burned out
operator replaces them
We have to convert these verb phrases into
operations of the candidate classes
5.32
Look for possible generalizations
LocationLight Light
turnOn() turnOn()
turnOff() turnOff()
NoPrintedMapsSign
turnOn() LocationLight PaperLowLight
turnOff()
NoPrintedMapsSign
PaperLowLight
turnOn()
turnOff()
5.34
9.2
What is a relationship?
A relationship is a connection between
modelling elements
In this section we’ll look at:
Links between objects
Associations between classes
Aggregation These are essential in design,
not in analysis. They are not
Composition covered in this module.
Association classes
What is a link?
Links are connections between objects
Think of a link as a telephone line connecting you and a
friend. You can send messages back and forth using this link
Links are the way that objects communicate
Objects send messages to each other via links
Messages invoke operations
OO programming languages implement links as
object references or pointers. These are unique
handles that refer to specific objects
When an object has a reference to another object, we say
that there is a link between the objects
Object diagrams
BookClub
Paths in UML role name
chairperson
ila:Person
drawn as object
link naomi:Person
orthogonal, oblique member
What is an association?
association
Club Person
links
«instantiate» «instantiate» «instantiate»
instantiate
associations
link
chairman
bookClub:Club jim:Person
Association syntax
association
employs name
Company 1
Person
multiplicity *
navigability
role names
employer employee
Company 1
Person
*
Multiplicity
A Company employs many People
employer employee
Company Person
1 *
Each Person works for one Company
BankAccount
Exercise
Model a computer file system. Here
are the minimal facts you need:
The basic unit of storage is the file
Files live in directories
Directories can contain other directories
Use your own knowledge of a specific
file system (e.g. Windows 95 or UNIX)
to build a model
Reflexive associations
subdirectory
0..* 1 0..*
Directory File
0..1
parent
reflexive association
autoexec
C
config
Command
directories files
© Clear View Training 2010 v2.6 5.43
9.4.4
=
address
House Address House
1 1
address:Address
If a navigable relationship has a role name, it is as though the source class has a pseudo-
attribute whose attribute name is the role name and whose attribute type is the target class
Objects of the source class can refer to objects of the target class using this pseudo-attribute
Use associations when:
The target class is an important part of the model
The target class is a class that you have designed yourself and which must be shown on the model
Use attributes when:
The target class is not an important part of the model e.g. a primitive type such as number, string etc.
The target class is just an implementation detail such as a bought-in component or a library component
e.g. java.util.Vector (from the Java standard libraries)
Association classes
* employment *
Company Person
Not on the Person class - there is a different salary for each employment
Not on the Company class - different Person objects have different salaries
The salary is a property of the employment relationship itself
every time a Person object is employed by a Company object, there is a salary
If, however a
particular Person can
have multiple jobs 1 * Job * 1
with the same Company Person
salary:double
Company, then we
must use a reified
association
© Clear View Training 2010 v2.6 5.47
Analysis –
inheritance and polymorphism
Generalisation
A relationship between a more general element and a more
specific element
The more specific element is entirely consistent with the more
general element but contains more information
An instance of the more specific element may be used where
an instance of the more general element is expected
Substitutability
Principle
ancestor
“is kind of”
child
specialisation
A generalisation hierarchy
© Clear View Training 2010 v2.6 5.50
10.3
Class inheritance
Subclasses inherit all features of Shape
origin : Point = (0,0)
their superclasses: width : int {>0}
attributes height : int {>0}
draw( g : Graphics )
operations getArea() : int
relationships getBoundingArea() : int
Overriding
Shape
draw( g : Graphics )
getArea() : int
getBoundingArea() : int
Square Circle
draw( g : Graphics ) draw( g : Graphics )
width x height getArea() : int getArea() : int p x radius2
Square Circle
concrete
classes draw( g : Graphics ) draw( g : Graphics )
getArea() : int getArea() : int
Polymorphism
A Canvas object has a collection of Shape objects
Polymorphism = "many forms" where each Shape may be a Square or a Circle
A polymorphic operation has Canvas
many implementations 1
Square and Circle provide
implementations for the shapes *
polymorphic operations Shape
Shape::draw() and
draw( g : Graphics ) abstract
Shape::getArea() polymorphic
operations getArea() : int superclass
All concrete subclasses of Shape getBoundingArea() : int
must provide concrete draw()
and getArea() operations
because they are abstract in the
superclass Square Circle
For draw() and getArea() we
draw( g : Graphics ) draw( g : Graphics )
can treat all subclasses of
getArea() : int getArea() : int
Shape in a similar way - we
have defined a contract for concrete subclasses
Shape subclasses
© Clear View Training 2010 v2.6 5.54
10.4.1
What happens?
Each class of object
has its own 1.draw()
s1:Circle
implementation of
the draw() operation 2.draw()
s2:Square
On receipt of the :Canvas
draw() message, 3.draw()
BankAccount example
BankAccount
1 * withdraw()
Bank calculateInterest()
deposit()
5.57
Reading Material
5.58