Skip to content

Commit 92f8501

Browse files
committed
Decorator pattern: Improve the example
1 parent 68ec24c commit 92f8501

File tree

10 files changed

+207
-166
lines changed

10 files changed

+207
-166
lines changed

decorator/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ functionality.
2424
## Applicability
2525
Use Decorator
2626

27-
* to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects
28-
* for responsibilities that can be withdrawn
29-
* when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing
27+
* To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects
28+
* For responsibilities that can be withdrawn
29+
* When extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing
3030

3131
## Real world examples
3232
* [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),

decorator/etc/decorator.png

-7.96 KB
Loading

decorator/etc/decorator.ucls

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,72 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3-
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4-
<interface id="1" language="java" name="com.iluwatar.decorator.Hostile" project="decorator"
5-
file="/decorator/src/main/java/com/iluwatar/decorator/Hostile.java" binary="false" corner="BOTTOM_RIGHT">
6-
<position height="-1" width="-1" x="554" y="188"/>
2+
<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
3+
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
4+
router="FAN">
5+
<class id="1" language="java" name="com.iluwatar.decorator.App" project="decorator"
6+
file="/decorator/src/main/java/com/iluwatar/decorator/App.java" binary="false" corner="BOTTOM_RIGHT">
7+
<position height="113" width="114" x="177" y="237"/>
78
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
89
sort-features="false" accessors="true" visibility="true">
910
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1011
<operations public="true" package="true" protected="true" private="true" static="true"/>
1112
</display>
12-
</interface>
13-
<class id="2" language="java" name="com.iluwatar.decorator.App" project="decorator"
14-
file="/decorator/src/main/java/com/iluwatar/decorator/App.java" binary="false" corner="BOTTOM_RIGHT">
15-
<position height="-1" width="-1" x="117" y="202"/>
13+
</class>
14+
<class id="2" language="java" name="com.iluwatar.decorator.SimpleTroll" project="decorator"
15+
file="/decorator/src/main/java/com/iluwatar/decorator/SimpleTroll.java" binary="false" corner="BOTTOM_RIGHT">
16+
<position height="149" width="125" x="496" y="237"/>
1617
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
1718
sort-features="false" accessors="true" visibility="true">
1819
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1920
<operations public="true" package="true" protected="true" private="true" static="true"/>
2021
</display>
2122
</class>
22-
<class id="3" language="java" name="com.iluwatar.decorator.Troll" project="decorator"
23-
file="/decorator/src/main/java/com/iluwatar/decorator/Troll.java" binary="false" corner="BOTTOM_RIGHT">
24-
<position height="-1" width="-1" x="315" y="100"/>
23+
<class id="3" language="java" name="com.iluwatar.decorator.ClubbedTroll" project="decorator"
24+
file="/decorator/src/main/java/com/iluwatar/decorator/ClubbedTroll.java" binary="false" corner="BOTTOM_RIGHT">
25+
<position height="149" width="125" x="249" y="426"/>
2526
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2627
sort-features="false" accessors="true" visibility="true">
2728
<attributes public="true" package="true" protected="true" private="true" static="true"/>
2829
<operations public="true" package="true" protected="true" private="true" static="true"/>
2930
</display>
3031
</class>
31-
<class id="4" language="java" name="com.iluwatar.decorator.SmartHostile" project="decorator"
32-
file="/decorator/src/main/java/com/iluwatar/decorator/SmartHostile.java" binary="false" corner="BOTTOM_RIGHT">
33-
<position height="-1" width="-1" x="318" y="315"/>
32+
<class id="4" language="java" name="com.iluwatar.decorator.TrollDecorator" project="decorator"
33+
file="/decorator/src/main/java/com/iluwatar/decorator/TrollDecorator.java" binary="false" corner="BOTTOM_RIGHT">
34+
<position height="149" width="125" x="331" y="237"/>
3435
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
3536
sort-features="false" accessors="true" visibility="true">
3637
<attributes public="true" package="true" protected="true" private="true" static="true"/>
3738
<operations public="true" package="true" protected="true" private="true" static="true"/>
3839
</display>
3940
</class>
40-
<dependency id="5">
41-
<end type="SOURCE" refId="2"/>
42-
<end type="TARGET" refId="4"/>
43-
</dependency>
41+
<interface id="5" language="java" name="com.iluwatar.decorator.Troll" project="decorator"
42+
file="/decorator/src/main/java/com/iluwatar/decorator/Troll.java" binary="false" corner="BOTTOM_RIGHT">
43+
<position height="113" width="125" x="414" y="426"/>
44+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
45+
sort-features="false" accessors="true" visibility="true">
46+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
47+
<operations public="true" package="true" protected="true" private="true" static="true"/>
48+
</display>
49+
</interface>
4450
<realization id="6">
51+
<end type="SOURCE" refId="4"/>
52+
<end type="TARGET" refId="5"/>
53+
</realization>
54+
<generalization id="7">
4555
<end type="SOURCE" refId="3"/>
46-
<end type="TARGET" refId="1"/>
56+
<end type="TARGET" refId="4"/>
57+
</generalization>
58+
<realization id="8">
59+
<end type="SOURCE" refId="2"/>
60+
<end type="TARGET" refId="5"/>
4761
</realization>
48-
<association id="7">
62+
<association id="9">
4963
<end type="SOURCE" refId="4" navigable="false">
50-
<attribute id="8" name="decorated"/>
51-
<multiplicity id="9" minimum="0" maximum="1"/>
64+
<attribute id="10" name="decorated"/>
65+
<multiplicity id="11" minimum="0" maximum="1"/>
5266
</end>
53-
<end type="TARGET" refId="1" navigable="true"/>
67+
<end type="TARGET" refId="5" navigable="true"/>
5468
<display labels="true" multiplicity="true"/>
5569
</association>
56-
<realization id="10">
57-
<end type="SOURCE" refId="4"/>
58-
<end type="TARGET" refId="1"/>
59-
</realization>
60-
<dependency id="11">
61-
<end type="SOURCE" refId="2"/>
62-
<end type="TARGET" refId="3"/>
63-
</dependency>
6470
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
6571
sort-features="false" accessors="true" visibility="true">
6672
<attributes public="true" package="true" protected="true" private="true" static="true"/>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
* target. Using the Decorator pattern it is possible to change the behavior of the class during
3333
* runtime.
3434
* <p>
35-
* In this example we show how the simple {@link Troll} first attacks and then flees the battle.
36-
* Then we decorate the {@link Troll} with a {@link SmartHostile} and perform the attack again. You
35+
* In this example we show how the simple {@link SimpleTroll} first attacks and then flees the battle.
36+
* Then we decorate the {@link SimpleTroll} with a {@link ClubbedTroll} and perform the attack again. You
3737
* can see how the behavior changes after the decoration.
3838
*
3939
*/
@@ -50,16 +50,16 @@ public static void main(String[] args) {
5050

5151
// simple troll
5252
LOGGER.info("A simple looking troll approaches.");
53-
Hostile troll = new Troll();
53+
Troll troll = new SimpleTroll();
5454
troll.attack();
5555
troll.fleeBattle();
5656
LOGGER.info("Simple troll power {}.\n", troll.getAttackPower());
5757

5858
// change the behavior of the simple troll by adding a decorator
59-
LOGGER.info("A smart looking troll surprises you.");
60-
Hostile smart = new SmartHostile(troll);
61-
smart.attack();
62-
smart.fleeBattle();
63-
LOGGER.info("Smart troll power {}.\n", smart.getAttackPower());
59+
LOGGER.info("A troll with huge club surprises you.");
60+
Troll clubbed = new ClubbedTroll(troll);
61+
clubbed.attack();
62+
clubbed.fleeBattle();
63+
LOGGER.info("Clubbed troll power {}.\n", clubbed.getAttackPower());
6464
}
6565
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.decorator;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
/**
29+
* Decorator that adds a club for the troll
30+
*/
31+
public class ClubbedTroll extends TrollDecorator {
32+
33+
private static final Logger LOGGER = LoggerFactory.getLogger(ClubbedTroll.class);
34+
35+
public ClubbedTroll(Troll decorated) {
36+
super(decorated);
37+
}
38+
39+
@Override
40+
public void attack() {
41+
super.attack();
42+
LOGGER.info("The troll swings at you with a club!");
43+
}
44+
45+
@Override
46+
public int getAttackPower() {
47+
return super.getAttackPower() + 10;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1-
/**
2-
* The MIT License
3-
* Copyright (c) 2014 Ilkka Seppälä
4-
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
11-
*
12-
* The above copyright notice and this permission notice shall be included in
13-
* all copies or substantial portions of the Software.
14-
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
* THE SOFTWARE.
22-
*/
23-
package com.iluwatar.decorator;
24-
25-
/**
26-
*
27-
* Interface for the hostile enemies.
28-
*
29-
*/
30-
public interface Hostile {
31-
32-
void attack();
33-
34-
int getAttackPower();
35-
36-
void fleeBattle();
37-
38-
}
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.decorator;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
/**
29+
*
30+
* SimpleTroll implements {@link Troll} interface directly.
31+
*
32+
*/
33+
public class SimpleTroll implements Troll {
34+
35+
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTroll.class);
36+
37+
@Override
38+
public void attack() {
39+
LOGGER.info("The troll tries to grab you!");
40+
}
41+
42+
@Override
43+
public int getAttackPower() {
44+
return 10;
45+
}
46+
47+
@Override
48+
public void fleeBattle() {
49+
LOGGER.info("The troll shrieks in horror and runs away!");
50+
}
51+
}
Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,38 @@
1-
/**
2-
* The MIT License
3-
* Copyright (c) 2014 Ilkka Seppälä
4-
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
11-
*
12-
* The above copyright notice and this permission notice shall be included in
13-
* all copies or substantial portions of the Software.
14-
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
* THE SOFTWARE.
22-
*/
23-
package com.iluwatar.decorator;
24-
25-
import org.slf4j.Logger;
26-
import org.slf4j.LoggerFactory;
27-
28-
/**
29-
*
30-
* Troll implements {@link Hostile} interface directly.
31-
*
32-
*/
33-
public class Troll implements Hostile {
34-
35-
private static final Logger LOGGER = LoggerFactory.getLogger(Troll.class);
36-
37-
@Override
38-
public void attack() {
39-
LOGGER.info("The troll swings at you with a club!");
40-
}
41-
42-
@Override
43-
public int getAttackPower() {
44-
return 10;
45-
}
46-
47-
@Override
48-
public void fleeBattle() {
49-
LOGGER.info("The troll shrieks in horror and runs away!");
50-
}
51-
}
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.decorator;
24+
25+
/**
26+
*
27+
* Interface for trolls
28+
*
29+
*/
30+
public interface Troll {
31+
32+
void attack();
33+
34+
int getAttackPower();
35+
36+
void fleeBattle();
37+
38+
}

0 commit comments

Comments
 (0)