#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;
/**
*
* Decorator pattern is 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 decorator pattern it is possible to change class behavior during
* runtime, as the example shows.
*
*/
public class App {
public static void main(String[] args) {
// simple troll
System.out.println("A simple looking troll approaches.");
Hostile troll = new Troll();
troll.attack();
troll.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();
}
}
package com.iluwatar.decorator;
/**
*
* 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.
* <p>
* Using decorator pattern it is possible to change class behavior during
* runtime, as the example shows.
*
*/
public class App {
/**
* 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();
// 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;
/**
* SmartTroll is a decorator for Hostile objects.
* The calls to the Hostile interface are intercepted
* and decorated. Finally the calls are delegated
* to the decorated Hostile object.
*
*/
public class SmartTroll implements Hostile {
private Hostile 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 fleeBattle() {
System.out.println("The troll calls for help!");
decorated.fleeBattle();
}
}
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.
*
*/
public class SmartTroll implements Hostile {
private Hostile 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 fleeBattle() {
System.out.println("The troll calls for help!");
decorated.fleeBattle();
}
}

View File

@ -1,18 +1,18 @@
package com.iluwatar.decorator;
/**
*
* Troll implements Hostile interface directly.
*
*/
public class Troll implements Hostile {
public void attack() {
System.out.println("The troll swings at you with a club!");
}
public void fleeBattle() {
System.out.println("The troll shrieks in horror and runs away!");
}
}
package com.iluwatar.decorator;
/**
*
* Troll implements {@link Hostile} interface directly.
*
*/
public class Troll implements Hostile {
public void attack() {
System.out.println("The troll swings at you with a club!");
}
public void fleeBattle() {
System.out.println("The troll shrieks in horror and runs away!");
}
}

View File

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