Design Patterns: Elements of Reusable Object-Oriented Software
155
4.
Structural Patterns
Structural patterns are concerned with how classes and objects arecomposed to
form larger structures.Structural
class
patterns use inheritance to compose
interfacesor implementations. As a simple example, consider how
multipleinheritance mixes two or more classes into one. The result is a classthat
combines the properties of its parent classes. This pattern isparticularly useful
for making independently developed class librarieswork together. Another example
is the class form of the Adapter (157) pattern. In general, an adapter makes
oneinterface (the adaptee's) conform to another, thereby
providing auniform
abstraction of different interfaces. A class adapteraccomplishes this by
inheriting privately from an adaptee class. Theadapter then expresses its
interface in terms of the adaptee's.
Rather than composing interfaces or implementations, structural
object
patterns
describe ways to compose objects to realize newfunctionality. The added
flexibility of object composition comes fromthe ability to change the composition
at run-time, which is impossiblewith static class composition.
Composite (183) is an example of a structural objectpattern. It describes how
to build a class hierarchy made up ofclasses for two kinds of objects: primitive
and composite. Thecomposite objects let you compose primitive and other
compositeobjects into arbitrarily complex structures. In the Proxy (233) pattern,
a proxy acts as a convenientsurrogate or placeholder for another object. A proxy
can be used inmany ways. It can act as a local representative for an object in
aremote address space. It can represent a large object that should beloaded on
demand. It might protect access to a sensitive object.Proxies provide
a level
of indirection to specific properties ofobjects. Hence they can restrict, enhance,
or alter these properties.
The Flyweight (218) pattern defines a structure forsharing objects. Objects are
shared for at least two reasons:efficiency and consistency.
Flyweight focuses
on sharing for spaceefficiency. Applications that use lots of objects must pay
carefulattention to the cost of each object. Substantial savings can be hadby
sharing objects instead of replicating them. But objects can beshared only if
they don't define context-dependent state. Flyweightobjects have no such state.
Any additional information they need toperform their task is passed to them when
needed. With nocontext-dependent state, Flyweight objects may be shared freely.
Whereas Flyweight shows how to make lots of little objects, Facade (208) shows
how to make a single object representan entire subsystem. A facade is a
representative for a set ofobjects. The facade carries out its responsibilities
by forwardingmessages to the objects it represents. The Bridge (171) pattern