39

I am finishing a course on design patterns, and while reviewing the notes came across something I missed during the semester: Composite vs. Composition. What I managed to understand is that composite is when an object actually encapsulates whole objects, while composition is when it only holds pointers to them.

  1. Is this right? Can someone explain this to me a little better?
  2. When would I prefer one over the other?
Baruch
  • 20,590
  • 28
  • 126
  • 201

1 Answers1

75

Composition

This is a design concept (not really a pattern). This term is used when you want to describe one object containing another one. It occurs very often in Composition over inheritance discussion.

Moreover, composition implies strong ownership. One objects owns (i.e. manages the lifecycle) of another object. When parent is destroyed, all children are destroyed as well. If there is no such strong relationship (children can outlive parent) we are talking about aggregation.

Quoting great example in Wikipedia:

For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.

So as you can see you should choose between composition or aggregation depending on the type of ownership relationship.

Composite pattern

This is a GoF design pattern describing a parent-child strong relationship where the child can be a simple node or a container of other nodes (possibly containing other children).

It is very common in GUI and tree like structure. E.g. in Java Swing a JPanel can hold various controls like text fields, labels, lists, etc. but it can also hold other JPanels which, in turn, can contain simple components and even more nested panels.

Typically Composite design pattern uses composition, however in some cases the parent does not have to own all children. To continue GUI example, you can take one panel and move it to another place (change the parent).

joshweir
  • 5,427
  • 3
  • 39
  • 59
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 1
    +1 for explaining the two, but the OP *also* seemed to need help on the containing vs. just having a pointer issue. – Matt Fenwick Jan 30 '12 at 21:45
  • @MattFenwick: right, I added some clarification and quotes (also about aggregation). – Tomasz Nurkiewicz Jan 30 '12 at 21:55
  • 6
    @baruch: my pleasure. So you've been attending design patterns course and you don't know who the [Gang of Four](http://c2.com/cgi/wiki?GangOfFour) was? :-) – Tomasz Nurkiewicz Jan 31 '12 at 07:24
  • 2
    @baruch: GoF refers to the authors of the canonical Design Patterns book (Gamma, Helm, Johnson, Vlissides). – ybakos Mar 28 '12 at 23:44
  • 1
    Hi @TomaszNurkiewicz i know this is OLD, but this is GOLD... Thanks for the explanation, Anyway i have questions regarding `design concept`, What are the others design concept that available? I have tried to google it around, could not find any. Thanks – kilua Jun 11 '15 at 03:52