#107 Decorator example JavaDoc

This commit is contained in:
Ilkka Seppala 2015-08-18 22:10:57 +03:00
parent 3526d96e37
commit 10a911be54
4 changed files with 100 additions and 91 deletions

View File

@ -1,29 +1,33 @@
package com.iluwatar.decorator; package com.iluwatar.decorator;
/** /**
* *
* Decorator pattern is more flexible alternative to subclassing. The decorator * Decorator pattern is a more flexible alternative to subclassing. The decorator
* class implements the same interface as the target and uses composition to * class implements the same interface as the target and uses composition to
* "decorate" calls to the target. * "decorate" calls to the target.
* * <p>
* Using decorator pattern it is possible to change class behavior during * Using decorator pattern it is possible to change class behavior during
* runtime, as the example shows. * runtime, as the example shows.
* *
*/ */
public class App { public class App {
public static void main(String[] args) { /**
* Program entry point
// simple troll * @param args command line args
System.out.println("A simple looking troll approaches."); */
Hostile troll = new Troll(); public static void main(String[] args) {
troll.attack();
troll.fleeBattle(); // simple troll
System.out.println("A simple looking troll approaches.");
// change the behavior of the simple troll by adding a decorator Hostile troll = new Troll();
System.out.println("\nA smart looking troll surprises you."); troll.attack();
Hostile smart = new SmartTroll(troll); troll.fleeBattle();
smart.attack();
smart.fleeBattle(); // 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();
}
}

View File

@ -1,30 +1,30 @@
package com.iluwatar.decorator; package com.iluwatar.decorator;
/** /**
* SmartTroll is a decorator for Hostile objects. * SmartTroll is a decorator for {@link Hostile} objects.
* The calls to the Hostile interface are intercepted * The calls to the {@link Hostile} interface are intercepted
* and decorated. Finally the calls are delegated * and decorated. Finally the calls are delegated
* to the decorated Hostile object. * to the decorated {@link Hostile} object.
* *
*/ */
public class SmartTroll implements Hostile { public class SmartTroll implements Hostile {
private Hostile decorated; private Hostile decorated;
public SmartTroll(Hostile decorated) { public SmartTroll(Hostile decorated) {
this.decorated = decorated; this.decorated = decorated;
} }
@Override @Override
public void attack() { public void attack() {
System.out.println("The troll throws a rock at you!"); System.out.println("The troll throws a rock at you!");
decorated.attack(); decorated.attack();
} }
@Override @Override
public void fleeBattle() { public void fleeBattle() {
System.out.println("The troll calls for help!"); System.out.println("The troll calls for help!");
decorated.fleeBattle(); decorated.fleeBattle();
} }
} }

View File

@ -1,18 +1,18 @@
package com.iluwatar.decorator; package com.iluwatar.decorator;
/** /**
* *
* Troll implements Hostile interface directly. * Troll implements {@link Hostile} interface directly.
* *
*/ */
public class Troll implements Hostile { public class Troll implements Hostile {
public void attack() { public void attack() {
System.out.println("The troll swings at you with a club!"); System.out.println("The troll swings at you with a club!");
} }
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!");
} }
} }

View File

@ -1,14 +1,19 @@
package com.iluwatar.decorator; package com.iluwatar.decorator;
import org.junit.Test; import org.junit.Test;
import com.iluwatar.decorator.App; import com.iluwatar.decorator.App;
public class AppTest { /**
*
@Test * Application test
public void test() { *
String[] args = {}; */
App.main(args); public class AppTest {
}
} @Test
public void test() {
String[] args = {};
App.main(args);
}
}