Sunday, 6 June 2010

Fly Weight Design Pattern

General meaning of Fly Weight means reducing the weight, here reducing the unnecessary objects.

Definition:

which is used to avoid too many objects of similar types instead try to use the same old object repetively. Create new Object when you really need them.

Fly Weight Design Pattern is used to when there is a need to create high number of objects of almost similar nature. High number of objects consumes (takes) high memory and flyweight design pattern gives a solution to reduce the load on memory by sharing objects. It is achieved by segregating objects properties into two types intrinsic (internal)  and extrinsic (outside or external).

Use sharing to support large numbers of fine-grained objects efficiently". Sharing is key in flyweight pattern and we need to properly decide if this pattern can be applied.

How to Apply Fly Weight Design Pattern:

The object which we are going to create in high number should be analysed before going for Fly Weight Design Pattern. Idea is to create lesser number of objects by reusing the same objects. Create smaller groups of objects and they should be reused by sharing. Closely look at objects properties and they can be segregated as two types intrinsic and extrinsic

Ex:
for ice-cream, brand name is intrinsic. flavor is extrinsic.
for Book, Book name is intrinsic, type of paper, quality of printing is extrinsic,
for Park, visitor name is intrinsic, visitor ticket type is extrinsic



Fly Weight Design PatternExample programs on Fly Weight Design pattern : Student marks list

No comments:

Post a Comment