You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: composite/README.md
+113-1
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,119 @@ Compose objects into tree structures to represent part-whole
16
16
hierarchies. Composite lets clients treat individual objects and compositions
17
17
of objects uniformly.
18
18
19
-

19
+
## Explanation
20
+
21
+
Real world example
22
+
23
+
> 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
24
+
25
+
In plain words
26
+
27
+
> Composite pattern lets clients treat the individual objects in a uniform manner.
28
+
29
+
Wikipedia says
30
+
31
+
> 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.
32
+
33
+
**Programmatic Example**
34
+
35
+
Taking our sentence example from above. Here we have the base class and different printable types
36
+
37
+
```
38
+
public abstract class LetterComposite {
39
+
private List<LetterComposite> children = new ArrayList<>();
40
+
public void add(LetterComposite letter) {
41
+
children.add(letter);
42
+
}
43
+
public int count() {
44
+
return children.size();
45
+
}
46
+
protected void printThisBefore() {}
47
+
protected void printThisAfter() {}
48
+
public void print() {
49
+
printThisBefore();
50
+
for (LetterComposite letter : children) {
51
+
letter.print();
52
+
}
53
+
printThisAfter();
54
+
}
55
+
}
56
+
57
+
public class Letter extends LetterComposite {
58
+
private char c;
59
+
public Letter(char c) {
60
+
this.c = c;
61
+
}
62
+
@Override
63
+
protected void printThisBefore() {
64
+
System.out.print(c);
65
+
}
66
+
}
67
+
68
+
public class Word extends LetterComposite {
69
+
public Word(List<Letter> letters) {
70
+
for (Letter l : letters) {
71
+
this.add(l);
72
+
}
73
+
}
74
+
@Override
75
+
protected void printThisBefore() {
76
+
System.out.print(" ");
77
+
}
78
+
}
79
+
80
+
public class Sentence extends LetterComposite {
81
+
public Sentence(List<Word> words) {
82
+
for (Word w : words) {
83
+
this.add(w);
84
+
}
85
+
}
86
+
@Override
87
+
protected void printThisAfter() {
88
+
System.out.print(".");
89
+
}
90
+
}
91
+
```
92
+
93
+
Then we have a messenger to carry messages
94
+
95
+
```
96
+
public class Messenger {
97
+
LetterComposite messageFromOrcs() {
98
+
List<Word> words = new ArrayList<>();
99
+
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
100
+
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))));
101
+
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
0 commit comments