Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -2,37 +2,37 @@ package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Decorator pattern is a more flexible alternative to subclassing. The Decorator
|
||||
* class implements the same interface as the target and uses composition to
|
||||
* "decorate" calls to the target. Using the Decorator pattern it is possible to
|
||||
* change the behavior of the class during runtime.
|
||||
* The Decorator pattern is a more flexible alternative to subclassing. The Decorator class
|
||||
* implements the same interface as the target and uses composition to "decorate" calls to the
|
||||
* target. Using the Decorator pattern it is possible to change the behavior of the class during
|
||||
* runtime.
|
||||
* <p>
|
||||
* In this example we show how the simple {@link Troll} first attacks and then
|
||||
* flees the battle. Then we decorate the {@link Troll} with a {@link SmartTroll}
|
||||
* and perform the attack again. You can see how the behavior changes after the
|
||||
* decoration.
|
||||
* In this example we show how the simple {@link Troll} first attacks and then flees the battle.
|
||||
* Then we decorate the {@link Troll} with a {@link SmartTroll} and perform the attack again. You
|
||||
* can see how the behavior changes after the decoration.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// simple troll
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
Hostile troll = new Troll();
|
||||
troll.attack();
|
||||
troll.fleeBattle();
|
||||
System.out.printf("Simple troll power %d.\n", troll.getAttackPower());
|
||||
// simple troll
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
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());
|
||||
}
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.decorator;
|
||||
*/
|
||||
public interface Hostile {
|
||||
|
||||
void attack();
|
||||
void attack();
|
||||
|
||||
int getAttackPower();
|
||||
int getAttackPower();
|
||||
|
||||
void fleeBattle();
|
||||
void fleeBattle();
|
||||
|
||||
}
|
||||
|
@ -1,36 +1,34 @@
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
* SmartTroll is a decorator for {@link Hostile} objects.
|
||||
* The calls to the {@link Hostile} interface are intercepted
|
||||
* and decorated. Finally the calls are delegated
|
||||
* to the decorated {@link Hostile} object.
|
||||
* SmartTroll is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
|
||||
* are intercepted and decorated. Finally the calls are delegated to the decorated {@link Hostile}
|
||||
* object.
|
||||
*
|
||||
*/
|
||||
public class SmartTroll implements Hostile {
|
||||
|
||||
private Hostile decorated;
|
||||
private Hostile decorated;
|
||||
|
||||
public SmartTroll(Hostile decorated) {
|
||||
this.decorated = decorated;
|
||||
}
|
||||
public SmartTroll(Hostile decorated) {
|
||||
this.decorated = decorated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("The troll throws a rock at you!");
|
||||
decorated.attack();
|
||||
}
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("The troll throws a rock at you!");
|
||||
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!");
|
||||
decorated.fleeBattle();
|
||||
}
|
||||
@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!");
|
||||
decorated.fleeBattle();
|
||||
}
|
||||
}
|
||||
|
@ -7,17 +7,16 @@ package com.iluwatar.decorator;
|
||||
*/
|
||||
public class Troll implements Hostile {
|
||||
|
||||
public void attack() {
|
||||
System.out.println("The troll swings at you with a club!");
|
||||
}
|
||||
public void attack() {
|
||||
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!");
|
||||
}
|
||||
@Override
|
||||
public int getAttackPower() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public void fleeBattle() {
|
||||
System.out.println("The troll shrieks in horror and runs away!");
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.decorator.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user