Browser docs

Composite

Intent

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Explanation

Real-world example

Every sentence is composed of words which are in turn composed of characters. Each of these objects are printable and they can have something printed before or after them like sentence always ends with full stop and word always has space before it.

In plain words

Composite pattern lets clients uniformly treat the individual objects.

Wikipedia says

In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects is to be treated in the same way as a single instance of an object. The intent of a composite is to “compose” objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

Programmatic Example

Taking our sentence example from above. Here we have the base class LetterComposite and the different printable types Letter, Word and Sentence.

 1public abstract class LetterComposite {
 2
 3  private final List<LetterComposite> children = new ArrayList<>();
 4
 5  public void add(LetterComposite letter) {
 6    children.add(letter);
 7  }
 8
 9  public int count() {
10    return children.size();
11  }
12
13  protected void printThisBefore() {
14  }
15
16  protected void printThisAfter() {
17  }
18
19  public void print() {
20    printThisBefore();
21    children.forEach(LetterComposite::print);
22    printThisAfter();
23  }
24}
25
26public class Letter extends LetterComposite {
27
28  private final char character;
29
30  public Letter(char c) {
31    this.character = c;
32  }
33
34  @Override
35  protected void printThisBefore() {
36    System.out.print(character);
37  }
38}
39
40public class Word extends LetterComposite {
41
42  public Word(List<Letter> letters) {
43    letters.forEach(this::add);
44  }
45
46  public Word(char... letters) {
47    for (char letter : letters) {
48      this.add(new Letter(letter));
49    }
50  }
51
52  @Override
53  protected void printThisBefore() {
54    System.out.print(" ");
55  }
56}
57
58public class Sentence extends LetterComposite {
59
60  public Sentence(List<Word> words) {
61    words.forEach(this::add);
62  }
63
64  @Override
65  protected void printThisAfter() {
66    System.out.print(".");
67  }
68}

Then we have a messenger to carry messages:

 1public class Messenger {
 2
 3  LetterComposite messageFromOrcs() {
 4
 5    var words = List.of(
 6        new Word('W', 'h', 'e', 'r', 'e'),
 7        new Word('t', 'h', 'e', 'r', 'e'),
 8        new Word('i', 's'),
 9        new Word('a'),
10        new Word('w', 'h', 'i', 'p'),
11        new Word('t', 'h', 'e', 'r', 'e'),
12        new Word('i', 's'),
13        new Word('a'),
14        new Word('w', 'a', 'y')
15    );
16
17    return new Sentence(words);
18
19  }
20
21  LetterComposite messageFromElves() {
22
23    var words = List.of(
24        new Word('M', 'u', 'c', 'h'),
25        new Word('w', 'i', 'n', 'd'),
26        new Word('p', 'o', 'u', 'r', 's'),
27        new Word('f', 'r', 'o', 'm'),
28        new Word('y', 'o', 'u', 'r'),
29        new Word('m', 'o', 'u', 't', 'h')
30    );
31
32    return new Sentence(words);
33
34  }
35
36}

And then it can be used as:

1var messenger = new Messenger();
2
3LOGGER.info("Message from the orcs: ");
4messenger.messageFromOrcs().print();
5
6LOGGER.info("Message from the elves: ");
7messenger.messageFromElves().print();

The console output:

Message from the orcs: 
 Where there is a whip there is a way.
Message from the elves: 
 Much wind pours from your mouth.

Class diagram

alt text

Applicability

Use the Composite pattern when

  • You want to represent part-whole hierarchies of objects.
  • You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.

Known uses

Credits