Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flyweight Design Pattern

Similar presentations


Presentation on theme: "Flyweight Design Pattern"— Presentation transcript:

1 Flyweight Design Pattern
Cse 776 design patterns Summer ‘10 Bharath setty Dr. James fawcett

2 Intent “Use sharing to support large numbers of fine-grained objects efficiently.” One object instance holding shared (intrinsic) state Unique (extrinsic) state is stored outside of the shared object

3 Motivation Folder Representation

4 Forces Problem Context Forces Solution Benefits Consequences
Related Patterns John Reekie (UTS)

5 Forces To obtain a fine grained object structure
We will possibly have many objects Cost of storing a copy of each object is high

6 Motivation Example Folder Representation

7 flyweight pattern is used when all of the following are true
Applicability flyweight pattern is used when all of the following are true An application has a large number of objects Store costs are high Most object state can be made extrinsic??? Authors claim. I strongly disagree You want most of object state intrinsic, e.g., shared Many groups of objects will be replaced by few shared objects(intrinsic) The application doesn’t depend on object identity

8 Structure

9 Participants Flyweight (Window)
Declares interface that flyweights can use to receive and act on intrinsic state ConcreteFlyweight (Icon) Implements flyweight interface and adds storage for intrinsic state. Must be shareable UnsharedConcreteFlyweight (Name, Location) Commonly has ConcreteFlyweights as children Flyweightfactory Creates and manages flyweight objects Client Maintains references to flyweights Computes or stores extrinsic state of flyweights

10 Application

11 Collaborations State of Flyweight is characterized by intrinsic and extrinsic state Intrinsic state stored in ConcreteFlyweight Extrinsic state stored or computed by Client Objects Clients should not instantiate ConcreteFlyweights directly Proper sharing will not occur

12 Consequences Pros: Cost saved by space savings (Function of reduction of number of instances and amount of intrinsic state per object) Cons: Cost increased in run-time to transfer, find or compute extrinsic state

13 Implementation Removing Extrinsic State
Must be easily identifiable and be removed from shared objects Pattern is only useful if state can be shared Managing Shared Objects Clients should not instantiate ConcreteFlyweights directly Flyweight factory allows clients to locate a particular flyweight Reference counting and garbage collection can be used

14 Known Uses 2D/3D Vector drawing program 2D/3D Video game
Cad applications

15 Related Patterns Flyweight is often combined with Composite pattern to implement a logically hierarchical structure in terms of a graph with shared leaf nodes State and Strategy can be implemented as flyweights State: An object can alter its behavior when its internal state changes Strategy: Define a family of algorithms and make them interchangeable

16 References Design Patterns, Elements of Reusable Object-Oriented Software, Erich Gamma, et. al.

17 Questions????


Download ppt "Flyweight Design Pattern"

Similar presentations


Ads by Google