Flyweight: - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Flyweight:

Description:

... the object's context, sharable (e.g. State might include name, postal ... that can be calculated on the spot (e.g. State might need access to region) ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 12
Provided by: gawe
Category:
Tags: flyweight | spot

less

Transcript and Presenter's Notes

Title: Flyweight:


1
  • Flyweight
  • Structural Pattern


  • Prepared by Galina Walters

2
  • Flyweight pattern
  • describes how to share objects
    to allow their use at fine granularities without
    prohibitive cost. A flyweight is a shared object
    that can be used in multiple contexts
    simultaneously.

3
  • Name
  • Flyweight
  • Intent
  • Use sharing to support large numbers of
    fine-grained objects efficiently
  • Context
  • Desire to Use Objects Throughout Design
  • Including the Finest Level of Granularity
  • Expensive if the Objects are not Shared
  • Desire to Reduce System Footprint
  • Memory is Limited
  • Problem
  • Placing the same information in many different
    place
  • Mistakes can be made in copying the information
  • If the data changes, all occurrences of the
    information must be located and changed. This
    makes maintenance of the document difficult
  • Documents can become quite large if the same
    information is repeated over and over again
  • Issues

4
Solution
  • Create Flyweight Objects for Intrinsic State
  • Intrinsic State - Information independent of the
    objects context, sharable (e.g. State might
    include name, postal abbreviation, time zone,
    etc.). This is included in the Flyweight and
    instead of storing the same information n times
    for n objects, it is only stored once
  • Extrinsic State - Information dependent on the
    objects context, unsharable, stateless, having
    no stored values, but values that can be
    calculated on the spot (e.g. State might need
    access to region). This is excluded from the
    Flyweight
  • Flyweight Objects
  • Shared, Pooled Instances
  • Used in Multiple Contexts Simultaneously
  • Indistinguishable From Non-Shared Instance
  • Cannot Make Assumptions about Context
  • Flyweight Requires Special Coding
  • Deserialization Must Replace Objects as Loaded
  • Copying an Instance Must Return the Instance

5
Example OO Document Editor
  • Using an object for each character in the
    document
  • Promote flexibility at the finest levels in the
    application
  • Characters and embedded elements could be treated
    uniformly with respect to how they are drawn and
    formatted
  • The application could be extended to support new
    character sets without disturbing other
    functionality
  • The applications object structure could mimic
    the documents physical structure
  • logically

    physically
  • Creating a flyweight for each letter of the
  • alphabet
  • Intrinsic state a character code
  • Extrinsic state coordinate position in the
    document
  • typographic style (font, color)
  • is determined from the text layout algorithms and
    formatting commands in effect wherever the
    character appears

6
Flyweight Pattern Participants
  • The Flyweight
  • is the set of intrinsic information that a set of
    objects share in common. It is abstract
  • The ConcreteFlyweight
  • is a subclass of the Flyweight that contains all
    of its information, as well as how to calculate
    extrinsic information for a particular (yet
    arbitrary) object. It is shared among more than
    one object
  • The FlyweightFactory
  • serves to dispense particular flyweights
    requested. When a Flyweight with certain
    properties is requested, it checks to see if one
    already exists, and if so, returns that
    flyweight. If the requested flyweight does not
    exist, it creates the required flyweight, stores
    it, and then returns it
  • The Client
  • in creating a new object must assign a flyweight
    to it, so it asks the FlyweightFactory for a
    particular flyweight, receives that flyweight,
    and creates a reference to it in the object it is
    creating.

7
Flyweight Pattern Structure
8
  • public class Client extends Object
  • / Uses the Flyweight Factory to create the
    flyweight it needs. When the Client needs the
    flyweight to do something, it calls the
  • appropriate method of the flyweight,
    passing it any extrinsic state information it
    might need as its context /

  • private Flyweight flyweight

  • private FlyweightFactory flyweightFactory
  • public abstract class Flyweight extends Object
  • /The abstract Flyweight class. The concrete
    subclasses may or may not be shared. /
  • public abstract Object
    doSomething(Object context) / The task the
    flyweight performs. /

  • Parameters context - The extrinsic state
    information needed to perform the task.

  • Returns The appropriate result.
  • public class ConcreteFlyweightA extends Flyweight
  • / A concrete flyweight for a specific class of
    tasks. /
  • public Object
    doSomething(Object context) / The task this
    flyweight performs. /

  • Parameters context - The extrinsic state
    information needed to perform the task.

  • Returns The appropriate result.
  • public class FlyweightFactory extends Object
  • / Maintains a pool of instantiated flyweights.
    When the Client requests a shareable flyweight,
    the factory first searches the pool
  • to see if there is one and returns that
    instance. Otherwise it instantiates a new
    flywieght,adds it to the pool and returns a
  • reference to the client. /

9
Consequences
  • Flyweights Lose Equality, Gain Identity
  • Same Instance used Many Places
  • Flyweights Must Separate Intrinsic/Extrinsic
  • Flyweight Representing a Node in a Graph Cannot
    Point to Parent (Parent must be Passed)
  • Flyweights Save Space
  • More Flyweights Shared Yields More Savings
  • More Intrinsic State Yields More Savings
  • Flyweights Are Found, Not Instantiated
  • Slight Shift in Terminology
  • Flyweights May Introduce CPU Costs
  • Finding Flyweights (Large Pool Increases)
  • Computing Extrinsic State (Previously Stored)
  • Transferring the Extrinsic information

10
Flyweight is not for everything
  • The Flyweight pattern should not be used if the
    shared information can vary over time. This would
    increase the maintenance burden of the document
  • Readability of the document can suffer if the
    Flyweight pattern is used, readers are forced to
    reference a different section of the document
    when looking at the contents

11
References
  • http//hillside.net/patterns/DPBook/0-width.pdf
  • http//exciton.cs.rice.edu/JavaResources/DesignPat
    terns/FlyweightPattern.htm
  • http//www.xml.com/pub/a/2000/01/19/feature/?page
    3
  • http//www.xmlpatterns.com/FlyweightMain.shtml
  • http//www.ciol.com/content/technology/sw_desg_pat
    t/101080901.asp
  • http//www.cis.ksu.edu/hankley/d644/Patterns/Flig
    hweight/FlyPattern.html
  • www.microgold.com/version2/articles/article2/artic
    le2.html
  • www.cijug.org/presentation/CIJUG-Patterns.ppt
Write a Comment
User Comments (0)
About PowerShow.com