180 likes | 313 Views
Flyweight. Katie C. O’Shea Dennis T. Tillman 11 February 2K2. Flyweight. Patterns Presentation . Recap Agenda. Flyweight. Flyweight. Description. Definition. Flyweight(fli-wat) Something that is particularly small, light, or inconsequential. . Comp Sci: Pattern
E N D
Flyweight Katie C. O’Shea Dennis T. Tillman 11 February 2K2
Flyweight Patterns Presentation Recap Agenda Flyweight
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 objectsefficiently • Flyweight factors the common properties of multiple instances of a class into a single object, saving space and maintenance of duplicate instances.
Flyweight Description Design Considerations Pattern for managing the use of a discreet set of objects. Operations • throughout a system • singularly, independently • in parallel contexts Organization • 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
CD: a CD: b CD: c <<extrinsic state>> year <<extrinsic state>> title <<extrinsic state>> year <<extrinsic state>> title <<extrinsic state>> year <<extrinsic state>> title Nirvana: artistFlyweight Sinatra: artistFlyweight :artistFactory <<intrinsic state>> artistName <<intrinsic state>> artistName Flyweight Implementation Portfolio Application
Flyweight Implementation Document Processing Pattern for managing the use of a small set of objects. • throughout a system
Flyweight Implementation Document Processing Pattern for managing the use of a small set of objects. • throughout a system • In multiple contexts • throughout a system • In multiple contexts
Structure Row client supports context dependent information — tells children where they should draw themselves by passing location information Glyph Row Draw(Context) Intersection(Point, Context) Draw(Context) Intersection(Point, Context) Column Draw(Context) Intersection(Point, Context) Character Draw(Context) Intersection(Point, Context) Flyweight Word Processor Example • GLYPH, the abstract class for graphical objects Flyweight storing a single character — doesn’t need to know location or font char c Supplies context dependent information that the Flyweight needs to draw itself
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 — doesn’t 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)
Flyweight Structure Class Diagram
Flyweight Structure 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)
Flyweight Structure Object Diagram page 198
Flyweight Efficiency 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
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 Flyweight Efficiency Benefits and Consequences • f(g): • reduction in number of instances • amount of intrinsic state • state per object • computation • storage Savings Number of Objects Shared
Flyweight Implementation Applications • Celebrity portfolio, CD Database • Word processing • GUI (skins, look and feel) • Server connection pool
Flyweight Implementation Code Example Balls and Strategies (java) http://exciton.cs.rice.edu/JavaResources/DesignPatterns/FlyweightDocs/example/Flyweight.zip
Composite State Strategy Flyweight Implementation Related Patterns Flyweight
Flyweight Patterns Presentations Catalogued patterns - Recap