Improvements to the composite example

This commit is contained in:
Ilkka Seppälä 2021-06-13 09:39:35 +03:00
parent ed98e7a128
commit 26c06d8f7f
No known key found for this signature in database
GPG Key ID: 31B7C8F5CC412ECB
3 changed files with 28 additions and 17 deletions

View File

@ -16,15 +16,15 @@ treat individual objects and compositions of objects uniformly.
## Explanation ## Explanation
Real world example Real-world example
> Every sentence is composed of words which are in turn composed of characters. Each of these > 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 > objects are printable and they can have something printed before or after them like sentence
> ends with full stop and word always has space before it. > always ends with full stop and word always has space before it.
In plain words 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 Wikipedia says
@ -154,10 +154,22 @@ public class Messenger {
And then it can be used as: And then it can be used as:
```java ```java
var orcMessage = new Messenger().messageFromOrcs(); var messenger = new Messenger();
orcMessage.print(); // Where there is a whip there is a way.
var elfMessage = new Messenger().messageFromElves(); LOGGER.info("Message from the orcs: ");
elfMessage.print(); // Much wind pours from your mouth. 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 ## Class diagram
@ -172,7 +184,7 @@ Use the Composite pattern when
* You want clients to be able to ignore the difference between compositions of objects and * 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. 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) * [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) * [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)

View File

@ -45,14 +45,13 @@ public class App {
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
var messenger = new Messenger();
LOGGER.info("Message from the orcs: "); LOGGER.info("Message from the orcs: ");
messenger.messageFromOrcs().print();
var orcMessage = new Messenger().messageFromOrcs(); LOGGER.info("Message from the elves: ");
orcMessage.print(); messenger.messageFromElves().print();
LOGGER.info("\nMessage from the elves: ");
var elfMessage = new Messenger().messageFromElves();
elfMessage.print();
} }
} }

View File

@ -39,6 +39,6 @@ public class Sentence extends LetterComposite {
@Override @Override
protected void printThisAfter() { protected void printThisAfter() {
System.out.print("."); System.out.print(".\n");
} }
} }