Академический Документы
Профессиональный Документы
Культура Документы
An
Example
A
simple
problem
Drawing
shapes
Rectangles
Example
(Contd)
Proper
use
of
inheritance
We
dont
want
the
code
that
draws
the
rectangles
to
worry
about
what
type
of
drawing
program
it
should
use.
V1Rectangle
uses
DP1
V2Rectangle
uses
DP2
class V1Rectangle : public Rectangle! {! public:! V1Rectangle(double x1, double y1, double x2, double y2) : ! Rectangle(x1, y1, x2, y2){} void drawLine( double x1, double y1, double x2, double y2){! DP1::draw_a_line( x1, y1, x2, y2);! }! public:! ~V1Rectangle(void);! };!
Sequence Diagram
Any
Problem?
Combinatorial
explosion
Another
new
drawing
program
Six
dierent
kinds
of
Shapes
2
Shape
concepts
7mes
three
drawing
programs
An
Observa7on
The
way
to
look
at
design
pa1erns
Focus
on
the
context
of
the
pa1ern
the
problem
it
is
trying
to
solve
Not
on
the
solu7ons
the
pa1erns
oer
10
11
12
Separate the abstraction class hierarchy from the implementation class hierarchy and use delegation as a bridge between the two.
13
Class
Diagram
Generic
structure
of
the
Bridge
pa1ern
14
The Par7cipants
Abstraction defines the abstractions interface RefinedAbstraction extends the interface defined by Abstraction Implementor defines the interface for implementation classes; can be different from Abstraction interface. Typically, Implementor provides only primitive operations, and Abstraction defines higher-level operations based on these primitives. ConcreteImplementor implements the Implementor interface and defines its concrete implementation.
15
16
Applicability
Use
the
bridge
when:
You
want
to
avoid
a
permanent
binding
between
an
abstrac7on
and
its
implementa7on.
Both
the
abstrac7ons
and
their
implementa7ons
should
be
extensible
by
subclassing.
Changes
in
the
implementa7on
of
an
abstrac7on
should
have
no
impact
on
clients
(i.e.
code
should
not
have
to
be
recompiled).
You
want
to
share
an
implementa7on
among
mul7ple
objects,
and
want
to
hide
this
from
the
client.
17
18
19
20
21
Flyweight
22
23
Flyweight
pa1ern
yweight:
an
assurance
that
no
more
than
one
instance
of
a
class
will
have
iden7cal
state
achieved
by
caching
iden7cal
instances
of
objects
to
reduce
object
construc7on
similar
to
singleton,
but
has
many
instances,
one
for
each
unique-state
object
useful
for
cases
when
there
are
many
instances
of
a
type
but
many
are
the
same
can
be
used
in
conjunc7on
with
Factory
pa1ern
to
create
a
very
ecient
object-builder
24
String distinctString = fly + weight; System.out.println(distinctString == "flyweight"); // false String flyweight = (fly + weight).intern(); System.out.println(flyweight == "flyweight"); // true
25
Implemen7ng
a
Flyweight
yweigh7ng
works
best
on
immutable
objects
immutable:
cannot
be
changed
once
constructed
class
pseudo-code
sketch:
public
class
Flyweighted
{
sta7c
map
or
table
of
instances
private
constructor
sta7c
method
to
get
an
instance
if
we
have
created
this
type
of
instance
before,
get
it
from
map
and
return
it
otherwise,
make
the
new
instance,
store
and
return
it
}
26
27 27
29
Scope: domain over which a pattern applies Purpose: reflects what a pattern does
31
32