1 / 9

Flyweight An Object Structural Design Pattern

Flyweight An Object Structural Design Pattern. JM Imbrescia. Intent. “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. Motivation (Why use the Flyweight Pattern?).

Download Presentation

Flyweight An Object Structural Design Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FlyweightAn Object Structural Design Pattern JM Imbrescia

  2. Intent • “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.

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

  4. Definitions • Flyweight • A shared object that can be used in multiple contexts simultaneously. • Intrinsic • State information that is independent of the flyweights context. Shareable Information. • 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: • 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 • ConcreteFlyweight • UnsharedConcreeteFlyweight • FlyweightFactory • 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: • 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. • State and Strategy objects are often implemented as flyweights.

More Related