Merge pull request #340 from tomroy/master
Decorator pattern: SmartTroll should be SmartHostile #264
This commit is contained in:
commit
03e187743c
Binary file not shown.
Before Width: | Height: | Size: 7.9 KiB After Width: | Height: | Size: 29 KiB |
@ -1,18 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||
<interface id="1" language="java" name="com.iluwatar.decorator.Hostile" project="decorator"
|
||||
file="/decorator/src/main/java/com/iluwatar/decorator/Hostile.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="134" x="116" y="408"/>
|
||||
<position height="-1" width="-1" x="554" y="188"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="2" language="java" name="com.iluwatar.decorator.SmartTroll" project="decorator"
|
||||
file="/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="124" width="134" x="116" y="244"/>
|
||||
<class id="2" language="java" name="com.iluwatar.decorator.App" project="decorator"
|
||||
file="/decorator/src/main/java/com/iluwatar/decorator/App.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="117" y="202"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -21,29 +21,46 @@
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.decorator.Troll" project="decorator"
|
||||
file="/decorator/src/main/java/com/iluwatar/decorator/Troll.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="124" width="134" x="290" y="244"/>
|
||||
<position height="-1" width="-1" x="315" y="100"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<realization id="4">
|
||||
<class id="4" language="java" name="com.iluwatar.decorator.SmartHostile" project="decorator"
|
||||
file="/decorator/src/main/java/com/iluwatar/decorator/SmartHostile.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="318" y="315"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<dependency id="5">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</dependency>
|
||||
<realization id="6">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</realization>
|
||||
<association id="5">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="6" name="decorated"/>
|
||||
<multiplicity id="7" minimum="0" maximum="1"/>
|
||||
<association id="7">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="8" name="decorated"/>
|
||||
<multiplicity id="9" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="8">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<realization id="10">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</realization>
|
||||
<dependency id="11">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</dependency>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 24 KiB |
@ -8,7 +8,7 @@ package com.iluwatar.decorator;
|
||||
* runtime.
|
||||
* <p>
|
||||
* In this example we show how the simple {@link Troll} first attacks and then flees the battle.
|
||||
* Then we decorate the {@link Troll} with a {@link SmartTroll} and perform the attack again. You
|
||||
* Then we decorate the {@link Troll} with a {@link SmartHostile} and perform the attack again. You
|
||||
* can see how the behavior changes after the decoration.
|
||||
*
|
||||
*/
|
||||
@ -30,7 +30,7 @@ public class App {
|
||||
|
||||
// change the behavior of the simple troll by adding a decorator
|
||||
System.out.println("\nA smart looking troll surprises you.");
|
||||
Hostile smart = new SmartTroll(troll);
|
||||
Hostile smart = new SmartHostile(troll);
|
||||
smart.attack();
|
||||
smart.fleeBattle();
|
||||
System.out.printf("Smart troll power %d.\n", smart.getAttackPower());
|
||||
|
@ -1,34 +1,34 @@
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
* SmartTroll is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
|
||||
* SmartHostile is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
|
||||
* are intercepted and decorated. Finally the calls are delegated to the decorated {@link Hostile}
|
||||
* object.
|
||||
*
|
||||
*/
|
||||
public class SmartTroll implements Hostile {
|
||||
public class SmartHostile implements Hostile {
|
||||
|
||||
private Hostile decorated;
|
||||
|
||||
public SmartTroll(Hostile decorated) {
|
||||
public SmartHostile(Hostile decorated) {
|
||||
this.decorated = decorated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("The troll throws a rock at you!");
|
||||
System.out.println("It throws a rock at you!");
|
||||
decorated.attack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAttackPower() {
|
||||
// decorated troll power + 20 because it is smart
|
||||
// decorated hostile's power + 20 because it is smart
|
||||
return decorated.getAttackPower() + 20;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fleeBattle() {
|
||||
System.out.println("The troll calls for help!");
|
||||
System.out.println("It calls for help!");
|
||||
decorated.fleeBattle();
|
||||
}
|
||||
}
|
@ -11,15 +11,15 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class SmartTrollTest {
|
||||
public class SmartHostileTest {
|
||||
|
||||
@Test
|
||||
public void testSmartTroll() throws Exception {
|
||||
public void testSmartHostile() throws Exception {
|
||||
// Create a normal troll first, but make sure we can spy on it later on.
|
||||
final Hostile simpleTroll = spy(new Troll());
|
||||
|
||||
// Now we want to decorate the troll to make it smarter ...
|
||||
final Hostile smartTroll = new SmartTroll(simpleTroll);
|
||||
final Hostile smartTroll = new SmartHostile(simpleTroll);
|
||||
assertEquals(30, smartTroll.getAttackPower());
|
||||
verify(simpleTroll, times(1)).getAttackPower();
|
||||
|
Loading…
x
Reference in New Issue
Block a user