Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Regarding Flyweight pattern

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have yet not understood the logic of the FlyWeight pattern implementation.
Can you please explain me in brief with suitable example?
Please do share the useful links as well.

Thanks and regards,
Paritosh Chandorkar
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Flyweight pattern is basically all about resource management, more specifically about supporting large numbers of objects efficiently by encapsulating state in an immutable object (the flyweight) and sharing that object among as many similar objects as possible. One example of a flyweight implementation is actually a well-known mechanism provided by the core Java API: java.lang.String.intern(). XML parsers, for instance, can use the String.intern() mechanism to efficiently share XML tag names (among other things) when processing XML data. Building a complete DOM tree in-memory is already notoriously expensive when it comes to memory consumption. Imagine how much more expensive it would be if the XML parser would have to allocate heap space for a new String object for each and every tag it encouters, regardless of whether or not it had already processed tags with an identical name. A String is immutable by design and the String.intern() method offers a convenient way to add a new value to the constant pool at runtime, so an XML parser can make use of this to keep the number of String objects related to tag names in check. This is the essence of the Flyweight pattern, which in this example is facilited by an object pool. To reiterate, a flyweight object is an immutable object that encapsulates state that can be shared among many other objects - immutability is key because state must be shared safely.
 
Paritosh H Chandorkar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Jelle,for the reply.I got a hang of it now.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic