@@ -6,7 +6,7 @@ permalink: /patterns/decorator/
6
6
categories : Structural
7
7
language : en
8
8
tags :
9
- - Gang Of Four
9
+ - Gang of Four
10
10
- Extensibility
11
11
---
12
12
@@ -21,9 +21,9 @@ alternative to subclassing for extending functionality.
21
21
22
22
## Explanation
23
23
24
- Real world example
24
+ Real- world example
25
25
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
27
27
> has a weapon. To arm the troll it's not necessary to create a new troll but to decorate it
28
28
> dynamically with a suitable weapon.
29
29
@@ -72,7 +72,7 @@ public class SimpleTroll implements Troll {
72
72
}
73
73
```
74
74
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:
76
76
77
77
``` java
78
78
@Slf4j
@@ -106,23 +106,33 @@ Here's the troll in action:
106
106
107
107
``` java
108
108
// simple troll
109
+ LOGGER . info(" A simple looking troll approaches." );
109
110
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());
112
114
113
115
// change the behavior of the simple troll by adding a decorator
116
+ LOGGER . info(" A troll with huge club surprises you." );
114
117
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());
117
121
```
118
122
119
123
Program output:
120
124
121
125
``` java
126
+ A simple looking troll approaches.
122
127
The troll tries to grab you!
123
128
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!
125
134
The troll shrieks in horror and runs away!
135
+ Clubbed troll power: 20.
126
136
```
127
137
128
138
## Class diagram
@@ -140,11 +150,11 @@ affecting other objects.
140
150
are possible and would produce an explosion of subclasses to support every combination. Or a class
141
151
definition may be hidden or otherwise unavailable for subclassing.
142
152
143
- ## Tutorial
153
+ ## Tutorials
144
154
145
155
* [ Decorator Pattern Tutorial] ( https://www.journaldev.com/1540/decorator-design-pattern-in-java-example )
146
156
147
- ## Real world examples
157
+ ## Known uses
148
158
149
159
* [ 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 ) ,
150
160
[ 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 )
0 commit comments