Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flyweight An Object Structural Design Pattern JM Imbrescia.

Similar presentations


Presentation on theme: "Flyweight An Object Structural Design Pattern JM Imbrescia."— Presentation transcript:

1 Flyweight An Object Structural Design Pattern JM Imbrescia

2 Intent “Use Sharing to support large numbers of fine- grained objects efficiently.” “Use Sharing to support large numbers of fine- grained objects efficiently.” Simply put, a method for storing a small number of complex objects that are used repeatedly. Simply put, a method for storing a small number of complex objects that are used repeatedly.

3 Motivation (Why use the Flyweight Pattern?) If you have an application that would benefit from using objects throughout your design. If you have an application that would benefit from using objects throughout your design. –Document Editors –50 Decks of cards

4 Definitions Flyweight Flyweight –A shared object that can be used in multiple contexts simultaneously. Intrinsic Intrinsic –State information that is independent of the flyweights context. Shareable Information. Extrinsic Extrinsic –State information that depends on the context of the flyweight and cannot be shared.

5 Applicability Use the Flyweight when all of the following are true: Use the Flyweight when all of the following are true: –Application has a large number of objects. –Storage costs are high because of the large quantity of objects. –Most object state can be made extrinsic. –Many groups of objects may be replaced by relatively few once you remove their extrinsic state. –The application doesn’t depend on object identity.

6 Participants Flyweight Flyweight ConcreteFlyweight ConcreteFlyweight UnsharedConcreeteFlyweight UnsharedConcreeteFlyweight FlyweightFactory FlyweightFactory Client Client

7 Image from the book thanks to: http://www.cs.jhu.edu/~scott/oos/lectures/PatternDiagrams

8 Consequences Flyweights introduce runtime costs, these costs need to be offset by storage space savings. Savings come from: Flyweights introduce runtime costs, these costs need to be offset by storage space savings. Savings come from: –Fewer total instances due to sharing. –Increasing intrinsic space per object. –Computing extrinsic state instead of storing it.

9 Related Patterns Often combined with the Composite pattern. Often combined with the Composite pattern. State and Strategy objects are often implemented as flyweights. State and Strategy objects are often implemented as flyweights.


Download ppt "Flyweight An Object Structural Design Pattern JM Imbrescia."

Similar presentations


Ads by Google