Java Reference
In-Depth Information
that tradeoffs are possible. Therefore, a given design pattern might have variations
on its application to match the various tradeoffs inherent in a given situation.
The rest of this section introduces a few simple design patterns that are used
later in the topic.
1.3.1
Flyweight
The Flyweight design pattern is meant to solve the following problem. You have an
application with many objects. Some of these objects are identical in the informa-
tion that they contain, and the role that they play. But they must be reached from
various places, and conceptually they really are distinct objects. Because there is
so much duplication of the same information, we would like to take advantage of
the opportunity to reduce memory cost by sharing that space. An example comes
from representing the layout for a document. The letter “C” might reasonably be
represented by an object that describes that character's strokes and bounding box.
However, we do not want to create a separate “C” object everywhere in the doc-
ument that a “C” appears. The solution is to allocate a single copy of the shared
representation for “C” objects. Then, every place in the document that needs a
“C” in a given font, size, and typeface will reference this single copy. The various
instances of references to a specific form of “C” are called flyweights.
We could describe the layout of text on a page by using a tree structure. The
root of the tree represents the entire page. The page has multiple child nodes, one
for each column. The column nodes have child nodes for each row. And the rows
have child nodes for each character. These representations for characters are the fly-
weights. The flyweight includes the reference to the shared shape information, and
might contain additional information specific to that instance. For example, each
instance for “C” will contain a reference to the shared information about strokes
and shapes, and it might also contain the exact location for that instance of the
character on the page.
Flyweights are used in the implementation for the PR quadtree data structure
for storing collections of point objects, described in Section 13.3. In a PR quadtree,
we again have a tree with leaf nodes. Many of these leaf nodes represent empty
areas, and so the only information that they store is the fact that they are empty.
These identical nodes can be implemented using a reference to a single instance of
the flyweight for better memory efficiency.
1.3.2
Visitor
Given a tree of objects to describe a page layout, we might wish to perform some
activity on every node in the tree. Section 5.2 discusses tree traversal, which is the
process of visiting every node in the tree in a defined order. A simple example for
our text composition application might be to count the number of nodes in the tree
Search WWH ::




Custom Search