Pattern Comparison
I grouped the Composite and Flyweight patterns in this chapter because they both deal with structures of multiple objects. The Composite pattern is concerned with reacting to commands for accessing and altering a data structure in a uniform way, whereas the Flyweight pattern is a clever way of saving space when there are multiple identical objects. They both have the property that the types making up the pattern can be packaged into a namespace and then used by a client.
In our examples, the CompositePattern
namespace was programmed
generically, giving any client the built-in flexibility of instantiating
the pattern to suit its needs. The constraints that I mentioned at the
time were that a type used for instantiating Component
needed to have Equals
and ToString
methods defined. The Equals
method is essential for the correct
working of the Find
method, but the
ToString
requirement can be dispensed
with if different output is arranged. The FlyweightPattern
namespace, on the other hand,
contains the IFlyweight
and Flyweight
types, which are very specific to
the photo application used for illustration. (It also contains the
FlyweightFactory
class, which is not
dependent on any class from the client.)
Flyweight is a server pattern in the sense that it can be useful to many other patterns to keep data compact. Examples we will encounter later are the Interpreter, State, and Strategy patterns. The Composite pattern is also useful in combination with other patterns that ...
Get C# 3.0 Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.