@@ -6,7 +6,7 @@ permalink: /patterns/decorator/
66categories : Structural
77language : en
88tags :
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." );
109110var 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." );
114117var 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
119123Program output:
120124
121125``` java
126+ A simple looking troll approaches.
122127The troll tries to grab you!
123128The 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!
125134The troll shrieks in horror and runs away!
135+ Clubbed troll power: 20.
126136```
127137
128138## Class diagram
@@ -140,11 +150,11 @@ affecting other objects.
140150are possible and would produce an explosion of subclasses to support every combination. Or a class
141151definition 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 )
0 commit comments