Merge pull request #253 from mgalushka/master
#247 adding getAttackPower method to pattern decorator
This commit is contained in:
commit
6735c81b52
@ -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!");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user