Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Your example, though, actually makes fly-weights take up way more than twice as much space (I think).

You're right.

So, that's how I interpretted the situation you were laying out. I assume I just misintrpretted it... but how exactly did I?

The GoF example was dealing with a WYSIWYG word processor, so "character" was more complicated than a single byte.

For example: A naive implementation of a bitmapped character in a word processor might (in Perl) look something like this:

my $char = bless { char => 'e', face => 'Times', size => '12pt', bitmap => '... some binary data ...', row => 12, column => 22, }, 'BitmapCharacter'; ... $char->render # draw a 12pt Times letter 'e' at row 12, column 22

So if we have a a bit of text that says "errol eats enough eggs everyday" then we have 6 x 'e' BitmapCharacter objects that only differ by their row and column attributes.

In the GoF language we say that the row and column attributes are extrinsic state - they're dependent on the characters context (as opposed to intrinsic state which is context independent).

The GoF Flyweight pattern is to rip out the extrinsic state from the object so we'd have something like:

my $char = bless { char => 'e', face => 'Times', size => '12pt', bitmap => '... some binary data ...', }, 'BitmapCharacter'; ... $char->render( 12, 22 ) # draw a 12pt Times letter 'e' at row 12, colu +mn 22

So the container now has to pass the extrinsic state and we can use the same object for each occurance of each letter - saving lots of space.

In reply to Re^5: Doubt about fly-weight objects. by adrianh
in thread Doubt about fly-weight objects. by blazar

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2019-06-19 00:39 GMT
    Find Nodes?
      Voting Booth?
      Is there a future for codeless software?

      Results (83 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!