Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improvements to the composite example
  • Loading branch information
iluwatar committed Jun 13, 2021
commit 26c06d8f7f70bf73860abf11fe82892206a9ffa9
30 changes: 21 additions & 9 deletions composite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ treat individual objects and compositions of objects uniformly.

## Explanation

Real world example
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.
> 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 treat the individual objects in a uniform manner.
> Composite pattern lets clients uniformly treat the individual objects.

Wikipedia says

Expand Down Expand Up @@ -154,10 +154,22 @@ public class Messenger {
And then it can be used as:

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

LOGGER.info("Message from the orcs: ");
messenger.messageFromOrcs().print();

LOGGER.info("Message from the elves: ");
messenger.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
Expand All @@ -172,7 +184,7 @@ Use the Composite pattern when
* 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.

## Real world examples
## Known uses

* [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)
* [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)
Expand Down
11 changes: 5 additions & 6 deletions composite/src/main/java/com/iluwatar/composite/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
LOGGER.info("Message from the orcs: ");

var orcMessage = new Messenger().messageFromOrcs();
orcMessage.print();
var messenger = new Messenger();

LOGGER.info("\nMessage from the elves: ");
LOGGER.info("Message from the orcs: ");
messenger.messageFromOrcs().print();

var elfMessage = new Messenger().messageFromElves();
elfMessage.print();
LOGGER.info("Message from the elves: ");
messenger.messageFromElves().print();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public Sentence(List<Word> words) {

@Override
protected void printThisAfter() {
System.out.print(".");
System.out.print(".\n");
}
}