提交 ec9af416 编写于 作者: M Maxim Galushka

#247 adding getAttackPower method to pattern decorator

上级 b3e4e8a4
......@@ -23,11 +23,13 @@ public class App {
Hostile troll = new Troll();
troll.attack();
troll.fleeBattle();
System.out.printf("Simple troll power %d.\n", troll.getAttackPower());
// 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);
smart.attack();
smart.fleeBattle();
System.out.printf("Smart troll power %d.\n", smart.getAttackPower());
}
}
......@@ -9,6 +9,8 @@ public interface Hostile {
void attack();
int getAttackPower();
void fleeBattle();
}
......@@ -21,6 +21,12 @@ public class SmartTroll implements Hostile {
decorated.attack();
}
@Override
public int getAttackPower() {
// decorated troll power + 20 because it is smart
return decorated.getAttackPower() + 20;
}
@Override
public void fleeBattle() {
System.out.println("The troll calls for help!");
......
......@@ -11,6 +11,11 @@ public class Troll implements Hostile {
System.out.println("The troll swings at you with a club!");
}
@Override
public int getAttackPower() {
return 10;
}
public void fleeBattle() {
System.out.println("The troll shrieks in horror and runs away!");
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册