Design Patterns: Elements of Reusable Object-Oriented Software
154
hierarchy
—
either because thecompiler provides it automatically (as in Smalltalk
or Objective C) orbecause it's needed in another part of the system.
Overall, the Prototype pattern is probably the best for the drawingeditor framework,
because it only requires implementing a Cloneoperation on each Graphics class.
That reduces the number of classes,and Clone can be used for purposes other than
pure instantiation (e.g.,a Duplicate menu operation).
Factory Method makes a design more customizable and only a little morecomplicated.
Other design patterns require new classes, whereasFactory Method only requires
a new operation. People often useFactory Method as the standard way to create
objects, but it isn'tnecessary when the class that's instantiated never changes
or wheninstantiation takes place in an operation that subclasses can
easilyoverride, such as an initialization operation.
Designs that use Abstract Factory, Prototype, or Builder are even moreflexible
than those that use Factory Method, but they're also morecomplex. Often, designs
start out using Factory Method and evolvetoward the other creational patterns
as the designer discovers wheremore flexibility is needed. Knowing many design
patterns gives youmore choices when trading off one design criterion against
another.
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
Do'stlaringiz bilan baham: |