|
| 1 | +# Flyweight |
| 2 | + |
| 3 | +Use sharing to support large numbers of objects efficiently and to save RAM. |
| 4 | + |
| 5 | +## Problem |
| 6 | + |
| 7 | +* Large numbers of objects should be supported efficiently. |
| 8 | +* Creating large numbers of objects should be avoided. |
| 9 | + |
| 10 | +When representing large text documents, for example, creating an object for each character in the document would result in a huge amount of objects that couldn't be processed efficiently. |
| 11 | + |
| 12 | +## Solution |
| 13 | + |
| 14 | +Define `Flyweight` objects that |
| 15 | + |
| 16 | +* store intrinsic (invariant) state that can be shared |
| 17 | + * *intrinsic state* is context independent, for example the character `A`. |
| 18 | +* provide an interface through which extrinsic (variant) state can be passed in |
| 19 | + * extrinsic state depends on and varies with the flyweight's context and therefore can't be shared, for example the position of the character `A` in a GUI. |
| 20 | + |
| 21 | +This enables clients to reuse / share Flyweight objects (instead of creating a new object each time) and pass in extrinsic state when they invoke a Flyweight operation. |
| 22 | +This greatly reduces the number of physically created objects. |
| 23 | + |
| 24 | +## Common Structure |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +* Flyweight |
| 29 | + * declares an interface through which flyweights can receive and act on extrinsic state. |
| 30 | +* ConcreteFlyweight |
| 31 | + * implements the Flyweight interface and holds the intrinsic state, which must be independent of the object's context. |
| 32 | +* UnsharedConcreteFlyweight (not appropriate) |
| 33 | +* FlyweightFactory |
| 34 | + * creates and manages flyweight objects |
| 35 | + * ensures that flyweights are shared properly. When a client requests a flyweight, the FlyweightFactory object supplies an exisiting instance or creates one, if none exists. |
| 36 | +* Client |
| 37 | + * maintains a reference to flyweights. |
| 38 | + * computes or stores the extrinsic state of flyweights. |
| 39 | + |
| 40 | +## Collaboration |
| 41 | + |
| 42 | +* Clients should not instantiate ConcreteFlyweights directly. Clients must obtain ConcreteFlyweight objects exclusively from the FlyweightFactory object to ensure they are shared properly. |
| 43 | + |
| 44 | +## Benefits |
| 45 | + |
| 46 | +* Saves RAM, thus allowing a program to support much more objects. |
| 47 | + |
| 48 | +## Drawbacks |
| 49 | + |
| 50 | +* Wastes CPU time on searching or calculating the context |
| 51 | +* Increases overall code complexity by creating multiple additional classes. |
| 52 | + |
| 53 | +## Example |
| 54 | + |
| 55 | +**Definition** |
| 56 | + |
| 57 | +**Usage** |
| 58 | + |
| 59 | +## Comparison with other patterns |
| 60 | + |
| 61 | +* **Composite** - Flyweight is often combined with Composite to implement a shared leaf nodes and save RAM. |
| 62 | + * A consequence of sharing is that flyweight leaf nodes cannot store a pointer to their parent. Rather, the parent pointer is passed to the flyweight as part of its extrinsic state. This has a major impact on how the objects in the hierarchy communicate with eath other. |
| 63 | + |
| 64 | +* It's often best to implement `State` and `Strategy` objects as flyweights. |
| 65 | + |
| 66 | +* Flyweight looks almost like `Singleton`. The key differences are |
| 67 | + 1. Singleton object can be mutable. Flyweight objects are immutable. |
| 68 | + 2. There should be only one Singleton instance, whereas Flyweight class can have multiple instances with a different intrinsic state. |
0 commit comments