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?

  • 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, 2012 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, 2012 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, 2012 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, 2012 at 21:53

2 Answers 2


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.

  • 1
    wait... the correct collective noun for tuna is .. a "school" of tuna !? Feb 18, 2012 at 22:56
  • 1
    Or a "Shoal of Tuna". But. It's a "Hover of Trout".
    – S.Lott
    Feb 19, 2012 at 3:42

You don't apply Composite to treat Tuna and Fish the same way. To do that, you simply use Fish, because every Tuna is Fish.

Composite is useful when multiple items expose the same interface as each of them separate. But there is the twist. Composite element is then responsible to iterate through the collection (like in this article: Working With Collections) and to aggregate multiple results into a single result (like in this article: Composite Design Pattern).

Actually, Composite enforces Single Responsibility Principle in that it separates the aggregation step from actual feature, which is then enclosed in the simple element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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