Application Development, Product to Market
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use

Flyweight Design Pattern Principle

6/15/2016

0 Comments

 
Flyweight pattern is a structural pattern in GoF as it supports large numbers of fine-grained objects efficiently. Since each fine-grained object (e.g. a character in an editor application) may incur overhead that adds up. This pattern shares objects in a pool (Flyweight Factory) to minimize memory use.
Picture
source: GoF

overview

Flyweight: A flyweight is a shared independent object with its own intrinsic state (e.g. character code) that is stateless or immutable. If the intrinsic state is not going to be modified, then the object can be shared. Flyweight objects are shared among different contexts that hold extrinsic states (e.g. coordinate position in the document and its typographic style). These extrinsic state (out side of object) of the context will be passed by client object to the flyweight to operate. A flyweight makes as much of its state information extrinsic as possible and that is where the memory saving comes from.

FlyweightFactory: Once you have flyweight, then you can have a FlyweightFactory as a Flyweight Pool which is a collection of Flyweights that are cached to be shared.  FlyweightFactory is usually a Singleton to keep track of whether a particular flyweight has been generated and returns either a new instance or reference to one that it has already generated.

Real world Flyweight use cases: 
  1. Apply Flylweight to cells on a big board. For example, each cell on a 1000 by 1000 game of life game board can be a flyweight object to reduce memory footprint for each cell since extrinsic state are kelp outside of the Flyweight object.
  2. The Java language specification requires that integers between -128 and 127 be 'cached' is actually a form of flyweight to conserve system resource.

flyweight code example

FlyweightFactory class:

    
Flyweight class:

    
source: ​Java™ Design Patterns: A Tutorial

when to use

  1. Need use large numbers of objects when a simple repeated shared representation would save a large amount of memory.​​​
  2. Not often used at the application level in Java, more of a system resource management technique that is used at a lower level than Java.

comparison

Flyweight vs. Composite
  • Flyweight tends to go with composite that contains Flyweight objects.

Flyweight vs. Strategy
  • Flyweights are small strategy classes handled by the flyweight factory.
  • Both Strategy and Flyweight externalize extrinsic state.

references

  • The Game of Life
0 Comments



Leave a Reply.

    Categories

    All
    Algorithm
    Concurrency
    CQ
    Data Structure
    Design Pattern
    Developer Tool
    Dynamic Programming
    Entrepreneur
    Functional Programming
    IDE
    Java
    JMX
    Marketing
    Marklogic
    Memory
    OSGI
    Performance
    Product
    Product Management
    Security
    Services
    Sling
    Social Media Programming
    Software Development

    Feed Widget

    Archives

    May 2020
    March 2020
    April 2018
    March 2018
    February 2018
    December 2017
    March 2017
    November 2016
    June 2016
    May 2016
    April 2016
    October 2015
    September 2015
    August 2015
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013

    RSS Feed

in loving memory of my mother  and my 4th aunt
Photos used under Creative Commons from net_efekt, schani, visnup, Dan Zen, gadl, bobbigmac, Susana López-Urrutia, jwalsh, Philippe Put, michael pollak, oskay, Creative Tools, Violentz, Kyknoord, mobilyazilar