Designing objects down to the lowest levels of system “granularity” provides optimal flexibility, but can be unacceptably expensive in terms of performance and memory usage.
The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost. Each “flyweight” object is divided into two pieces: the state-dependent (extrinsic) part, and the state-independent (intrinsic) part. Intrinsic state is stored (shared) in the Flyweight object. Extrinsic state is stored or computed by client objects, and passed to the Flyweight when its operations are invoked.
An illustration of this approach would be Motif widgets that have been re-engineered as light-weight gadgets. Whereas widgets are “intelligent” enough to stand on their own; gadgets exist in a dependent relationship with their parent layout manager widget. Each layout manager provides context-dependent event handling, real estate management, and resource services to its flyweight gadgets, and each gadget is only responsible for context-independent state and behavior.
Flyweights are stored in a Factory’s repository. The client restrains herself from creating Flyweights directly, and requests them from the Factory. Each Flyweight cannot stand on its own. Any attributes that would make sharing impossible must be supplied by the client whenever a request is made of the Flyweight. If the context lends itself to “economy of scale” (i.e. the client can easily compute or look-up the necessary attributes), then the Flyweight pattern offers appropriate leverage.
The Ant
, Locust
, and Cockroach
classes can be “light-weight” because their instance-specific state has been de-encapsulated, or externalized, and must be supplied by the client.
The Flyweight uses sharing to support large numbers of objects efficiently. The public switched telephone network is an example of a Flyweight. There are several resources such as dial tone generators, ringing generators, and digit receivers that must be shared between all subscribers. A subscriber is unaware of how many resources are in the pool when he or she lifts the handset to make a call. All that matters to subscribers is that a dial tone is provided, digits are received, and the call is completed.
Uses sharing to support large numbers of fine-grained objects efficiently.
This structural code demonstrates the Flyweight pattern in which a relatively small number of objects is shared many times by different clients.
1: using System;2: using System.Collections;3:4: class MainApp5: {6: static void Main()7: {8: // Arbitrary extrinsic state
9: int extrinsicstate = 22;10:11: FlyweightFactory f = new FlyweightFactory();12:13: // Work with different flyweight instances
14: Flyweight fx = f.GetFlyweight("X");
15: fx.Operation(--extrinsicstate);16:17: Flyweight fy = f.GetFlyweight("Y");
18: fy.Operation(--extrinsicstate);19:20: Flyweight fz = f.GetFlyweight("Z");
21: fz.Operation(--extrinsicstate);22:23: UnsharedConcreteFlyweight uf = new24: UnsharedConcreteFlyweight();25:26: uf.Operation(--extrinsicstate);27:28: // Wait for user
29: Console.Read();30: }31: }32:33: // "FlyweightFactory"
34: class FlyweightFactory35: {36: private Hashtable flyweights = new Hashtable();37:38: // Constructor
39: public FlyweightFactory()40: {41: flyweights.Add("X", new ConcreteFlyweight());
42: flyweights.Add("Y", new ConcreteFlyweight());
43: flyweights.Add("Z", new ConcreteFlyweight());
44: }45:46: public Flyweight GetFlyweight(string key)
47: {48: return((Flyweight)flyweights[key]);49: }50: }51:52: // "Flyweight"
53: abstract class Flyweight54: {55: public abstract void Operation(int extrinsicstate);56: }57:58: // "ConcreteFlyweight"
59:60: class ConcreteFlyweight : Flyweight61: {62: public override void Operation(int extrinsicstate)63: {64: Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
65: }66: }67:68: // "UnsharedConcreteFlyweight"
69: class UnsharedConcreteFlyweight : Flyweight70: {71: public override void Operation(int extrinsicstate)72: {73: Console.WriteLine("UnsharedConcreteFlyweight: " +
74: extrinsicstate);75: }76: }
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18