Flyweight
Intent
Use sharing to support large numbers of fine-grained objects
efficiently.
Participants
-
Flyweight -
declares an interface through which flyweights (shared and unshared)
can receive and act on extrinsic state.
-
ConcreteFlyweight -
implements the Flyweight interface with shared objects.
-
UnsharedConcreteFlyweight -
implements the Flyweight interface with unshared objects.
-
FlyweightFactory -
provides methods for creating instances of Flyweight subclasses.
The instances may be shared (singletons).
-
Client -
stores or computes extrinsic state.
Additional Terminology
-
intrinsic state
-
extrinsic state
Examples
Borders for Java Swing Components
-
Border
is a Flyweight.
-
simple raised and lowered
BevelBorders
may be ConcreteFlyweights since they have no extrinsic state beyond
that provided by a JComponent.
-
TitledBorder
is an UnsharedConcreteFlyweight - the title is extrinsic state that
cannot be found in a JComponent.
-
BorderFactory
is a FlyweightFactory.
Java Swing Component Delegates
-
ComponentUI
is a Flyweight.
Each look-and-feel provides a ConcreteFlyweight for each subclass of
JComponent.
All extrinsic state is stored in JComponent objects.
-
UIManager
is a FlyweightFactory