Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chap5: Strings and Flyweight

 
Siegfried Heintze
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 5 of Allen's book gives an example of the Flyweight pattern.
It then says that java.lang.String is an example of this pattern. I don't understand this statement. It seems one must use a (hash) table to implement flyweight so as not to stored data redundantly with each element. How does java.lang.String do this? Is it really implemented with a hash table?
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day,

I'm in the same boat as Siegfried. I don't understand how java.lang.String implements the Flyweight pattern.

Any thoughts?

Thanks,
Dan
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Too many objects" is the problem that FlyWeight attempts to address.

In java.lang.String example, note that the individual characters that make up the string are not modeled as objects because doing so would mean creating a large number of objects affecting performance, footprint and more than anything, manageability. String object instead represents an aggregate of character instances, acts as a single object instance while still allowing sharing of those characters at the finegrained level.

Note that "sharing" is also an important criteria here, not just manageability.
Flyweights are used in scenarios where object identity is determined at a conceptual level ie., two strings are equal if their character sequence is equal and this makes sharing possible. This refers to the concept of "FlyWeight pools" that enable reuse of constituent entities. Every String has an extrinsic state which is its presentation as a sequence of characters, as a single object whereas its intrinsic state may be more distinct ie., the individual bytes that make up the string.

Here are a few other examples of the FlyWeight patter
  • Infinite number of points on an arbitrarily shaped curve.
  • Seats in an aircraft( hint..hint.. )
  • An index table of entries( in a book, for example )



  • I strongly recommend reading the GOF Design Patterns book if you can get hold of a copy. I found the explanation of FlyWeight pattern very easy to understand, like every other pattern in the book.

    HTH
     
    Dan Drillich
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Ajith for the clear explanation!
     
    Dan Drillich
    Ranch Hand
    Posts: 1183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good Day Ajith,

    About Seats and the FlyWeight pattern; I see two attributes of a Seat: Location, or a plain seat number and Occupied, whether the Seat was taken or not. I can't relate these attributes to the extrinsic and intrinsic states of the Seats.

    Any ideas?

    Thanks,
    Dan
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic