
.NET 1.1+Flyweight Design Pattern
The flyweight pattern is a design pattern that is used to minimise resource usage when working with very large numbers of objects. When creating many thousands of identical objects, stateless flyweights can lower the memory used to a manageable level.
What is the Flyweight Pattern?
The flyweight pattern is a Gang of Four design pattern. This is a structural pattern as it defines a manner for creating relationships between classes. The flyweight design pattern is used to reduce the memory and resource usage for complex models containing many hundreds, thousands or even hundreds of thousands of similar objects.
When you need to create a very large number of objects, each requires an amount of memory to store the objects' state. Even if the storage requirements for each individual object are small, the number of objects may cause the overall memory usage to be high. Depending upon the scenario and the target environment, the memory usage may be so high that the program cannot execute.
In some cases, the objects being created may include information that is often duplicated. Where this is true, the flyweight pattern can be used. When this pattern is applied, the properties of the objects that are shared and are reasonably unchanging are moved into flyweight objects. For each of the main objects that use the shared data, only a reference to the appropriate flyweight object is required. This can drastically reduce the memory used by each of the main objects.
The flyweight pattern uses the concepts of intrinsic and extrinsic data. The intrinsic data is held in the properties of the flyweight objects that are shared. This information is stateless and generally remains unchanged, as any changes would be effectively replicated amongst all of the objects that reference the flyweight. Extrinsic data can be stateful as it is held outside of a flyweight object. It can be passed to methods of a flyweight when needed but should never be stored within a shared flyweight object.
The flyweight design pattern often uses a variation on the factory method pattern for the generation of the shared objects. The factory receives a request for a flyweight instance. If a matching object is already in use, that particular object is returned. If not, a new flyweight is generated. Usually the full set of available flyweight objects is held within the factory in a collection that can be accessed quickly, such as a Hashtable.
An appropriate use of the flyweight pattern is for a simulation that contains many similar items. For example, a war strategy simulation game may include the ability to monitor every individual unit on the battlefield. For a large battle, this could easily involve tens of thousands of infantry and vehicles. Holding the details of each object individually would use prohibitively large amounts of memory. However, as most units would share a set of stateless information, this could be extracted from the main objects to be held in flyweight objects.
19 November 2008