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
constructing
well-defined semantics behind symbols precise, unambigious 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
an empty compartment doesn't necessarily mean nothing inside
usually you CAN'T show all
you can use "…"
abstract class name in italic
© The Robert Gordon University
K. Hui 2008-2009
5
Class Examples simple class name attributes with type attribute with type and default value path name (with package name)
operations © The Robert Gordon University
Wall height:float width:float thickness:float isLoadBearing:boolean=false
java::awt::Rectangle Shape origin move() resize() display() K. Hui 2008-2009
6
Class Examples (cont'd)
can be drawn only with its name Temperature Sensor
java::awt::Rectangle
Customer Wall class with simple name
Business Rules::FraudAgent class with path 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
class with attributes
Customer name:String address:String phone:String
Jan:Customer :Customer Elyse
Figure from: Booch, Rumbaugh & Jacobson. The UML User Guide © The Robert Gordon University
K. Hui 2008-2009
object with name & type anonymous object with type named object with type specified elsewhere 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 attributes
class with 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 add() grow() move() isEmpty() class with operations
TemperatureSensor reset() setAlarm(t:Temperature) value():Temperature class with 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 <> 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 radius:double Circle() perimeter() area()
Rectangle height,width:double Rectangle() perimeter() area()
© The Robert Gordon University
K. Hui 2008-2009
Square Square() 16
Association
links between objects
"is-associated-with-a" relationship
i.e. instances of classes "uses-a", "has-a"
a solid line
with optional multiplicity and role names
© The Robert Gordon University
K. Hui 2008-2009
17
Association Examples Lecturer
Student
Person
tutor
tutee
Student
is taking Module 1..* 6 employee employer
1..*
© The Robert Gordon University
*
Company
K. Hui 2008-2009
association with role names association with name & multiplicity association with role names & multiplicity 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
© The Robert Gordon University
is taking Module 1..* 6
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
© The Robert Gordon University
uses
Employee
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 1
part
© The Robert Gordon University
aggregation
* Department
K. Hui 2008-2009
24
Relationship Example has School 1..*
Department 1 1..* 1..*
member
0..1 1..* assignTo 1..*
0..1
1..* * teaches attends 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 <> Observer
TargetTracker
update()
© The Robert Gordon University
K. Hui 2008-2009
26
The Shape2D Interface Example <> Shape2Di
Circle
© The Robert Gordon University
Rectangle
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