CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Flyweight pattern

    I see the importance of using a pool of distincted objects,
    it saves time and space (by not creating instances identical to existing objects).

    What I miss here, is the difference between this pattern and maintaining
    a std::map.

    Please correct me if I'm wrong, but whenever the client of the Flyweight
    class has to save the extrinsic state, then for each usage of a Flyweight
    (that is shared from the pool) there still stands an object that keeps
    the extrinsic state,
    which means the pattern seperates the intrinsic (into some structure as map) - but the quantity of objects is decreased only if the extrinsic state
    can be computed (but not saved).
    **** **** **** **** **/**

  2. #2
    Join Date
    Aug 2005
    Posts
    115

    Re: Flyweight pattern

    The difference is that std::map is not the Flyweight Pattern itself, but can be used in the implementation of one. Here's an illustration:

    http://www.codeproject.com/gen/desig...validators.asp

  3. #3
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Re: Flyweight pattern

    Quote Originally Posted by Darth Hacker
    The difference is that std::map is not the Flyweight Pattern itself, but can be used in the implementation of one.
    Sure, thats what I meant by "...maintaining a std::map...", which is a good
    'tool' to create a pool of distictions.
    The Flyweight suppose to reduce the number of objects whenever there
    are plenty of objects which most have common states.
    That common part is shared from a pool, instead of duplicated for each object.

    My question was not about the how to implement the pool (map or other manipulation). I wanted to know whether the main goal of this pattern is the reduction of objects since it seems to me that it saves memory
    but whenever the extrinsic state is saved elsewhere - then the number
    of objects is not reduced.
    **** **** **** **** **/**

  4. #4
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    36

    Re: Flyweight pattern

    The main goals are to reduce memory usage and possibly to re-use caching.

    A word processor can avoid the overhead of having an object for each glyph and still operate with objects by using a simple mapping from character data. The glyphs are completely intrinsic and could cache any aspect of their state, such as their outline.

    The colour of the font would be passed into the Draw() function as extrinsic state stored in a text span. Even if extrinsic state is stored on a 1 to 1 basis, you can still get a large degree of memory savings and caching.

  5. #5
    Join Date
    Aug 2005
    Posts
    115

    Re: Flyweight pattern

    Quote Originally Posted by Guysl
    My question was not about the how to implement the pool (map or other manipulation). I wanted to know whether the main goal of this pattern is the reduction of objects since it seems to me that it saves memory but whenever the extrinsic state is saved elsewhere - then the number
    of objects is not reduced.
    That is the goal. It can be done depending on how you are able to save the extrinisic state, as bmoodle's example demonstrates. But it's not always possible. If you are unable to reduce the number of objects, then obviously the Flyweight Pattern doesn't help.

  6. #6
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: Flyweight pattern

    Quote Originally Posted by bmoodie
    The main goals are to reduce memory usage and possibly to re-use caching.
    Couldn't this be accomplished by using a shared pointer like boost::shared_ptr?
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured