File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes flyweight pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "flyweight pattern" Watch "flyweight pattern" New topic

flyweight pattern

JigaR Parekh
Ranch Hand

Joined: May 23, 2005
Posts: 112
Intent of flyweight pattern is sharing of large number of fine grained objects.

i can understand that flyweight does this by separating intrinsic and extrinsic, storing intrinsic state in concrete flyweight and let client store or compute extrinsic state.

FlyWeightFactory is responsible for creating ,storing and sharing fly weight objects.

my question is what is purpose of UnsharedConcreteFlyweight?

[Thumbnail for flywe050.gif]

deepak adlakha
Ranch Hand

Joined: Jul 27, 2001
Posts: 325
What exactly you mean by UnsharedConcreteFlyweight?
JigaR Parekh
Ranch Hand

Joined: May 23, 2005
Posts: 112
In GOF book below is explained but not it is not at all clear to me what exactly it does and in what scenario it should be used

UnsharedConcreteFlyweight :
Not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing; it doesn't enforce it. It's common for UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level in the flyweight object structure (as the Row and Column classes have).

Rest of elements in GOF diagram are clear to me.
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi Jigar,

After your post I did some Googling on flyWeight pattern and following are some main points that I think we must keep in mind while using Flyweight pattern in our application:

1) Flyweight is used when we want to avoid creating multiple objects of the same kind and share existing instances. These objects are shared based on the intrinsic state + extrinsic state. I believe this part is clear to you.

2) We can have a set of flyweights which we don't want to share. Now lets discuss this part. We need to remember the following points for the unshared flyweights:
a) This is an optional part of the flyweight. We need not have in all the flyweight pattern implementations.
b) Understand when we use it. For example:- if we want to design an application where we have shopping cart collection object containing different products we can use Flyweight pattern. Now for each product we have an intrinsic state like the name, description, feature set etc. and extrinsic state like the number of this product present in shopping cart. Here we can easily use shared flyweight for all the different product objects. But we can have some products like assembled computer which cannot be same for all customers who plans to buy it. For this type of products we might wish to create a new object whenever any customer adds it in their basket.

Hope this helps,

SCJP 1.5
I agree. Here's the link:
subject: flyweight pattern
It's not a secret anymore!