Flyweight


Usage

  • A fine-grained instance used for efficient sharing
  • Use sharing to support large numbers of fine-grained objects efficiently

Diagram

Flyweight

Participants

Flyweight
  • Declares an interface through which flyweights can receive and act on extrinsic state.
ConcreteFlyweight
  • Implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic, that is, it must be independent of the ConcreteFlyweight object's context.
UnsharedConcreteFlyweight
  • Not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing, but it doesn't enforce it. It is common for UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level in the flyweight object structure (as the Row and Column classes have).
FlyweightFactory
  • Creates and manages flyweight objects
  • Ensures that flyweight are shared properly. When a client requests a flyweight, the FlyweightFactory objects supplies an existing instance or creates one, if none exists.
Client
  • Maintains a reference to flyweight(s).
  • Computes or stores the extrinsic state of flyweight(s).
Resources URL: 
notes/design_patterns/resources
Sources URL: 
notes/design_patterns/sources

See Also