Sunteți pe pagina 1din 47

CHAPTER 2

Object-Oriented
Approach

Learning Outcome


Describe the fundamental concepts of objectorientation, including:


Objects and classes
 Generalization, specialization and inheritance
 Encapsulation, Information hiding and message
passing


Identify the justifications for an object-oriented


approach

Objects
An object is:
an abstraction of something in a
problem domain, reflecting the
capabilities of the system to
keep information about it,
 interact with it,
 or both.
Coad and Yourdon (1990)


Objects
Objects have state, behaviour and
identity.
Booch (1994)

State: the condition of an object at any


moment, affecting how it can behave
 Behaviour: what an object can do,
how it can respond to events and
stimuli
 Identity: each object is unique


Examples of Objects
Object

Identity

Behaviour State

A person.

Hussain Pervez.

Speak, walk, read.

Studying, resting,
qualified.

A shirt.

My favourite button
white denim shirt.

Shrink, stain, rip.

Pressed, dirty,
worn.

A sale.

Sale no #0015,
18/05/11.

Earn loyalty points.

Invoiced,
cancelled.

A bottle of
ketchup.

This bottle of
ketchup.

Spill in transit.

Unsold, opened,
empty.

Graphical Representation of an Object

<Object Name>

We use a rectangle to
represent an object and
place the underlined
name of the object
inside the rectangle.

Example:

SV198

This is an object named


SV198.

Class and Instance


All objects are instances of some class
 A Class is a description of a set of objects
with similar:


features (attributes, operations, links);


 Semantics (behaviour of the objects);
 constraints (e.g. when and whether an
object can be instantiated).
OMG (2009)


Graphical Representation of a Class


<Class Name>

Example:

Account

We use a rectangle to
represent a class with
its name appearing
inside the rectangle.

Motorcycle

The notation we used here is based on the industry standard notation


called UML, which stands for Unified Modeling Language.

Class and Instance


An object is an instance of some class
 So, instance = object





but also carries connotations of the


class to which the object belongs

Instances of a class are similar in


their:
Structure: what they know, what
information they hold, what links they
have to other objects
 Behaviour: what they can do


An Object with the Class


Name
<Object Name> : <Class Name>

This notation indicates


the class which the
object is an instance.

Example:

SV198 : BankAccount

This tells an object


SV198 is an instance of
the BankAccount class.

Attribute


Attribute- a named property of a class


that describes a range of values that
instances of the attribute might hold

Attributes are the way classes


encapsulate data

An example: A Class with Attributes

Minus sign
indicates
these are
private
(hidden)

Attributes are properties containing values

Operation


A behavior of an object

Implemented in classes are methods

Methods are identified and invoked by


their signatures, including name,
parameters, and return type

An example: An Operation with Signature and


Method Written in PL/SQL
Signature has
name,
parameters,
return type

Method
implements
the behavior

Messages and Methods




To instruct a class or an object to perform a task, we


send a message to it.
You can send a message only to the classes and objects
that understand the message you sent to them.
A class or an object must possess a matching method to
be able to handle the received message.
A method defined for a class is called a class method,
and a method defined for an object is called an instance
method.
A value we pass to an object when sending a message
is called an argument of the message.

Sending a Message
Message deposit with
the argument 250.00 is
sent to a BankAccount
object SV198.

deposit 250.00
SV198 : BankAccount

Sending a Message and Getting


an Answer
Ask for the current
balance of this
particular account.

getCurrentBalance()
SV198 : BankAccount

current balance

The current balance of


SV198 is returned.

Calling a Class Method


Ask for the maximum
possible speed for all
MobileRobot objects is
returned.

MobileRobot
getMaximumSpeed()

maximum speed

Generalization and
Specialization



Classification is hierarchic in nature


For example, a person may be an employee,
a customer, a supplier of a service
An employee may be paid monthly, weekly or
hourly
An hourly paid employee may be a driver, a
cleaner, a sales assistant

Specialization Hierarchy
More general
(superclasses)

Person

Employee

monthly paid

Customer

weekly paid

Driver

Cleaner

Supplier

hourly paid

Sales
assistant

More specialized
(subclasses)

Generalization and
Specialization
More general bits of description are abstracted
out from specialized classes:
SystemsAnalyst
name
employee-no
startDate
monthlySalary
grade

Driver
name
employee-no
startDate
standardHourlyRate
overtimeRate
licenceType

Specialized (subclasses)
General (superclass)
Employee
name
employee-no
startDate

SystemsAnalyst
monthlySalary
grade

Driver
standardHourlyRate
overtimeRate
licenceType

Inheritance


The whole description of a superclass


applies to all its subclasses, including:
Information structure (including associations)
 Behaviour


Often known loosely as inheritance


 (But actually inheritance is how an O-O
programming language implements
generalization / specialization)


Employee
name
employee-no
startDate

All characteristics of the


superclass are inherited by its
subclasses

SystemsAnalyst
monthlySalary
grade

Driver
standardHourlyRate
overtimeRate
licenceType

Instances of each
subclass include the
characteristics of the
superclass (but not
usually shown like this on
diagrams)

:Driver
name
employee-no
startDate
standardHourlyRate
overtimeRate
licenceType

:SystemsAnalyst

name
employee-no
startDate
monthlySalary
grade

Aggregation and
Composition


In object-oriented terminology, the


relationship of one object to its
component objects is called
aggregation..
aggregation

A strong form of aggregation in which


the life of components relies on the
life of the whole is called composition
composition..
26

Aggregation and
Composition

27

Encapsulation


The characteristic of
object-orientation in
which data and behavior
are bundled into a class
and hidden from the
outside world

Access to the data and


behavior is provided and
controlled through an
objects interface

An example: A Class with Attributes and Operations

Plus sign
indicates
these are
public
(accessible)

Encapsulation: an objects data


is located with the operations that
use it
Layers of an onion
model of an object:
An outer layer of
operation signatures
gives access to middle
layer of operations
which access an
inner core of data

Message from another


object requests a service.

Operation signature is an
interface through which an
operation can be called.

Operations are located


within an object.

Data used by an
operation is located in
the object too

30

Information Hiding: a strong


design principle only an objects
interface is visible to other objects
Layers of an onion
model of an object:
Only the outer layer is
visible to other objects
and it is the only way to
access operations
which are the only
way to access the
hidden data

Message from another object


requests a service.

Operations can only be called


by message with valid
operation signature.

Only objects own operations


can access its data.

Representation of data
is hidden inside object

31

Polymorphism
Polymorphism allows one message to be
sent to objects of different classes
 Sending object need not know what kind
of object will receive the message
 Each receiving object knows how to
respond appropriately
 For example, a resize operation in a
graphics package


32

Polymorphism




The ability for different classes of objects to respond to identical


messages in different ways
Polymorphism = having many forms
Different behaviors for the same message

An example of Polymorphism

Here, each type


of vehicle has
its own version
of
calcPrice()

Component


A replaceable part of a system


providing a clearly defined function
through a set of interfaces

Group of classes working together


toward a common end; a
subsystem

Interface


The mechanism by which users of a


component invoke its behaviors and
manipulate its properties

The interface is implemented by


method signatures

36

An example: An EC System with Four Components

Interfaces are
represented as
small rectangles

Package


A logical grouping of related


analysis or design elements

Group of classes sharing similar


characteristics or purposes

An example of a Package

Any complex system can be decomposed into


subsystems, where each subsystem is modeled as a
package.
Customer
Bank

Account

Object Persistence
Objects lifetime
 require technologies (e.g., data
storage) to provide support for objects
having a longer lifeline longer than the
duration of the process for which they
were created
 To be able to retain and quickly
access the information defined by an
object


40

OO Justification
Encapsulation of data and functions into
object classes, no function without
corresponding data some basic
consistency constraints are insured
 Information hiding is the principle of
concealing the internal data and
procedures of an object
 Interface to reveal as little as possible
about the objects inner working


41

OO Justification


Inheritance allows:
objects to be built from other objects
reusability
 objects to change and evolve over
time changing base classes
changes the properties and attributes
of a class


42

OO Justification


Polymorphism
allows to write generic, reusable code
more easily specify general
instructions and delegate the
implementation details to the objects
involved
 easier maintenance


43

OO Justification
The OO system architecture consists
of a set of interacting objects
 Objects communicate and interact to
fulfill the required system functionality
 OO system eliminates duplicated
effort by allowing classes to share and
reuse


44

Advantages of O-O


Can save effort




Reuse of generalized components


cuts work, cost and time

Can improve software quality


Encapsulation increases modularity
 Sub-systems less coupled to each
other
 Better translations between analysis
and design models and working code


Summary
In this lecture you have learned about:
 The fundamental concepts of O-O
Object, class, instance
 Generalization and specialization
 Message-passing and polymorphism


Some of the advantages and


justifications of O-O

References
Bennett, McRobb and Farmer (2010)
 George, Batra, Valacich and Hoffer
(2007)


S-ar putea să vă placă și