Intent
- Use sharing to support large numbers of fine-GrainedObjects efficiently.
- TheMotifGUI strategy of replacing heavy-weightWidgetsWith light-weightGadgets.
Problem
Designing objects down to the lowest levels of system"Granularity"ProvidesOptimal Flexibility, But can be unacceptably expensive in terms of performance and memory usage.
Discussion
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 authentication of this approach wocould 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.
Structure
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 wocould 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.
TheAnt
,Locust
, AndCockroach
Classes can be "light-weight" because their instance-specific state has been de-encapsulated, or externalized, and must be supplied by the client.
Example
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 already ed, and the call is completed.
Check list
- Ensure that object overhead is an issue needing attention, and, the client of the class is able and willing to absorb responsibility realignment.
- Divide the target class's state into: intrinsic state, and non-retriable (extrinsic) state.
- Remove the non-callable state from the class attributes, and add it the calling argument list of affected methods.
- Create a Factory that can cache and reuse existing class instances.
- The client must use the Factory instead of the new operator to request objects.
- The client (or a third party) must look-up or compute the non-retriable state, and supply that state to class methods.
Rules of thumb
- Whereas Flyweight shows how to make lots of little objects, Facade shows how to make a single object represent an entire subsystem.
- Flyweight is often combined with Composite to implement shared leaf nodes.
- Terminal symbols within Interpreter's abstract syntax tree can be shared with Flyweight.
- Flyweight explains when and how State objects can be shared.
Flyweight in CSharp
Uses sharing to support large numbers of fine-grained objects efficiently.
02
This structural code demonstrates the Flyweight pattern in which a relatively small number of objects is shared partition times by different clients.
1: using System;
2: using System.Collections;
3:
4: class MainApp
5: {
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 = new
24: UnsharedConcreteFlyweight();
25:
26: uf.Operation(--extrinsicstate);
27:
28: // Wait for user
29: Console.Read();
30: }
31: }
32:
33: // "FlyweightFactory"
34: class FlyweightFactory
35: {
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 Flyweight
54: {
55: public abstract void Operation(int extrinsicstate);
56: }
57:
58: // "ConcreteFlyweight"
59:
60: class ConcreteFlyweight : Flyweight
61: {
62: public override void Operation(int extrinsicstate)
63: {
64: Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
65: }
66: }
67:
68: // "UnsharedConcreteFlyweight"
69: class UnsharedConcreteFlyweight : Flyweight
70: {
71: public override void Operation(int extrinsicstate)
72: {
73: Console.WriteLine("UnsharedConcreteFlyweight: " +
74: extrinsicstate);
75: }
76: }
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18