[1] These patterns promote flexible object-oriented software design, which is easier to implement, change, test, and reuse.
The term was first coined, and the idea extensively explored, by Paul Calder and Mark Linton in 1990[2] to efficiently handle glyph information in a WYSIWYG document editor.
The factory interface for creating or reusing flyweight objects is often a facade for a complex underlying system.
For example, the factory interface is commonly implemented as a singleton to provide global access for creating flyweights.
This object caching system can be encapsulated with the chain of responsibility pattern, which promotes loose coupling between components.
If the list of values is finite and known in advance, the flyweights can be instantiated ahead of time and retrieved from a container on multiple threads with no contention.
If flyweights are instantiated on multiple threads, there are two options: To enable safe sharing between clients and threads, flyweight objects can be made into immutable value objects, where two instances are considered equal if their values are equal.
The C++ Standard Template Library provides several containers that allow unique objects to be mapped to a key.
The use of containers helps further reduce memory usage by removing the need for temporary objects to be created.