Structural Design Patterns



Use sharing to support large numbers of fine-grained objects efficiently. (Gamma, 151)


Flyweight allows multiple objects to share data. This is typically done to make bettor use of available memory. It can also improve processor efficiency. An example of such speed increases would be the case of 3d textures pre-loaded into a graphics card. Without preloading this shared data, the GPU bus would be overloaded.


Separate the object state information into Intrinsic and Extrinsic data.Think about what state information is the same across a group of objects. This only works if you have a large amount of state information that is shared among objects. Put the unchanging information into the flyweight and reference that information from each of the individual objects that use it.



Autobot Robotics has begun experimenting in artificial intelligence. It has quickly became apparent that even the simplest algorithms would being a modern compter to its knees from memory requirements alone. In order to reduce this problem, the designers chose to share as much state information as was possible between objects. The system keeps a history creates an object for each event the machine encounters. Rather than storing the entire envorinment for each object that was discovered, they chose store that state one time and have all of the objects that use that environment to share that data.


The flyweight breaks encapsulation by splitting the object into separate components. This can make the code more brittle. It can also make concurreny a greater challenge. The concept of equality can be confusing in a flyweight since each of the objects is using exactly the same data.