0% found this document useful (0 votes)
24 views2 pages

Two Types Intrinsic and Extrinsic. The Object With Intrinsic State Is Called Flyweight Object. When We Implement Flyweight We Create

Flyweight is used when a large number of similar objects need to be created to reduce memory usage. It works by extracting intrinsic, sharable state from objects and making it shared, while extrinsic state remains unshared. For example, in drawing shapes, the label of a shape could be intrinsic state shared across objects, while properties like color and size are extrinsic and not shared. A flyweight factory manages and reuses intrinsic state objects to avoid duplicates and reduce memory usage when a large number of similar objects would otherwise be created.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Two Types Intrinsic and Extrinsic. The Object With Intrinsic State Is Called Flyweight Object. When We Implement Flyweight We Create

Flyweight is used when a large number of similar objects need to be created to reduce memory usage. It works by extracting intrinsic, sharable state from objects and making it shared, while extrinsic state remains unshared. For example, in drawing shapes, the label of a shape could be intrinsic state shared across objects, while properties like color and size are extrinsic and not shared. A flyweight factory manages and reuses intrinsic state objects to avoid duplicates and reduce memory usage when a large number of similar objects would otherwise be created.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Flyweight is used when there is a need to create high number of objects of almost similar nature.

High number of objects consumes high memory and flyweight design pattern gives a solution to reduce the load on memory by sharing objects. It is achieved by segregating object properties into two types intrinsic and extrinsic. Need to create large number of objects. Because of the large number when memory cost is a constraint. When most of the object attributes can be made external and shared.

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. The object with intrinsic state is called flyweight object. When we implement flyweight we create concrete objects and have the intrinsic state stored in that. To create those concrete objects we will have factory and that is called Flyweight factory. This factory is to ensure that the objects are shared and we dont end up creating duplicate objects. Let us take an example scenario of drawing. We need to draw different geometrical shapes like rectangles and ovals in huge number. Every shape may vary in colour, size, fill type, font used. For implementation sake lets limit our shapes to two rectangle and oval. Every shape will be accompanied by a label which directly maps it with the shape. That is all rectangles will have label as R and all ovals will have label as O. Now our flyweight will have intrinsic state as label only. Therefore we will have only two flyweight objects. The varying properties colour, size, fill type and font will be extrinsic . We will have a flyweight factory that will maintain the two flyweight objects and distribute to client accordingly. Client code will use random number generators to create extrinsic properties. We are not storing the extrinsic properties anywhere; we will calculate on the fly and pass it. Use of random number generator is for convenience.

http://javapapers.com/design-patterns/flyweight-design-pattern

You might also like