Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Apparently, a composite design pattern represents individual entities and collective entities in the same manner.

What is the advantage to this? For instance, if I subclass a class, why would I want to treat objects of both classes in the same way?

For instance, if 'tuna' is a subclass of 'fish', why would I want to have the same implementations for methods of objects in both classes?

share|improve this question
1  
Since all hierarchies (i.e., XML Document Tags, File System Directory Trees) are composites, it's not clear what the question is. Clearly, composite objects (like XML documents and file directories) are everywhere. Are you asking for more examples of composite data items? –  S.Lott Feb 18 '12 at 22:04
    
I think that is what I'm confused about. What is the composite design pattern describing? How is it different from a simple hierarchy of say XML tags? –  David Faux Feb 18 '12 at 22:07
    
It's not different. That's why I don't understand the question. A hierarchy is a composite object. Can you clarify what you're asking by -- perhaps -- getting rid of the "Fish" example. Can you ask about "collection-like" things? A composite object is a collection. –  S.Lott Feb 18 '12 at 22:10
    
You mixed a parent of an instance and an ancestor of a class. The first is about instances, the second is about classes. Example is very unappropriate - I can't imagine a situation in which you should manage a tuna and a group of them (parent of the tuna instance) in the same way. –  Gangnus Feb 19 '12 at 21:53

1 Answer 1

up vote 6 down vote accepted

The relationship being described in composite design patterns isn't a subclass relationship, it's a collection relationship. In other words, it's about treating a tuna the same as a school of tuna. That way for operations like translating, rotating, scaling, drawing, etc. the caller doesn't need to care if it is acting on one object or an entire collection.

share|improve this answer
1  
wait... the correct collective noun for tuna is .. a "school" of tuna !? –  Stefano Borini Feb 18 '12 at 22:56
1  
Or a "Shoal of Tuna". But. It's a "Hover of Trout". –  S.Lott Feb 19 '12 at 3:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.