X Tutup
Skip to content

Commit 6735c81

Browse files
committed
Merge pull request iluwatar#253 from mgalushka/master
iluwatar#247 adding getAttackPower method to pattern decorator
2 parents b3e4e8a + ec9af41 commit 6735c81

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ public static void main(String[] args) {
2323
Hostile troll = new Troll();
2424
troll.attack();
2525
troll.fleeBattle();
26+
System.out.printf("Simple troll power %d.\n", troll.getAttackPower());
2627

2728
// change the behavior of the simple troll by adding a decorator
2829
System.out.println("\nA smart looking troll surprises you.");
2930
Hostile smart = new SmartTroll(troll);
3031
smart.attack();
3132
smart.fleeBattle();
33+
System.out.printf("Smart troll power %d.\n", smart.getAttackPower());
3234
}
3335
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public interface Hostile {
99

1010
void attack();
1111

12+
int getAttackPower();
13+
1214
void fleeBattle();
1315

1416
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public void attack() {
2121
decorated.attack();
2222
}
2323

24+
@Override
25+
public int getAttackPower() {
26+
// decorated troll power + 20 because it is smart
27+
return decorated.getAttackPower() + 20;
28+
}
29+
2430
@Override
2531
public void fleeBattle() {
2632
System.out.println("The troll calls for help!");

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public void attack() {
1111
System.out.println("The troll swings at you with a club!");
1212
}
1313

14+
@Override
15+
public int getAttackPower() {
16+
return 10;
17+
}
18+
1419
public void fleeBattle() {
1520
System.out.println("The troll shrieks in horror and runs away!");
1621
}

0 commit comments

Comments
 (0)
X Tutup