Title: Katie%20C.%20O
1Flyweight
- Katie C. OShea
- Dennis T. Tillman
- 11 February 2K2
2Patterns Presentation
Flyweight
Purpose
Creational Structural Behavioral
Scope Class Deferring actions to subclasses Composition, inheritance Models that describe algorithms and flow of control
Object Deferring actions to other objects Models that describe how to manage objects Specifying how things work together
Flyweight
3Flyweight
Description
Definition
- Flyweight(fli-wat)
- Something that is particularly small, light, or
inconsequential.
- Comp Sci Pattern
- Structural pattern used to support a large number
of small objects efficiently - Flyweight factors the common properties of
multiple instances of a class into a single
object, saving space and maintenance of duplicate
instances.
4Description
Flyweight
Design Considerations
- Pattern for managing the use of a discreet set of
objects.
Operations
- throughout a system
- singularly, independently
- in parallel contexts
- Reduce costs when sheer quantities of objects
require increased storage - Deal with intrinsic and external states
independently - Replace groups with shared objects
- Application must not rely on object identity
Organization
5Flyweight
Implementation
Portfolio Application
6Implementation
Flyweight
Document Processing
- Pattern for managing the use of a small set of
objects.
7Implementation
Flyweight
Document Processing
- throughout a system
- In multiple contexts
- Pattern for managing the use of a small set of
objects.
- throughout a system
- In multiple contexts
8Structure
Flyweight
Word Processor Example
- Row client supports context dependent information
tells children where they should draw
themselves by passing location - information
- GLYPH, the abstract class for graphical objects
Flyweight storing a single character doesnt
need to know location or font
char c
Supplies context dependent information that the
Flyweight needs to draw itself
9Structure
Flyweight
Word Processor Example
Row client supports context dependent information
tells children where they should draw
themselves by passing location information
Row
Draw(Context) Intersection(Point, Context)
- GLYPH, the abstract class for graphical objects
Character
Flyweight storing a single character doesnt
need to know location or font
Glyph
Draw(Context) Intersection(Point, Context)
Draw(Context) Intersection(Point, Context)
char c
Column
Supplies context dependent information that the
Flyweight needs to draw itself
Draw(Context) Intersection(Point, Context)
10Structure
Flyweight
Class Diagram
11Structure
Flyweight
Class Diagram
- Maintains references to Flyweight
- Supplies context dependent information that the
Flyweight needs to draw itself
- The Flyweight is the set of intrinsic information
that a set of objects share in common. It is
abstract.
- For subclasses that do not need to be shared
- Implements Flyweight interface
- Store intrinsic state
- (must be) shareable
- Creates, manages Flyweights
- Dispenses when requested
- Ensures sharing
- (checks existence)
12Structure
Flyweight
Object Diagram
page 198
13Efficiency
Flyweight
Benefits and Consequences
-
If the size of the set of objects used
repeatedly is substantially smaller than the
number of times the object is logically used,
there may be an opportunity for a considerable
cost benefit
- Overhead to track state
- Transfer
- Search
- computation
- When Not To Use Flyweight
- If the extrinsic properties have a large amount
of state information that would need passed to
the flyweight (overhead) - Need to be able to be distinguished shared from
non-shared objects
- When To Use Flyweight
- There is a need for many objects to exist that
share some intrinsic, unchanging information - Objects can be used in multiple contexts
simultaneously - Acceptable that flyweight acts as an independent
object in each instance
14Efficiency
Flyweight
Benefits and Consequences
- f(g)
- reduction in number of instances
- amount of intrinsic state
- state per object
- computation
- storage
Savings
Number of Objects Shared
15Implementation
Flyweight
Applications
- Celebrity portfolio, CD Database
- Word processing
- GUI (skins, look and feel)
- Server connection pool
16Implementation
Flyweight
Code Example
Balls and Strategies (java)
http//exciton.cs.rice.edu/JavaResources/DesignPat
terns/FlyweightDocs/example/Flyweight.zip
17Implementation
Flyweight
Purpose
Creational Structural Behavioral
Scope Class Deferring actions to subclasses Composition and inheritance Models that describe algorithms and flow of control
Object Deferring actions to other objects Models that describe how to manage objects Specifying how things work together
Flyweight
18Patterns Presentations
Flyweight
- Catalogued patterns - Recap
Purpose
Creational Structural Behavioral
Scope Class Factory Method Adapter (class) Interpreter Template Method
Object Abstract Factory Builder Prototype Singleton Adapter (object) Bridge Composite Decorator Façade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor