Download presentation
Presentation is loading. Please wait.
1
Katie C. O’Shea Dennis T. Tillman 11 February 2K2 Flyweight
2
Patterns Presentation Recap Agenda Purpose CreationalStructuralBehavioral ScopeClass 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
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. Description Definition Flyweight(fli-wat) Something that is particularly small, light, or inconsequential.
4
Description Pattern for managing the use of a discreet set of objects. Design Considerations 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 Operations Organization Flyweight
5
CD: a > year > title CD: b > year > title CD: c > year > title :artistFactory Sinatra: artistFlyweight > artistName Nirvana: artistFlyweight > artistName Implementation Portfolio Application Flyweight
6
Implementation Pattern for managing the use of a small set of objects. Document Processing throughout a system Flyweight
7
Implementation Document Processing throughout a system In multiple contexts throughout a system In multiple contexts Pattern for managing the use of a small set of objects. Flyweight
8
Structure Row client supports context dependent information — tells children where they should draw themselves by passing location information Word Processor Example Flyweight storing a single character — doesn’t need to know location or font Supplies context dependent information that the Flyweight needs to draw itself Glyph Draw(Context) Intersection(Point, Context) GLYPH, the abstract class for graphical objects char c Row Draw(Context) Intersection(Point, Context) Column Draw(Context) Intersection(Point, Context) Character Draw(Context) Intersection(Point, Context) Flyweight
9
Structure Word Processor Example Row client supports context dependent information — tells children where they should draw themselves by passing location information Supplies context dependent information that the Flyweight needs to draw itself Glyph Draw(Context) Intersection(Point, Context) GLYPH, the abstract class for graphical objects char c Row Draw(Context) Intersection(Point, Context) Column Draw(Context) Intersection(Point, Context) Character Draw(Context) Intersection(Point, Context) Flyweight storing a single character — doesn’t need to know location or font Flyweight
10
Structure Class Diagram Flyweight
11
Structure Class Diagram Creates, manages Flyweights Dispenses when requested Ensures sharing (checks existence) Maintains references to Flyweight Supplies context dependent information that the Flyweight needs to draw itself Implements Flyweight interface Store intrinsic state (must be) shareable For subclasses that do not need to be shared The Flyweight is the set of intrinsic information that a set of objects share in common. It is abstract. Flyweight
12
Structure Object Diagram Flyweight
13
Efficiency Benefits and Consequences +- 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 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 Flyweight
14
Efficiency Benefits and Consequences v A document uses about 100 character objects (the ASII set) 100 Chars shared in 8,000 char document 100 Chars shared in 200,000 char document Number of Objects Shared f(g): reduction in number of instances amount of intrinsic state state per object computation storage Savings Flyweight
15
Implementation Applications Celebrity portfolio, CD Database Word processing GUI (skins, look and feel) Server connection pool Flyweight
16
Implementation Code Example http://exciton.cs.rice.edu/JavaResources/DesignPatterns/FlyweightDocs/example/Flyweight.zip Balls and Strategies (java) Flyweight
17
Implementation Related Patterns Purpose CreationalStructuralBehavioral ScopeClass 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 State Strategy Composite Flyweight
18
Patterns Presentations Catalogued patterns - Recap Purpose CreationalStructuralBehavioral ScopeClass 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 Flyweight
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.