You are on page 1of 28

Object Oriented Programming

Techniques (CMM005)

Lecture 4 – Part 2
Introduction to UML

© The Robert Gordon University


Content
 An Overview of UML
 Visualisation Classes & Objects
 Attributes
 Operations/ Methods

 Relationships

© The Robert Gordon University K. Hui 2008-2009 2


UML Overview
 Unified Modelling Language
 a language for
 visualizing
 specifying
 well-defined semantics behind symbols
 precise, unambigious
 constructing
 easily map a model to a program
 documenting
© The Robert Gordon University K. Hui 2008-2009 3
Classes in UML
 a description of a set of objects that
share the same attributes, operations,
relationships & semantics
 a box with 3 compartments
 name, attributes & operations
 with optional type
 "+" (public), "-" (private), "#" (protected)

© The Robert Gordon University K. Hui 2008-2009 4


Classes in UML (cont'd)
 you don't need to show all attributes/
operations
 usually you CAN'T show all
 an empty compartment doesn't
necessarily mean nothing inside
 you can use "…"
 abstract class name in italic

© The Robert Gordon University K. Hui 2008-2009 5


Class Examples
simple class name Wall
height:float
attributes with type width:float
thickness:float
attribute with type isLoadBearing:boolean=false
and default value

path name java::awt::Rectangle


(with package name)
Shape
origin
move()
operations resize()
display()
© The Robert Gordon University K. Hui 2008-2009 6
Class Examples (cont'd)
 can be drawn only with its name
Temperature
Sensor
java::awt::Rectangle
Customer
Business Rules::FraudAgent
Wall
class with
class with path name
simple name
Figure from: Booch, Rumbaugh & Jacobson. The UML User Guide

© The Robert Gordon University K. Hui 2008-2009 7


Object
 underlying the object's name
object with
name & type
Jan:Customer
Customer
class name:String anonymous
with address:String
attributes :Customer object
phone:String with type

Elyse named object


with type
specified
Figure from: Booch, Rumbaugh & Jacobson. The UML User Guide elsewhere
© The Robert Gordon University K. Hui 2008-2009 8
Attributes
 named properties
 type is optional
 underline static (class-level) attributes
Customer Wall
name height:float
address width:float
phone thickness:float
birthDate isLoadBearing:boolean=false

class with class with attributes


attributes with types & default value
Figure from: Booch, Rumbaugh & Jacobson. The UML User Guide
© The Robert Gordon University K. Hui 2008-2009 9
Operations
 implementation of a service that can be
requested from any object of the class
to affect behaviour
 something that you can do to an object
 share by all objects of the same class
 in Java's term: methods

© The Robert Gordon University K. Hui 2008-2009 10


Operations (cont'd)
 argument & types are optional
Rectangle
TemperatureSensor
add()
grow() reset()
move() setAlarm(t:Temperature)
isEmpty() value():Temperature
class with class with operations
operations and types

Figure from: Booch, Rumbaugh & Jacobson. The UML User Guide
© The Robert Gordon University K. Hui 2008-2009 11
Interface as Stereotype Class

<<interface>>
URLStreamHandler

openConnection()
parseURL()
setURL()
toExternalForm()

© The Robert Gordon University K. Hui 2008-2009 12


Relationships
 a connection among things
 3 most important relationships
 dependency
 association
 generalization
 a 4th one
 realization

© The Robert Gordon University K. Hui 2008-2009 13


Dependency
 a "using" relationship
 a change in 1 thing may affect another
that uses it
 reverse may not be true
 a dashed directed line
 e.g. 1 class uses another class as an
argument in the signature of an operation
(i.e. method argument)
© The Robert Gordon University K. Hui 2008-2009 14
Generalization
 "is-a-kind-of" relationship
 i.e. inheritance
 the superclass is a generalization of the
subclass
 a solid line with a hollow arrowhead
pointing to the parent

© The Robert Gordon University K. Hui 2008-2009 15


Generalization Example
Shape2D

Shape2D()
perimeter()
area()
islarger()

Circle Rectangle
radius:double height,width:double Square
Circle() Rectangle()
perimeter() perimeter() Square()
area() area()
© The Robert Gordon University K. Hui 2008-2009 16
Association
 links between objects
 i.e. instances of classes
 "is-associated-with-a" relationship
 "uses-a", "has-a"
 a solid line
 with optional multiplicity and role names

© The Robert Gordon University K. Hui 2008-2009 17


Association Examples

tutor tutee association


Lecturer Student with role
names

association with
Student is taking Module name &
1..* 6 multiplicity

employee employer association with


Person Company role names &
1..* * multiplicity

© The Robert Gordon University K. Hui 2008-2009 18


Navigability in Association
 Does one object know the associated
object?
 e.g. Can a Student object send messages
to its associated Module objects?
 What about the other way round?
 put an arrow on association line to
represent that it is possible for
messages to be sent in a direction
© The Robert Gordon University K. Hui 2008-2009 19
Navigability Examples
 Module knows about Student, but not
vice versa

Student is taking Module


1..* 6

© The Robert Gordon University K. Hui 2008-2009 20


Association ("Uses-A")
 an object creates other objects to
perform its computation
 e.g. the main method uses other objects

TestEmployee uses Employee

© The Robert Gordon University K. Hui 2008-2009 21


Association ("Has-A")
 aggregation
 "has-a" relationship
 attributes are complex object
 i.e. an object contains other objects
 a "whole" contains "parts"

© The Robert Gordon University K. Hui 2008-2009 22


Aggregation & Composition
 composition is a special type of
aggregation
 the whole strongly own its parts
 if whole is copied/deleted, its parts are
copied/deleted with it

aggregation whole part

composition whole part

© The Robert Gordon University K. Hui 2008-2009 23


Aggregation Examples

whole Company

aggregation
1

*
part Department

© The Robert Gordon University K. Hui 2008-2009 24


Relationship Example

has
0..1
School Department
1 1..* 1..* 1..*
1..*
assignTo
member
1..* 0..1
* 1..*
attends teaches
Student Course Instructor
* * * 1..*
Figure from: Booch, Rumbaugh & Jacobson. The UML User Guide
© The Robert Gordon University K. Hui 2008-2009 25
Realisation
 an interface specifies a contract
 a class that implements an interface
guarantees to carry out the contract

<<interface>>
Observer TargetTracker

update()

© The Robert Gordon University K. Hui 2008-2009 26


The Shape2D Interface
Example

<<interface>>
Shape2Di

Circle Rectangle

© The Robert Gordon University K. Hui 2008-2009 27


Summary
 Representing classes in UML
 class hierarchy using UML notation
 Representing relationships in UML
 dependency
 generalization
 association
 realization

© The Robert Gordon University K. Hui 2008-2009 28

You might also like