Introduc - On To Object - Orienta - On: Baris Aktemur CS1003 - Ozyegin University
Introduc - On To Object - Orienta - On: Baris Aktemur CS1003 - Ozyegin University
Introduc.on
to
Object-Orienta.on
Baris
Aktemur
CS1003
|
Ozyegin
University
Whats
an
Object
An
object
packages
both
data
and
the
procedures
that
operate
on
that
data.
An
object
performs
an
opera.on
when
it
receives
a
request
(or
message)
from
a
client.
2/10/11
OO
Design
is
hard
Many
factors
come
into
play:
encapsula.on,
granularity,
dependency,
exibility,
performance,
evolu.on,
reusability,
etc.
Many
dierent
approaches.
Write
a
problem
statement,
single
out
the
nouns
and
verbs,
and
create
corresponding
classes
and
opera.ons.
Focus
on
the
collabora.ons
and
responsibili.es
in
the
system.
Model
the
real
world
and
translate
the
objects
found
during
analysis
into
design.
OO
Design
is
hard
Many
real-world
objects,
but...
OO
designs
oYen
end
up
with
classes
that
have
no
counterparts
in
the
real
world.
Strict
modeling
of
the
real
world
leads
to
a
system
that
reects
today's
reali.es
but
not
necessarily
tomorrow's.
The
abstrac.ons
that
emerge
during
design
are
key
to
making
a
design
exible.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
4
2/10/11
Interface
Every
opera.on
declared
by
an
object
species
the
opera.on's
name,
the
objects
it
takes
as
parameters,
and
the
opera.on's
return
value.
This
is
known
as
the
opera.on's
signature.
The
set
of
all
signatures
dened
by
an
object's
opera.ons
is
called
the
interface
to
the
object.
An
object's
interface
says
nothing
about
its
implementa.ondierent
objects
are
free
to
implement
requests
dierently.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
5
Type
A
type
denotes
a
par.cular
interface.
An
object
is
said
to
have
the
type
C
if
it
accepts
all
requests
for
the
opera.ons
dened
in
the
interface
named
C.
An
object
may
have
many
types.
Widely
dierent
objects
can
share
a
type.
A
type
is
a
subtype
of
another
if
its
interface
contains
the
interface
of
its
supertype.
A
subtype
inherits
the
interface
of
its
supertype.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
6
2/10/11
Encapsula.on
Requests
are
the
only
way
to
get
an
object
to
execute
an
opera.on.
Opera.ons
are
the
only
way
to
change
an
object's
internal
data.
Because
of
these
restric.ons,
the
object's
internal
state
is
said
to
be
encapsulated
it
cannot
be
accessed
directly
its
representa.on
is
invisible
from
outside
the
object.
Dynamic
Binding
When
a
request
is
sent
to
an
object,
the
par.cular
opera.on
that's
performed
depends
on
both
the
request
and
the
receiving
object.
Dierent
objects
that
support
iden.cal
requests
may
have
dierent
implementa.ons.
The
run-.me
associa.on
of
a
request
to
an
object
and
one
of
its
opera.ons
is
known
as
dynamic
binding.
(or
dynamic
dispatch)
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
8
2/10/11
Polymorphism
Issuing
a
request
doesn't
commit
you
to
a
par.cular
implementa.on
un.l
run.me.
Polymorphism:
being
able
to
subs.tute
objects
that
have
iden.cal
interfaces
for
each
other
at
run-.me.
Simplies
the
deni.ons
of
clients
Decouples
objects
from
each
other
Lets
objects
vary
their
rela.onships
to
each
other
at
run-.me.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
9
Polymorphism
From
Design
Paherns
Explained
by
Shalloway
and
Troh:
Polymorphism:
Being
able
to
refer
to
dierent
deriva.ons
of
a
class
in
the
same
way,
but
gejng
the
behavior
appropriate
to
the
derived
class
being
referred
to.
10
2/10/11
Object
Implementa.on
A
class
denes
the
objects
internal
data
representa.on
opera.ons
11
Subclassing
Inheritance:
Subclass
inherits
all
the
deni.ons
(data
and
opera.ons)
of
the
superclass.
(mod.
visibility)
2/10/11
Abstract
Class
An
abstract
class
denes
a
common
interface
for
its
subclasses.
Defers
some
or
all
of
its
implementa.on
to
opera.ons
dened
in
subclasses
abstract
opera9ons
Cannot
be
instan.ated.
Classes
that
aren't
abstract
are
called
concrete
classes.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
13
Abstract Class
14
2/10/11
Mixin
Class
A
mixin
class
is
a
class
that's
intended
to
provide
an
op:onal
interface
or
func.onality
to
other
classes.
Similar
to
an
abstract
class:
not
intended
to
be
instan.ated.
15
16
2/10/11
Interface inheritance (or subtyping) describes when an object can be used in place of another.
17
18
2/10/11
Ideally
a
subclass
should
add
or
override
opera.ons
and
should
not
hide
opera.ons
of
the
parent
class.
All
subclasses
become
subtypes
of
the
abstract
class,
sharing
the
same
interface
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
19
20
10
2/10/11
Reuse
Mechanisms
(Class)
Inheritance
White-box
reuse
Composi.on
Black-box
reuse
Must
have
well-dened
interfaces
22
11
2/10/11
Class
inheritance
+
Straighmorward
+
Easy
to
modify
the
implementa.on
being
reused
Cant
change
the
implementa.on
dynamically
breaks
encapsula.on:
superclass
is
exposed
to
subclass;
changing
superclass
likely
to
force
a
change
in
subclasses
23
Composi.on
+
Strong
encapsula.on
+
Easily
change
the
implementa.on
at
run.me
+
Fewer
implementa.on
dependencies
+
Keep
class
hierarchies
manageably
small
Design
interfaces
very
carefully
Many
more
objects
and
rela.ons
24
12
2/10/11
25
Delega.on
Achieving
inheritance
reuse
via
composi.on
A
receiving
object
delegates
opera.ons
to
its
delegate.
Receiver
passes
itself
to
the
delegate
similar
to
referring
to
the
receiver
object
using
this
in
inheritance
Delega.on
is
a
good
design
choice
only
when
it
simplies
more
than
it
complicates.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
26
13
2/10/11
Delega.on
Associa.on
An
associa9on
is
a
rela.onship
between
two
classes
that
allows
instances
created
from
those
classes
to
send
messages
to
each
other.
[Robert Mar.n, Agile Principles, Pa6erns and Prac:ces in C#, Pren.ce Hall, 2006 ]
28
14
2/10/11
Associa.on
The
navigability
of
the
associa.on
can
be
restricted
by
adding
arrowheads
to
the
associa.ons.
When
an
arrowhead
is
present,
the
associa.on
can
only
be
navigated
in
the
direc.on
of
the
arrow.
This
means
that
the
class
to
which
the
arrow
points
does
not
know
about
its
associate.
[Robert Mar.n, Agile Principles, Pa6erns and Prac:ces in C#, Pren.ce Hall, 2006 ]
29
Aggrega.on
An
aggrega9on
is
a
special
form
of
associa.on.
Aggrega.on
implies
a
whole/part
rela.onship.
The
class
adjacent
to
the
white
diamond
is
the
whole,
and
the
other
class
is
its
part.
The
whole/part
rela.onship
is
purely
connota.ve;
there
is
no
seman.c
dierence
from
associa.on.
[Robert Mar.n, Agile Principles, Pa6erns and Prac:ces in C#, Pren.ce Hall, 2006 ]
30
15
2/10/11
Composi.on
Composi9on
is
a
special
form
of
associa.on.
It
implies
that
the
whole
is
responsible
for
the
life.me
of
its
part.
This
responsibility
does
not
imply
either
crea.on
or
dele.on
responsibility.
Rather
it
implies
that
the
whole
must
see
to
it
that
the
part
is
somehow
deleted.
[Robert Mar.n, Agile Principles, Pa6erns and Prac:ces in C#, Pren.ce Hall, 2006 ]
31
Caveat
In
GOF,
composi.on
is
called
aggrega.on,
and
aggrega.on
is
called
composi.on.
A
mo.va.on
for
why
UML
was
needed
32
16
2/10/11
Each
design
pahern
ensures
that
a
system
can
change
in
some
specic
ways.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
33
17
2/10/11
Algorithmic
dependencies.
make
algorithm
interchangeable
Tight
coupling
use
techniques
such
as
abstract
coupling
and
layering
to
promote
loosely
coupled
systems.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
35
36
18
2/10/11
Frameworks
A
set
of
coopera.ng
classes
that
make
up
a
reusable
design
for
a
specic
class
of
soYware
ar.s.c
drawing,
music
composi.on,
and
mechanical
CAD,
nancial
modeling
Customize
a
framework
to
a
par.cular
applica.on
by
crea.ng
applica.on-specic
subclasses
of
abstract
classes
from
the
framework.
[Gamma,
Helm,
Johnson,
and
Vlissides.
Design
Pa6erns.
Addison-Wesley,
1994]
37
Frameworks
A
framework
is
a
par.ally
complete
soYware
(sub)system
that
is
intended
to
be
instan.ated.
It
denes
the
architecture
for
a
family
of
(sub) systems.
It
also
denes
the
places
where
adapta.ons
for
specic
func.onality
should
be
made.
In
an
OO
environment,
a
framework
consists
of
abstract
and
concrete
classes.
[Buschmann
et
al.,
POSA]
38
19
2/10/11
Frameworks
Framework
dictates
the
architecture
of
the
applica.on
Applica.on
designer
concentrates
on
the
specics
of
the
applica.on.
Frameworks
emphasize
design
reuse
over
code
reuse
39
Inversion
of
Control
Library:
You
write
the
main
method
and
call
the
library
Framework:
You
reuse
the
main
body
and
write
the
code
it
calls.
40
20
2/10/11
Hollywood
Principle
Dont
call
us,
we
will
call
you.
AKA
Greyhound
Principle:
Leave
the
driving
to
us
Implement the interfaces, get registered, get called when the .me is right. Framework determines the ow of control.
h6p://en.wikipedia.org/wiki/Hollywood_Principle
41
Frameworks
Very
valuable,
but
hard
to
design
A
change
in
the
framework
interface
aects
all
the
derived
applica.ons
Steep
learning
curve
Documen.ng
using
design
paherns
may
make
the
learning
process
easier
42
21
2/10/11
43
Law
of
Demeter
Governs
the
communica.on
structure
within
an
object-oriented
design
restricts
message-sending
statements
in
method
implementa.ons
only
talk
to
your
immediate
friends
the
method's
object
itself
(C++:
this)
an
object
that
is
an
argument
in
the
method's
signature
an
object
referred
to
by
the
object's
ahribute
an
object
created
by
the
method
an
object
referred
to
by
a
global
variable
h6p://c2.com/cgi/wiki?LawOfDemeter
44
22
2/10/11
[POSA]
45
Informa.on
hiding
Conceal
the
details
of
a
components
implementa.on
from
its
clients.
Modulariza.on
Meaningful
decomposi.on
of
a
system
into
subsystems
Physical
packaging
of
logical
structure
[POSA]
46
23
2/10/11
Coupling
Strength
of
associa.on
established
by
a
connec.on
from
one
module
to
another
Cohesion
Degree
of
connec.vity
between
the
func.ons
and
elements
of
a
single
module.
[POSA]
47
[POSA]
48
24