Skip to content

Commit 26c06d8

Browse files
committed
Improvements to the composite example
1 parent ed98e7a commit 26c06d8

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

composite/README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ treat individual objects and compositions of objects uniformly.
1616

1717
## Explanation
1818

19-
Real world example
19+
Real-world example
2020

2121
> Every sentence is composed of words which are in turn composed of characters. Each of these
22-
> objects is printable and they can have something printed before or after them like sentence always
23-
> ends with full stop and word always has space before it.
22+
> objects are printable and they can have something printed before or after them like sentence
23+
> always ends with full stop and word always has space before it.
2424
2525
In plain words
2626

27-
> Composite pattern lets clients treat the individual objects in a uniform manner.
27+
> Composite pattern lets clients uniformly treat the individual objects.
2828
2929
Wikipedia says
3030

@@ -154,10 +154,22 @@ public class Messenger {
154154
And then it can be used as:
155155

156156
```java
157-
var orcMessage = new Messenger().messageFromOrcs();
158-
orcMessage.print(); // Where there is a whip there is a way.
159-
var elfMessage = new Messenger().messageFromElves();
160-
elfMessage.print(); // Much wind pours from your mouth.
157+
var messenger = new Messenger();
158+
159+
LOGGER.info("Message from the orcs: ");
160+
messenger.messageFromOrcs().print();
161+
162+
LOGGER.info("Message from the elves: ");
163+
messenger.messageFromElves().print();
164+
```
165+
166+
The console output:
167+
168+
```
169+
Message from the orcs:
170+
Where there is a whip there is a way.
171+
Message from the elves:
172+
Much wind pours from your mouth.
161173
```
162174

163175
## Class diagram
@@ -172,7 +184,7 @@ Use the Composite pattern when
172184
* You want clients to be able to ignore the difference between compositions of objects and
173185
individual objects. Clients will treat all objects in the composite structure uniformly.
174186

175-
## Real world examples
187+
## Known uses
176188

177189
* [java.awt.Container](http://docs.oracle.com/javase/8/docs/api/java/awt/Container.html) and [java.awt.Component](http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html)
178190
* [Apache Wicket](https://github.com/apache/wicket) component tree, see [Component](https://github.com/apache/wicket/blob/91e154702ab1ff3481ef6cbb04c6044814b7e130/wicket-core/src/main/java/org/apache/wicket/Component.java) and [MarkupContainer](https://github.com/apache/wicket/blob/b60ec64d0b50a611a9549809c9ab216f0ffa3ae3/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java)

composite/src/main/java/com/iluwatar/composite/App.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ public class App {
4545
* @param args command line args
4646
*/
4747
public static void main(String[] args) {
48-
LOGGER.info("Message from the orcs: ");
4948

50-
var orcMessage = new Messenger().messageFromOrcs();
51-
orcMessage.print();
49+
var messenger = new Messenger();
5250

53-
LOGGER.info("\nMessage from the elves: ");
51+
LOGGER.info("Message from the orcs: ");
52+
messenger.messageFromOrcs().print();
5453

55-
var elfMessage = new Messenger().messageFromElves();
56-
elfMessage.print();
54+
LOGGER.info("Message from the elves: ");
55+
messenger.messageFromElves().print();
5756
}
5857
}

composite/src/main/java/com/iluwatar/composite/Sentence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public Sentence(List<Word> words) {
3939

4040
@Override
4141
protected void printThisAfter() {
42-
System.out.print(".");
42+
System.out.print(".\n");
4343
}
4444
}

0 commit comments

Comments
 (0)