Skip to content

Commit 30ca1ea

Browse files
committed
Decorator pattern: SmartTroll should be SmartHostile iluwatar#264
1 parent 4c5f9ae commit 30ca1ea

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

decorator/etc/decorator.ucls

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3-
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
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">
44
<interface id="1" language="java" name="com.iluwatar.decorator.Hostile" project="decorator"
55
file="/decorator/src/main/java/com/iluwatar/decorator/Hostile.java" binary="false" corner="BOTTOM_RIGHT">
6-
<position height="106" width="134" x="116" y="408"/>
6+
<position height="-1" width="-1" x="554" y="188"/>
77
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
88
sort-features="false" accessors="true" visibility="true">
99
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1010
<operations public="true" package="true" protected="true" private="true" static="true"/>
1111
</display>
1212
</interface>
13-
<class id="2" language="java" name="com.iluwatar.decorator.SmartTroll" project="decorator"
14-
file="/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java" binary="false" corner="BOTTOM_RIGHT">
15-
<position height="124" width="134" x="116" y="244"/>
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"/>
1616
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
1717
sort-features="false" accessors="true" visibility="true">
1818
<attributes public="true" package="true" protected="true" private="true" static="true"/>
@@ -21,29 +21,46 @@
2121
</class>
2222
<class id="3" language="java" name="com.iluwatar.decorator.Troll" project="decorator"
2323
file="/decorator/src/main/java/com/iluwatar/decorator/Troll.java" binary="false" corner="BOTTOM_RIGHT">
24-
<position height="124" width="134" x="290" y="244"/>
24+
<position height="-1" width="-1" x="315" y="100"/>
2525
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2626
sort-features="false" accessors="true" visibility="true">
2727
<attributes public="true" package="true" protected="true" private="true" static="true"/>
2828
<operations public="true" package="true" protected="true" private="true" static="true"/>
2929
</display>
3030
</class>
31-
<realization id="4">
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"/>
34+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
35+
sort-features="false" accessors="true" visibility="true">
36+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
37+
<operations public="true" package="true" protected="true" private="true" static="true"/>
38+
</display>
39+
</class>
40+
<dependency id="5">
3241
<end type="SOURCE" refId="2"/>
42+
<end type="TARGET" refId="4"/>
43+
</dependency>
44+
<realization id="6">
45+
<end type="SOURCE" refId="3"/>
3346
<end type="TARGET" refId="1"/>
3447
</realization>
35-
<association id="5">
36-
<end type="SOURCE" refId="2" navigable="false">
37-
<attribute id="6" name="decorated"/>
38-
<multiplicity id="7" minimum="0" maximum="1"/>
48+
<association id="7">
49+
<end type="SOURCE" refId="4" navigable="false">
50+
<attribute id="8" name="decorated"/>
51+
<multiplicity id="9" minimum="0" maximum="1"/>
3952
</end>
4053
<end type="TARGET" refId="1" navigable="true"/>
4154
<display labels="true" multiplicity="true"/>
4255
</association>
43-
<realization id="8">
44-
<end type="SOURCE" refId="3"/>
56+
<realization id="10">
57+
<end type="SOURCE" refId="4"/>
4558
<end type="TARGET" refId="1"/>
4659
</realization>
60+
<dependency id="11">
61+
<end type="SOURCE" refId="2"/>
62+
<end type="TARGET" refId="3"/>
63+
</dependency>
4764
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
4865
sort-features="false" accessors="true" visibility="true">
4966
<attributes public="true" package="true" protected="true" private="true" static="true"/>

decorator/etc/decorator_1.png

5.57 KB
Loading

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* runtime.
99
* <p>
1010
* In this example we show how the simple {@link Troll} first attacks and then flees the battle.
11-
* Then we decorate the {@link Troll} with a {@link SmartTroll} and perform the attack again. You
11+
* Then we decorate the {@link Troll} with a {@link SmartHostile} and perform the attack again. You
1212
* can see how the behavior changes after the decoration.
1313
*
1414
*/
@@ -30,7 +30,7 @@ public static void main(String[] args) {
3030

3131
// change the behavior of the simple troll by adding a decorator
3232
System.out.println("\nA smart looking troll surprises you.");
33-
Hostile smart = new SmartTroll(troll);
33+
Hostile smart = new SmartHostile(troll);
3434
smart.attack();
3535
smart.fleeBattle();
3636
System.out.printf("Smart troll power %d.\n", smart.getAttackPower());

decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java renamed to decorator/src/main/java/com/iluwatar/decorator/SmartHostile.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
package com.iluwatar.decorator;
22

33
/**
4-
* SmartTroll is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
4+
* SmartHostile is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
55
* are intercepted and decorated. Finally the calls are delegated to the decorated {@link Hostile}
66
* object.
77
*
88
*/
9-
public class SmartTroll implements Hostile {
9+
public class SmartHostile implements Hostile {
1010

1111
private Hostile decorated;
1212

13-
public SmartTroll(Hostile decorated) {
13+
public SmartHostile(Hostile decorated) {
1414
this.decorated = decorated;
1515
}
1616

1717
@Override
1818
public void attack() {
19-
System.out.println("The troll throws a rock at you!");
19+
System.out.println("It throws a rock at you!");
2020
decorated.attack();
2121
}
2222

2323
@Override
2424
public int getAttackPower() {
25-
// decorated troll power + 20 because it is smart
25+
// decorated hostile's power + 20 because it is smart
2626
return decorated.getAttackPower() + 20;
2727
}
2828

2929
@Override
3030
public void fleeBattle() {
31-
System.out.println("The troll calls for help!");
31+
System.out.println("It calls for help!");
3232
decorated.fleeBattle();
3333
}
3434
}

decorator/src/test/java/com/iluwatar/decorator/SmartTrollTest.java renamed to decorator/src/test/java/com/iluwatar/decorator/SmartHostileTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
*
1212
* @author Jeroen Meulemeester
1313
*/
14-
public class SmartTrollTest {
14+
public class SmartHostileTest {
1515

1616
@Test
1717
public void testSmartTroll() throws Exception {
1818
// Create a normal troll first, but make sure we can spy on it later on.
1919
final Hostile simpleTroll = spy(new Troll());
2020

2121
// Now we want to decorate the troll to make it smarter ...
22-
final Hostile smartTroll = new SmartTroll(simpleTroll);
22+
final Hostile smartTroll = new SmartHostile(simpleTroll);
2323
assertEquals(30, smartTroll.getAttackPower());
2424
verify(simpleTroll, times(1)).getAttackPower();
2525

0 commit comments

Comments
 (0)