Flyweight comes under Structural design pattern.
It minimizes the memory usage be sharing common resources with other similar objects.
Behaviour & Advantages
Supports large number of objects by efficiently sharing common features.
Minimizes the Memory footprint of a program.
Participants
Flyweight
Defines an abstract interface through which flyweight can receive and act on extrinsic state.
Concrete Flyweight
An implementation of Flyweight interface which acts as common sharable resource or feature.
It must maintains an intrinsic state which is independent of the ConcreteFlyweight object's context.
Flyweight Factory
It creates and manages flyweight objects. When a client requests a flyweight,
it returns one from the pool if it is available otherwise creates a new flyweight
instance, updates the pool with new instance and returns the flyweight.
Client
One who uses Flyweight through Flyweight Factory.
This example shows Label character border Flyweight Factory which returns character border
Flyweight instances as needed. Internally it maintains a map of border character and border instance.
Even though we have rendered six borders around different labels it has just created two
border instances.