Tuesday, 19 April 2016

Flyweight pattern - Structural


  • reduces the number of objects created and to decrease memory usage and increase performance
  • reuses already existing objects by storing them and creates new object when no matching object is found 
  • Make instances of classes on the fly to improve performance efficiently, like individual characters or icons on the screen.


Where to use ?
  • To reduce the number of objects created, decrease memory footprint and increase performance
  • Need to instantiate a large amount of small and fine-grained classes
  • To increase runtime cost associated with transferring, finding, or computing extrinsic data 

Implementation example
  • Interface Shape implemented by concrete classes : Circle, Rectangle, Square
  • A Factory class maintaining a HashMap of keys and different type of Shape objects
    • A factory method checking for object presence in map and return it, if exists
    • otherwise, create new object and put in the map

No comments:

Post a Comment

Note: only a member of this blog may post a comment.