Flyweight Pattern, is a known Structural Pattern. This pattern is used by us daily in the programming world knowingly or unknowingly. When i read about it in a Blog in Javabeat.com, i did feel like documenting it in my blog too.
The idea behind Flyweight Pattern is sharable Objects. The ideology is dnt recreate what already is existing on the Memory, when something is not found on Memory then recreate it. In simple words save some memory for better things than duplicating.
Java String is a perfect example of implementation of Flyweight Pattern.
String a = "Hello"
String b = "Hello"
String c = new String("Hello");
String 'a' and 'b' are implementation of the Flyweight Pattern. Both the variables point to the same memory. Hello is not created 2 times in your Memory. But String 'c' is duplicating Hello in memory which should be avoided.
Hope this helps in understanding Flyweight Pattern.
Friday, May 13, 2011
Subscribe to:
Post Comments (Atom)
3 comments:
I think in "the real world" these are called caches.
With a good cache framework the API should be simple and is the same from the component developer perspective, however the cache configuration&implementation will determine specifics like persistence,lifetime/expiry,validation etc. Should also deal when objects grow beyond available/allocated memory.
any suggestions?
hmm..that makes me interested about JBoss Infinispan and co.
This seems like a good example.
--Gautam
Post a Comment