luni, 23 noiembrie 2009

[DesignPatterns] Flyweight Pattern

The Flyweight pattern promotes an efficient way to share common information present in small objects that occur in a system in large numbers. Helps reduce storage requirements when many values are duplicated.

• The intrinsic state can be shared on a wide scale, minimizing storage requirements.
• The extrinsic state can be computed on the fly, trading computation for storage.

Flyweight pattern relies on being able to divide up the application’s state into three types:
- intrinsicState resides in the Flyweight objects
- Flyweight class implements an IFlyweight interface, which specifies the operations upon which the rest of the system relies. Client maintains the unSharedState as well as a dictionary of all the Flyweights.
- extrinsicState does not appear in the system as such; it is meant to be computed at runtime for each instrinsicState instance, as required.

FlyweightFactory - Creates and manages unique Flyweight objects
Flyweight - Stores intrinsic state that is shareable among all objects (they store only a simple representation of a bigger object. for example, for a image thay keep only a Thumbnai).

new Bitmap(filename). GetThumbnailImage(100, 100, null, new IntPtr( ));

public interface IFlyweight {
void Load (string filename);
void Display (PaintEventArgs e, int row, int col); }

• There is a very large number of objects (thousands) that may not fit in memory.
• Most of the state can be kept on disk or calculated at runtime.
• The remaining state can be factored into a much smaller number of objects with shared state.

Niciun comentariu:

Trimiteți un comentariu