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
+20-10
Original file line number
Diff line number
Diff line change
@@ -9,27 +9,34 @@ tags:
9
9
---
10
10
11
11
## Intent
12
-
Compose objects into tree structures to represent part-whole
13
-
hierarchies. Composite lets clients treat individual objects and compositions
14
-
of objects uniformly.
12
+
13
+
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients
14
+
treat individual objects and compositions of objects uniformly.
15
15
16
16
## Explanation
17
17
18
18
Real world example
19
19
20
-
> 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
20
+
> Every sentence is composed of words which are in turn composed of characters. Each of these
21
+
> objects is printable and they can have something printed before or after them like sentence always
22
+
> ends with full stop and word always has space before it.
21
23
22
24
In plain words
23
25
24
26
> Composite pattern lets clients treat the individual objects in a uniform manner.
25
27
26
28
Wikipedia says
27
29
28
-
> 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.
30
+
> In software engineering, the composite pattern is a partitioning design pattern. The composite
31
+
> pattern describes that a group of objects is to be treated in the same way as a single instance of
32
+
> an object. The intent of a composite is to "compose" objects into tree structures to represent
Taking our sentence example from above. Here we have the base class and different printable types
38
+
Taking our sentence example from above. Here we have the base class `LetterComposite` and the
39
+
different printable types `Letter`, `Word` and `Sentence`.
33
40
34
41
```java
35
42
publicabstractclassLetterComposite {
@@ -102,7 +109,7 @@ public class Sentence extends LetterComposite {
102
109
}
103
110
```
104
111
105
-
Then we have a messenger to carry messages
112
+
Then we have a messenger to carry messages:
106
113
107
114
```java
108
115
publicclassMessenger {
@@ -143,7 +150,7 @@ public class Messenger {
143
150
}
144
151
```
145
152
146
-
And then it can be used as
153
+
And then it can be used as:
147
154
148
155
```java
149
156
var orcMessage =newMessenger().messageFromOrcs();
@@ -153,13 +160,16 @@ elfMessage.print(); // Much wind pours from your mouth.
153
160
```
154
161
155
162
## Class diagram
163
+
156
164

157
165
158
166
## Applicability
167
+
159
168
Use the Composite pattern when
160
169
161
-
* you want to represent part-whole hierarchies of objects
162
-
* 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
170
+
* You want to represent part-whole hierarchies of objects.
171
+
* You want clients to be able to ignore the difference between compositions of objects and
172
+
individual objects. Clients will treat all objects in the composite structure uniformly.
0 commit comments