Prepared By : Hasnaeen Rizvi Rahman
Astha School of Advanced Computing
   Use sharing to support large number of
    fine-grained objects efficiently.
   An application uses many objects.
   A flyweight is a shared object that can be
    used in multiple contexts simultaneously.
   It acts as an individual object in each
    context.
   The key concept is the distinction
    between intrinsic and extrinsic state.
   An application uses a large number of
    objects.
   Storage costs are high because of the
    sheer quantity of objects.
   Most objects state can be made extrinsic.
   Many groups of objects may be replaced
    by relatively few shared objects once
    extrinsic state is removed.
   The application doesn’t depends on
    object identity.
   Flyweight (Glyph)
    • Declare an interface through which flyweights can receive
      and act on extrinsic state.
   ConcreteFlyweight (Character)
    • Implements the Flyweight interface and adds storage for
      intrinsic state, if any.
   UnsharedConcreteFlyweight (Row,Column)
    • Not all flyweight subclasses need to be shared.
   Flyweight Factory
    • Creates and manages flyweight objects.
    • Ensures that flyweights are shared properly.
   Client
    • Maintains a reference to flyweights
    • Computes or stores the extrinsic state of flyweight.
   Client passes extrinsic state to flyweight.
   Client invokes FlyweightFactory to create
    Flyweight object.
   Flyweight may introduce run-time cost.
   Storage savings are a function of several
    factors:
    • The reduction in the total number of instances
      that comes from sharing.
    • The amount of intrinsic state per object.
    • Whether extrinsic state is computed or stored.
   Removing extrinsic state.
   Managing shared objects.
   Design an application which stores list of
    entities.
    • Entity: It has an ID, Name and an attribute.
    • Attribute: Attributes are composite data where each
      attribute is either a leaf or a composite. It has all
      child management operation suggested in
      composite pattern. A leaf attribute have a name and
      value. A Composite attribute have a name but no
      value. Attribute returns an XML having Name and
      Value node. The composite attribute returns the list
      of child attribute XML.
   Entity XML could be something like…
    <Entity>
      <ID>1</ID>
      <Name>Entity 1</Name>
      <Attribute>
            <Name>Entity Attribute</Name>
            <Children>
              <Attribute>
                       <Name>OrgChart</Name>
                       <Children>
                         <Attribute>
                                  <Name>Type</Name>
                                  <Value>Corporation</Value>
                         </Attribute>
                         <Attribute>
                                  <Name>Country</Name>
                                  <Value>Bangladesh</Value>
                         </Attribute>
                       </Children>
              </Attribute>
            </Children>
      </Attribute>
    </Entity>
   Use Flyweight pattern to reduce memory usage for
    attributes.

Flyweight pattern

  • 1.
    Prepared By :Hasnaeen Rizvi Rahman Astha School of Advanced Computing
  • 2.
    Use sharing to support large number of fine-grained objects efficiently.
  • 3.
    An application uses many objects.  A flyweight is a shared object that can be used in multiple contexts simultaneously.  It acts as an individual object in each context.  The key concept is the distinction between intrinsic and extrinsic state.
  • 8.
    An application uses a large number of objects.  Storage costs are high because of the sheer quantity of objects.  Most objects state can be made extrinsic.  Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed.  The application doesn’t depends on object identity.
  • 11.
    Flyweight (Glyph) • Declare an interface through which flyweights can receive and act on extrinsic state.  ConcreteFlyweight (Character) • Implements the Flyweight interface and adds storage for intrinsic state, if any.  UnsharedConcreteFlyweight (Row,Column) • Not all flyweight subclasses need to be shared.  Flyweight Factory • Creates and manages flyweight objects. • Ensures that flyweights are shared properly.  Client • Maintains a reference to flyweights • Computes or stores the extrinsic state of flyweight.
  • 12.
    Client passes extrinsic state to flyweight.  Client invokes FlyweightFactory to create Flyweight object.
  • 13.
    Flyweight may introduce run-time cost.  Storage savings are a function of several factors: • The reduction in the total number of instances that comes from sharing. • The amount of intrinsic state per object. • Whether extrinsic state is computed or stored.
  • 14.
    Removing extrinsic state.  Managing shared objects.
  • 15.
    Design an application which stores list of entities. • Entity: It has an ID, Name and an attribute. • Attribute: Attributes are composite data where each attribute is either a leaf or a composite. It has all child management operation suggested in composite pattern. A leaf attribute have a name and value. A Composite attribute have a name but no value. Attribute returns an XML having Name and Value node. The composite attribute returns the list of child attribute XML.
  • 16.
    Entity XML could be something like… <Entity> <ID>1</ID> <Name>Entity 1</Name> <Attribute> <Name>Entity Attribute</Name> <Children> <Attribute> <Name>OrgChart</Name> <Children> <Attribute> <Name>Type</Name> <Value>Corporation</Value> </Attribute> <Attribute> <Name>Country</Name> <Value>Bangladesh</Value> </Attribute> </Children> </Attribute> </Children> </Attribute> </Entity>
  • 17.
    Use Flyweight pattern to reduce memory usage for attributes.