Document 15008314

advertisement
Matakuliah
Tahun
: Konsep object-oriented
: 2009
OBJECT AND CLASES: THE BASIC
CONCEPTS
Pertemuan 8
A Student Guide to Object- Orientated
Systems
Chapter 4 Objects and
Classes: the basic concepts
3
•
Objects and Classes – the basic
concepts
Use cases model the user’s view of the functionality of a
system. Each use case represents a task or major
chunk of functionality
• Object orientated software structure based on objects
• Objects must deliver all the system functionality
4
What is an object?
The most important concept in object-orientated
development
• A representation of something in the application domain
about which we need to store data to enable the system
to provide the required functionality.
5
What is an object?
2 sections, top is the object’s name,
bottom is the object’s attribute values
aBike :Bike
type = men’s
dailyHireRate = £8
deposit = £50
Object name – two optional parts,
both underlined
aBike = this specific object’s name
Bike = the class the object belongs to
The object’s attribute values
Attributes are data items defined as
part of an object or class.
• type is an attribute
• men’s is the value of the ‘type’
attribute for this object
6
More about objects
• Every object belongs to a class
• Objects in an O-O system can represent
– physical things (customers, products,
members, and books),
– conceptual things (orders, loans, reservations
and cancellations)
– organizational things (companies or
departments).
– computer implementation features (GUI
windows, files or linked lists)
7
More about objects
• Every object in a system has three characteristics
– Behaviour, real world objects
have certain attributes and
behaviour
– State, object behaviour may
vary depending on its state
– Identity, each object has a
unique identity and existence
8
Object characteristics – behaviour
Real world objects
• Car has data and characteristic behaviour. Data
– amount of fuel, engine temperature, speed.
Behaviour – stop, start, move.
Object orientated objects
• In OO data becomes attributes
• Behaviour becomes operations (procedures)
• Data and behaviour are packaged together
9
Object characteristics – state
An object’s state is determined by the value of its attributes
e.g. Bank a/c with no overdraft allowed
Attribute determining state = balance
Value of balance attribute = £300
£100
Behaviour –
Withdrawal of £90 allowed
Withdrawal of £150 allowed
denied
10
Object characteristics – identity
• Every object has an identity
• Every object is a unique
• Each object has a separate existence and ultimately
a separate space in memory
• Objects whose attribute values are identical are
totally distinct from one another.
11
Encapsulation
• Encapsulation - packaging related data and
operations together
• Data hiding – making the internal details of an object
inaccessible to another object
• Public interface – provides the services an object
makes available to other objects.
12
Encapsulation
Operations
data
Data inside an object is
surrounded by a
protective ring of
operations
The data is protected by
the operations that
encapsulate it.
Public interface – an object’s name, class and operations,
the only parts of the object accessible to other objects.
13
Class
• An object is defined in terms of its class
• A class of objects is a group of objects with
the same set of attributes, relationships and
behaviour
• An object is an instance of a class
• Instantiation is creating a new object
14
Class
Bike
bike#
available
type
size
make
model
dailyHireRate
deposit
getCharges (no.days)
findBike (bike#)
registerBike (bikeDetails)
getBike# ()
Class name – starts with
capital letter, if two words
‘CarPark’
Attributes – lower
case, no spaces, each
subsequent word starts
with capital letter
Operations – same
notation as
attributes
15
Relationships
Three types of relationships
»Association
»Aggregation
»Inheritance
• These are links between classes that are
used for message passing
• A navigable path between objects
16
Association – a link between two
classes indicating a relationship
studies
Student
0..*
Course
1..*
Association relationship between Student and Course classes
• The association can be named: a student studies a course.
• An association has two ends each attached to a class.
• The numbers and asterisk on the line indicate the multiplicity
of the association.
17
Multiplicity – UML notation
Meaning
an exact
number
many
a specific
range
a choice
unspecified
Example
Notation
exactly one
exactly six
zero or more
one or more
one to four, zero to
six
two or four or five
1 (or may be omitted)
6
0..*
1..*
1..4, 0..6,
an arbitrary,
unspecified number
*
2, 4, 5
18
Aggregation – a whole-part
relationship
Car
4
Wheel
2,4,5
Door
1
Engine
Identified by - 'consists of', 'has a', or 'is a part of‘
Wheels, doors and engine are ‘part’ of a car
19
Composition – tighter
aggregation
Head
3
WierdoRobot
Hand
2
6
Wheel
• the whole object has exclusive ownership of its parts i.e.
the part object can only participate in one aggregation;
• the parts live and die with the whole
20
Inheritance and generalization
StaffCard
VisitorsCard
cardNumber
name
dept
expiryDate
cardNumber
currentDate
delete ()
delete ()
Inheritance – mechanism for defining a new class in
terms of an existing class
Generalization – moving common attributes and
operations in to a more general class
21
Inheritance and generalization
Card
cardNumber
delete( )
VisitorsCard
currentDate
delete( )
StaffCard
name
dept.
expiryDate
Relationship described as
‘is-a’
‘is–a-type-of’
‘is–a–kind–of’
delete( )
•
•
•
•
a specialized class inherits from a general class
a subclass inherits from a superclass
a child class inherits from a parent class
a derived class inherits from a base class.
22
Abstract class – never instantiated
Card
{abstract}
cardNumber
delete()
Inheritance a relationship between classes
Card objects will never be created (instantiated)
23
Polymorphism
• The term polymorphism means the ability for a
single message to produce a different response
depending on the class of the object to which it is
sent.
• Polymorphism linked with an inheritance
hierarchy allows a single message to be
interpreted differently by different objects.
• Which method is executed will depend on which
object receives the message.
• A single operation may be implemented by more
than one method.
24
Download