OOAD Lect4 ClassModelling Part1
OOAD Lect4 ClassModelling Part1
OOAD Lect4 ClassModelling Part1
Lecture 4
Class Modelling – Part 1
1
Contents
Class diagrams
– Describe the use of the following relationships
• Association
–Multiplicity
–Naming
–Direction
– UML@Classroom – 4.2
2
Main Purposes of Class Diagrams
• Used to document the classes that make up the system
• Class diagrams show the static structure of the classes in the system
4
Modelling an individual Class
Only class name Attributes
shown shown
Reservation
Reservation startDate
endDate
BankAccount
Class name
Balance
Interest Rate
Attributes Review Date
Name
CreditAccount()
DebitAccount()
Operations OpenAccount()
CloseAccount() 6
Attribute Type - Java
7
Attributes of a Class
Attribute type
Reservation
Attribute
visibility
-reservationNumber: integer
-guest: Customer
-roomNumber: integer
-startDate: Date
-endDate: Date
Attribute
name
+calculateBill()
8
Encapsulation
• Objects are able to collaborate through behaviour and
attributes which are public
Reservation
-reservationNumber: integer
-guest: Customer
-roomNumber[0. . 20]: integer
-startDate: Date = today
Derived Attribute -endDate: Date
(the value can be -/lengthOfStay: integer
derived from other Default
attributes) value
Indicated using +calculateBill()
forward slash (/)
11
Valid Operation annotation
parameter type
Operation return type
+getCustomerName(): String
+calculateBill(): double
Visibility +setStartDate(stayStart:Date=today)
Private(-)
Public(+)
Protected(#)
Reservation
-reservationNumber: integer
-guest: Customer
-roomNumber[0. . 20]: integer
-startDate: Date = today
-endDate: Date
-/lengthOfStay: integer
+getCustomerName(): String
+calculateBill(): double
+setStartDate(stayStart:Date=today)
13
Class Modelling
14
Example of a diagram of a single class
Customer
-lastname: string
-firstname: string
-dateOfBirth: Date
-/age: integer
+ isPensioner(): boolean
+dispatch()
+close()
1 .. *
Order Line
-quantity : Integer
-price : Money Company Customer Personal Customer
contactName creditCardType
* creditRating creditCardNumber
creditLimit
1
Product Note that these classes are incomplete. They
would have more attributes and operations,
16
but for space reasons only a sample is given
Relationships
17
Associations
UML@Classroom – 4.2.5
• Associations represent relationships between instances of a class (i.e. between
objects).
• In a running object-oriented programming, messages are passed and received
to and from instances (objects) of the classes in the system.
• Where instances of one class pass messages to instances of another class, an
association is implied between those two classes.
Customer Order
0 to many
Customer 1 * Order
19
Associations - Multiplicity
The multiplicity of an association-end indicates the potential number of
objects at that end of an association, which may be linked to a SINGLE object
at the OPPOSITE end of the association.
ONE
Customer 1 * Order
ONE
Customer 1 .. 3 * Order
In this example :
ONE Customer can have zero, one, or many Orders
ONE Order can only have one, two or three Customers
22
To keep this example simple - Shows Multiplicity from ‘A to B’ ONLY
….. NOT ‘B to A’ ( but MUST always be shown in ALL class Diagrams
1 One A is always associated
A B
with one B
1..*
A B One A is always associated
with one or more B
0..1
A B One A is associated with
zero or one B
24
Associations can be named
An association can be labeled with a name to indicate the nature
of the association.
The name must start with a capital
The name should be a verb or a verb phrase
There should be a filled-in arrowhead to indicate the direction in which
the text name should be interpreted.
Customer 1 * Order
or
having no arrows (however having no arrows can also mean that the
diagram is not showing navigability)
Customer 1 * Order
27
From Programming 1
28
From Programming 1
29
From Programming 1
30
Generalisation (Inheritance)
• This will covered in greater detail next week
• Generalisation is the other form of relationship, in
class diagrams, that we’re going to look at.
• In object-oriented programming this is usually
referred to as inheritance.
• The UML refers to it as Generalisation.
• It is where one class is a specialized form of another
– sometimes referred to as a “kind of” relationship.
31
Generalisation in “Employee” class
Employee
-employeeNo
-lastName
-firstName
-address
+getsalary()
+calculateLeaveLeft()
33
Partial Sample Solution
Department 1 4
Course
1 .. *
12..24
Module
34