The Flyweight Design Pattern is of class Structural and having subclass of Object Structural. It helps to support large number of fined grained objects.
Flyweight Design Pattern is used for sharing to support large number of fine grained objects, that can be used to handle creation and deletion of objects on the specified scenarios.
Let's say we have a Document Editor, and in that document editor most common things such as Rows, Tables and Characters are objects. So, this will cause creation of lots of objects that ca cause loading and time overhead while opening a Document in the Document Editor. Flyweight Pattern describes how to share objects to allow their use of fine gains with any Prohibition costs. Flyweight is an object which is shared and that can be used in multiple contexts simultaneously.
Use the Flyweight Design Pattern when following all conditions met:
- Application use large number of objects.
- Due to large number of objects storage cost is high.
- Most objects start can be make extrinsic.
- Few shared objects can replace groups of objects after removing extrinsic state.
- Application does not depend on the object identity.
Flyweight Design Pattern Reduces the Storage Use by:
- Reduction in Total Number of Instances.
- Amount of Intrinsic State Per Object.
- Whether state is intrinsic of extrinsic.