Katie%20C.%20O - PowerPoint PPT Presentation

About This Presentation
Title:

Katie%20C.%20O

Description:

Structural pattern used to support a large number of small objects efficiently ... Pattern for managing the use of a discreet set of objects. Design Considerations ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 19
Provided by: cs1113
Category:
Tags: 20c | 20o | glyph | katie

less

Transcript and Presenter's Notes

Title: Katie%20C.%20O


1
Flyweight
  • Katie C. OShea
  • Dennis T. Tillman
  • 11 February 2K2

2
Patterns Presentation
Flyweight
  • Recap
  • Agenda

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
3
Flyweight
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.

4
Description
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
5
Flyweight
Implementation
Portfolio Application
6
Implementation
Flyweight
Document Processing
  • Pattern for managing the use of a small set of
    objects.
  • throughout a system

7
Implementation
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

8
Structure
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
9
Structure
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)
10
Structure
Flyweight
Class Diagram
11
Structure
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)

12
Structure
Flyweight
Object Diagram
page 198
13
Efficiency
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

14
Efficiency
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
15
Implementation
Flyweight
Applications
  • Celebrity portfolio, CD Database
  • Word processing
  • GUI (skins, look and feel)
  • Server connection pool

16
Implementation
Flyweight
Code Example
Balls and Strategies (java)
http//exciton.cs.rice.edu/JavaResources/DesignPat
terns/FlyweightDocs/example/Flyweight.zip
17
Implementation
Flyweight
  • Related Patterns

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
18
Patterns 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
Write a Comment
User Comments (0)
About PowerShow.com