#247 adding getAttackPower method to pattern decorator
This commit is contained in:
		| @@ -23,11 +23,13 @@ public class App { | |||||||
| 		Hostile troll = new Troll(); | 		Hostile troll = new Troll(); | ||||||
| 		troll.attack(); | 		troll.attack(); | ||||||
| 		troll.fleeBattle(); | 		troll.fleeBattle(); | ||||||
|  | 		System.out.printf("Simple troll power %d.\n", troll.getAttackPower()); | ||||||
|  |  | ||||||
| 		// change the behavior of the simple troll by adding a decorator | 		// change the behavior of the simple troll by adding a decorator | ||||||
| 		System.out.println("\nA smart looking troll surprises you."); | 		System.out.println("\nA smart looking troll surprises you."); | ||||||
| 		Hostile smart = new SmartTroll(troll); | 		Hostile smart = new SmartTroll(troll); | ||||||
| 		smart.attack(); | 		smart.attack(); | ||||||
| 		smart.fleeBattle(); | 		smart.fleeBattle(); | ||||||
|  | 		System.out.printf("Smart troll power %d.\n", smart.getAttackPower()); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -9,6 +9,8 @@ public interface Hostile { | |||||||
|  |  | ||||||
| 	void attack(); | 	void attack(); | ||||||
|  |  | ||||||
|  | 	int getAttackPower(); | ||||||
|  |  | ||||||
| 	void fleeBattle(); | 	void fleeBattle(); | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -21,6 +21,12 @@ public class SmartTroll implements Hostile { | |||||||
| 		decorated.attack(); | 		decorated.attack(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public int getAttackPower() { | ||||||
|  | 		// decorated troll power + 20 because it is smart | ||||||
|  | 		return decorated.getAttackPower() + 20; | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	@Override | 	@Override | ||||||
| 	public void fleeBattle() { | 	public void fleeBattle() { | ||||||
| 		System.out.println("The troll calls for help!"); | 		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!"); | 		System.out.println("The troll swings at you with a club!"); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public int getAttackPower() { | ||||||
|  | 		return 10; | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	public void fleeBattle() { | 	public void fleeBattle() { | ||||||
| 		System.out.println("The troll shrieks in horror and runs away!"); | 		System.out.println("The troll shrieks in horror and runs away!"); | ||||||
| 	} | 	} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user