Flyweights require a Factory Method or a Factory class to create them. The Factory class keeps track of the Flyweights that already exist.
When it is asked to create a new Flyweight, the Factory class first checks to see whether a matching Flyweight exists.
If so, it returns a pointer to the old Flyweight. This requires the Factory class to maintain a searchable list of existing Flyweights.
In some cases you may want to wait until a Flyweight is first requested before constructing it.
In other cases, you may find it simpler to preallocate all the possible Flyweights. The basic structure looks like this:
This diagram uses one concrete Flyweight class. In some situations, necessary flexibility is achieved by making the Flyweight class abstract,
then providing concrete implementations. One advantage to this approach is that you can use both shared and unshared Flyweights that respond to the same messages.
This can be useful in mixed situations in which you might have several thousand copies of one Flyweight as well as a few hundred unique Flyweights.