Académique Documents
Professionnel Documents
Culture Documents
Mo#va#on
Consider
a
context-sensi#ve
help
system
for
a
GUIDE
The
object
that
ul#mately
provides
the
help
isn't
known
explicitly
to
the
object
(e.g.,
a
buHon)
that
ini#ates
the
help
request.
So
use
a
chain
of
objects
to
decouple
the
senders
from
the
receivers.
The
request
gets
passed
along
the
chain
un#l
one
of
the
objects
handles
it.
Each
object
on
the
chain
shares
a
common
interface
for
handling
requests
and
for
accessing
its
successor
on
the
chain
2
Example 2
Applicability
Use
Chain
of
responsibility
paHern
when
more
than
one
object
may
handle
a
request,
and
the
handler
isnt
known
a
priori.
you
want
to
issue
a
request
to
one
of
several
objects
without
specifying
the
receiver
explicitly
the
set
of
objects
that
can
handle
a
request
should
be
specied
dynamically
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request 6
Client
handles
requests
it
is
responsible
for
can
access
its
successor
if
the
ConcreteHandler
can
handle
the
request,
is
does
so;
otherwise
it
forwards
the
request
to
its
successor
ini#ates
the
request
to
a
ConcreteHandler
object
on
the
chain
7
public: HelpHandler(HelpHandler* s) : _successor(s) {} virtual void HandleHelp(); private: HelpHandler* _successor; }; void HelpHandler::HandleHelp() { if (_successor) _successor->HandleHelp(); }
An
alterna#ve:
use
a
single
handler
func#on
which
takes
a
request
object
as
parameter
void Handler::HandleRequest(Request* theRequest) { switch (theRequest->getKind()) { case Help: HandleHelp((HelpRequest*)theRequest); break; }
10
Structural
PaHerns
Concerned
with
how
classes
and
objects
are
combined
to
create
larger
structures.
Concerned
with
composing
objects
to
establish
new
func#onality.
12
Composite
PaHern
A composite is a group of objects in which some objects contain others; one object may represent groups, and another may represent an individual item, a leaf. Facilitates the composition of objects into a stree structure, a hierarchy, that represent part-whole hierarchies. These hierarchies consist of both primitive and composite objects. The operations are appropriate for processing and traversing trees.
13
Example
Example:
complex
graphic
applica#on,
with
hierarchically
nested
objects.
Main
idea:
Containers
and
leaf
objects
appear
the
same
to
clients.
Up
and
down
delega6on:
A
container
may
delegate
to
all
contained
objects.
A
leaf/container
may
delegate
to
containing
objects.
aComposite
aLeaf
aLeaf
aComposite
aLeaf
aLeaf
aLeaf
14
Graphic Draw() Add(Graphic) Remove(Graphic) GetChild(int) graphics Line Draw( ) Rectangle Draw( ) Text Draw( ) Picture Draw( ) Add(Graphic g) Remove(Graphic) GetChild(int) forall g in graphics g.Draw()
15
Par#cipants
Component
(Graphic)
declares
interface
for
objects
in
the
composi#on
implements
default
behavior
declares
an
interface
for
accessing
and
managing
its
child
components
(op#onal)
declares
an
interface
for
accessing
a
components
parent
Composite
(Picture)
denes
behavior
for
components
having
children
stores
child
components
implements
child-related
opera#ons
in
the
Component
interface
Client
manipulates
objects
in
the
composi#on
through
the
Component
interface
16
Sharing
components
mul#ple
parents
ambigui#es
as
a
request
propagates
up
the
structure
17
Component operation()
Leaf operation()
Each node of the Component structure can respond to some common operation(s). I.e. the client can send the common operation to Component and the structure responds appropriately.
18
Example
MenuComponent
Waitress
add() remove() getChild() print()
MenuItem
print()
Menu
add() remove() getChild() print()
19
Composite
PaHern
Menu menuComponents: ArrayList
add() remove() getChild() print()
Note the association from Menu to MenuComponents with an array list data type. How to implementation of print() in Menu and in MenuItem
20
Composite
PaHern
In the Menu class we have a print() that is appropriate for an internal node.
public void print() { System.out.print("\n" + getName()); System.out.println(", " + getDescription()); System.out.println("---------------------"); Iterator iterator = menuComponents.iterator(); while (iterator.hasNext()) { MenuComponent menuComponent = (MenuComponent)iterator.next(); menuComponent.print();
21
Composite PaHern
22
Summary
The
Composite
PaHern
is
used
to
represent
part- whole
object
hierarchies.
Clients
interact
with
objects
through
the
component
class.
It
enables
clients
to
to
ignore
the
specics
of
which
leaf
or
composite
class
they
use.
Can
be
used
recursively,
so
that
Display
can
show
both
ares
and
stars.
New
components
can
easily
be
added
to
a
design.
23