Flyweight Design Pattern

Flyweight Pattern

Sometimes in system, a number of instances are required to represent the data even when all the instances are similar in structure and basic composition but with a few different parameters. Here using flyweight pattern, one instance can be used to represent the data by identifying the variable data and make the object configurable for this before every method call.

Example

  • In JTable, a column renderer serve the purpose to paint every cell on the table only table configures this renderer for its position, data and other variable attributes before using it for painting the next cell.
  • While painting a word on the screen, the same character instance is being used to paint complete word by just changing its location and data value.


When to Use

  • When large numbers of objects are required to represent the data and are similar in their composition and structure.
  • When basic structures of different instances are same and the variable parameters/data can be configured before operation.


Usage

  • Helps us to avoid the large number of object instances in memory


Drawbacks

  • Coding is not simple while implementing flyweight pattern for a complex system. Because some of the stale states can be carried forward by the shared instance and generally debugging becomes a headache for developer in these kinds of scenarios. 

People who read this post also read :



0 comments:

Post a Comment