File tree Expand file tree Collapse file tree 4 files changed +15
-0
lines changed
decorator/src/main/java/com/iluwatar/decorator Expand file tree Collapse file tree 4 files changed +15
-0
lines changed Original file line number Diff line number Diff 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 ("\n A 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}
Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ public interface Hostile {
99
1010 void attack ();
1111
12+ int getAttackPower ();
13+
1214 void fleeBattle ();
1315
1416}
Original file line number Diff line number Diff 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!" );
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments