Skip to content

Commit 57783aa

Browse files
committed
Minor updates to decorator pattern
1 parent abc8e0d commit 57783aa

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

decorator/README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ permalink: /patterns/decorator/
66
categories: Structural
77
language: en
88
tags:
9-
- Gang Of Four
9+
- Gang of Four
1010
- Extensibility
1111
---
1212

@@ -21,9 +21,9 @@ alternative to subclassing for extending functionality.
2121

2222
## Explanation
2323

24-
Real world example
24+
Real-world example
2525

26-
> There is an angry troll living in the nearby hills. Usually it goes bare handed but sometimes it
26+
> There is an angry troll living in the nearby hills. Usually, it goes bare-handed but sometimes it
2727
> has a weapon. To arm the troll it's not necessary to create a new troll but to decorate it
2828
> dynamically with a suitable weapon.
2929
@@ -72,7 +72,7 @@ public class SimpleTroll implements Troll {
7272
}
7373
```
7474

75-
Next we want to add club for the troll. We can do it dynamically by using a decorator:
75+
Next, we want to add a club for the troll. We can do it dynamically by using a decorator:
7676

7777
```java
7878
@Slf4j
@@ -106,23 +106,33 @@ Here's the troll in action:
106106

107107
```java
108108
// simple troll
109+
LOGGER.info("A simple looking troll approaches.");
109110
var troll = new SimpleTroll();
110-
troll.attack(); // The troll tries to grab you!
111-
troll.fleeBattle(); // The troll shrieks in horror and runs away!
111+
troll.attack();
112+
troll.fleeBattle();
113+
LOGGER.info("Simple troll power: {}.\n", troll.getAttackPower());
112114

113115
// change the behavior of the simple troll by adding a decorator
116+
LOGGER.info("A troll with huge club surprises you.");
114117
var clubbedTroll = new ClubbedTroll(troll);
115-
clubbedTroll.attack(); // The troll tries to grab you! The troll swings at you with a club!
116-
clubbedTroll.fleeBattle(); // The troll shrieks in horror and runs away!
118+
clubbedTroll.attack();
119+
clubbedTroll.fleeBattle();
120+
LOGGER.info("Clubbed troll power: {}.\n", clubbedTroll.getAttackPower());
117121
```
118122

119123
Program output:
120124

121125
```java
126+
A simple looking troll approaches.
122127
The troll tries to grab you!
123128
The troll shrieks in horror and runs away!
124-
The troll tries to grab you! The troll swings at you with a club!
129+
Simple troll power: 10.
130+
131+
A troll with huge club surprises you.
132+
The troll tries to grab you!
133+
The troll swings at you with a club!
125134
The troll shrieks in horror and runs away!
135+
Clubbed troll power: 20.
126136
```
127137

128138
## Class diagram
@@ -140,11 +150,11 @@ affecting other objects.
140150
are possible and would produce an explosion of subclasses to support every combination. Or a class
141151
definition may be hidden or otherwise unavailable for subclassing.
142152

143-
## Tutorial
153+
## Tutorials
144154

145155
* [Decorator Pattern Tutorial](https://www.journaldev.com/1540/decorator-design-pattern-in-java-example)
146156

147-
## Real world examples
157+
## Known uses
148158

149159
* [java.io.InputStream](http://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html), [java.io.OutputStream](http://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html),
150160
[java.io.Reader](http://docs.oracle.com/javase/8/docs/api/java/io/Reader.html) and [java.io.Writer](http://docs.oracle.com/javase/8/docs/api/java/io/Writer.html)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public static void main(String[] args) {
5050
var troll = new SimpleTroll();
5151
troll.attack();
5252
troll.fleeBattle();
53-
LOGGER.info("Simple troll power {}.\n", troll.getAttackPower());
53+
LOGGER.info("Simple troll power: {}.\n", troll.getAttackPower());
5454

5555
// change the behavior of the simple troll by adding a decorator
5656
LOGGER.info("A troll with huge club surprises you.");
5757
var clubbedTroll = new ClubbedTroll(troll);
5858
clubbedTroll.attack();
5959
clubbedTroll.fleeBattle();
60-
LOGGER.info("Clubbed troll power {}.\n", clubbedTroll.getAttackPower());
60+
LOGGER.info("Clubbed troll power: {}.\n", clubbedTroll.getAttackPower());
6161
}
6262
}

0 commit comments

Comments
 (0)