Composite
|
aka
recursive composition
Object Structural p. 163 |
|
Intent |
Compose
objects into tree structures.
Towards uniform treatment of objects and compositions |
|
Motivation |
Having to
distinguish between objects and container objects makes an application
complex |
|
Key Idea |
An
abstract class that represents both primitives and their containers |
|
Applicability |
1)
part
whole hierarchies 2)
uniform
treatment of compositions of objects and
individual objects |
|
Participants |
Component,
Leaf, Composite, Client |
|
Collaborations
|
Clients
use the Component interface to interact with objects in the structure If aLeaf, request handled directly If aComposite, forward request to child
components (possibly performing additional operations) |
|
Consequences
|
Defines
class hierarchies of primitives and composites, which are interchangeable in
all(?) method calls Client
can treat primitives and composites uniformly Easier to
add new kinds of components Prb:
resulting design can be overly
general |
|
Implementation |
1)
explicit
parent references 2)
sharing
components ??? 3)
maximize
the component interface (define many
common operations) – a class could end up
defining operations NOT meaningful to its subclasses 4)
where
should child-management operations be defined (declared in Component, impl in
Leaf; or decl and impl only in Composite?)
tradeoff between safety and transparency (transparency better here) 5)
Component
implement a list of Components? (child
pointer in the base class = space penalty) 6)
Child
ordering 7)
Cache
to improve performance (but need cache invalid interface) 8)
Who
should delete components? (relevant
only in languages w/o garbage collection, e.g.,…..) 9)
Which
data structure to store components? |
|
Known
Uses |
View
Class of ST MVC, UI toolkit/frameworks, RTL ST compiler, portfolio/assets,
Command pattern |
|
Related
Patterns |
Chain of
responsibility, decorator, flyweight, Iterator, interpreter, visitor |
|
redesign
cause |
|
|
Additional
refs |
http:// |
|
questions |
What is
the distinction between component and composite |
< diagram or code
snippet that will help us better understand the pattern>
<possibly structure diagram
annotated>