aspose file tools*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes GOF Question (Composite vs Flyweight) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "GOF Question (Composite vs Flyweight)" Watch "GOF Question (Composite vs Flyweight)" New topic
Author

GOF Question (Composite vs Flyweight)

Da Zhu
Greenhorn

Joined: Jan 02, 2001
Posts: 29
I doubt about the relationship between the Composite
and Flyweight patterns, written in the Disign Pattern Relationships Diagram inside the backcover of GOF Design Patterns book. (also on p12,I have the 21th Printing, Novemeber 2000)
It is written there that the relation from Composite to
Flyweight is "sharing composites"
I think it should be written as "sharing components" (p173) or
"sharing leaf nodes" (p206)
Can someone confirm for me? Thanks.
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
The Leaf participant has no children as it is the primitive type. Leaf doesn't embody the Composite pattern, so Leaf is out. Component is an abstract class which can include the Leaf type in a collection Components. The Composite is the type that has children and implements those related operations. Those are the objects that the GoF wanted to illustrated a relationship to Flyweights because you might want to share these complex objects.
That doesn't say that in any given implementation, the Leaf nodes couldn't be shared if their extrinsic state can be removed.
But who am I to judge the sermon on the mountain?
Scott
Da Zhu
Greenhorn

Joined: Jan 02, 2001
Posts: 29
I do not agree with Scott. The flyweight pattern suggests
the sharing of flyweight objects, these are the leaf node
in the composite pattern, not the complex objects (composite).
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
Are you saying you can't pool complex objects? What is big benefit of pooling a primitive object (e.g., Boolean)? I don't think the GoF precludes you from pooling simple objects (they might be expensive to create and hold resources), but their intent (IMO) was to illustrate a bigger bang.
I don't limit flyweight to lightweight. I see flyweight as anything that can be shared by externalizing specific state. I don't think a Stateless Session bean could be defined as "lightweight", but the container uses the flyweight pattern.
This might be a good question for the OO/Patterns board posters and get their input.
Scott
Da Zhu
Greenhorn

Joined: Jan 02, 2001
Posts: 29
Scott,
Q1) yes, Q2) check the book
Does the name flyweight give you some hint of the usage of the pattern, if not, consider the flyweight objects in the document editor example (used in both GOF and Grand books) what are they there? the char's. Still not convinced? read the intent in the GOF book:
"Using sharing to support large numbers of fine-grained object efficiently."
Please do not discuss with me the difference of the meaning of flyweight and lightweight, since English is not my native language.
[This message has been edited by Da Zhu (edited June 01, 2001).]
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
Okay, fine I'll open the book....
Q1) Your loss.
Q2) Your right, that's what it says. I just don't implement text editors, but have used the key concept for other uses.
You appear to be correct on the relationship notation on page 12. The Flyweight related pattern section says "shared leaf nodes" and the Composite related pattern section says "share components". I still say you can share composites, so I vote for the "share components" notation on page 12.
Scott
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GOF Question (Composite vs Flyweight)