Skip to content

Commit

Permalink
iluwatar#590 Add explanation for Composite pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Aug 31, 2017
1 parent 7f1fac0 commit f28ed7b
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 139 deletions.
114 changes: 113 additions & 1 deletion composite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,119 @@ Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and compositions
of objects uniformly.

![alt text](./etc/composite_1.png "Composite")
## Explanation

Real world example

> Every sentence is composed of words which are in turn composed of characters. Each of these objects is 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 treat the individual objects in a uniform manner.
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 and different printable types

```
public abstract class LetterComposite {
private List<LetterComposite> children = new ArrayList<>();
public void add(LetterComposite letter) {
children.add(letter);
}
public int count() {
return children.size();
}
protected void printThisBefore() {}
protected void printThisAfter() {}
public void print() {
printThisBefore();
for (LetterComposite letter : children) {
letter.print();
}
printThisAfter();
}
}
public class Letter extends LetterComposite {
private char c;
public Letter(char c) {
this.c = c;
}
@Override
protected void printThisBefore() {
System.out.print(c);
}
}
public class Word extends LetterComposite {
public Word(List<Letter> letters) {
for (Letter l : letters) {
this.add(l);
}
}
@Override
protected void printThisBefore() {
System.out.print(" ");
}
}
public class Sentence extends LetterComposite {
public Sentence(List<Word> words) {
for (Word w : words) {
this.add(w);
}
}
@Override
protected void printThisAfter() {
System.out.print(".");
}
}
```

Then we have a messenger to carry messages

```
public class Messenger {
LetterComposite messageFromOrcs() {
List<Word> words = new ArrayList<>();
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
words.add(new Word(Arrays.asList(new Letter('a'))));
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'), new Letter('i'), new Letter('p'))));
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
words.add(new Word(Arrays.asList(new Letter('a'))));
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'), new Letter('y'))));
return new Sentence(words);
}
LetterComposite messageFromElves() {
List<Word> words = new ArrayList<>();
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), new Letter('c'), new Letter('h'))));
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), new Letter('n'), new Letter('d'))));
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), new Letter('u'), new Letter('r'), new Letter('s'))));
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), new Letter('o'), new Letter('m'))));
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), new Letter('u'), new Letter('r'))));
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), new Letter('u'), new Letter('t'), new Letter('h'))));
return new Sentence(words);
}
}
```

And then it can be used as

```
LetterComposite orcMessage = new Messenger().messageFromOrcs();
orcMessage.print(); // Where there is a whip there is a way.
LetterComposite elfMessage = new Messenger().messageFromElves();
elfMessage.print(); // Much wind pours from your mouth.
```

## Applicability
Use the Composite pattern when
Expand Down
Binary file removed composite/etc/composite.png
Binary file not shown.
75 changes: 0 additions & 75 deletions composite/etc/composite.ucls

This file was deleted.

43 changes: 0 additions & 43 deletions composite/etc/composite.urm.puml

This file was deleted.

Binary file removed composite/etc/composite_1.png
Binary file not shown.
4 changes: 1 addition & 3 deletions composite/src/main/java/com/iluwatar/composite/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public static void main(String[] args) {
LetterComposite orcMessage = new Messenger().messageFromOrcs();
orcMessage.print();

LOGGER.info("\n");

LOGGER.info("Message from the elves: ");
LOGGER.info("\nMessage from the elves: ");

LetterComposite elfMessage = new Messenger().messageFromElves();
elfMessage.print();
Expand Down
5 changes: 0 additions & 5 deletions composite/src/main/java/com/iluwatar/composite/Letter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,4 @@ public Letter(char c) {
protected void printThisBefore() {
System.out.print(c);
}

@Override
protected void printThisAfter() {
// nop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public int count() {
return children.size();
}

protected abstract void printThisBefore();
protected void printThisBefore() {}

protected abstract void printThisAfter();
protected void printThisAfter() {}

/**
* Print
Expand Down
5 changes: 0 additions & 5 deletions composite/src/main/java/com/iluwatar/composite/Sentence.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public Sentence(List<Word> words) {
}
}

@Override
protected void printThisBefore() {
// nop
}

@Override
protected void printThisAfter() {
System.out.print(".");
Expand Down
5 changes: 0 additions & 5 deletions composite/src/main/java/com/iluwatar/composite/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,4 @@ public Word(List<Letter> letters) {
protected void printThisBefore() {
System.out.print(" ");
}

@Override
protected void printThisAfter() {
// nop
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
<param>prototype</param>
<param>adapter</param>
<param>bridge</param>
<param>composite</param>
</skipForProjects>
</configuration>
</plugin>
Expand Down

0 comments on commit f28ed7b

Please sign in to comment.