From 10a911be5498c3444f849a0490c3b75b9d151cc7 Mon Sep 17 00:00:00 2001 From: Ilkka Seppala Date: Tue, 18 Aug 2015 22:10:57 +0300 Subject: [PATCH] #107 Decorator example JavaDoc --- .../main/java/com/iluwatar/decorator/App.java | 62 ++++++++++--------- .../com/iluwatar/decorator/SmartTroll.java | 60 +++++++++--------- .../java/com/iluwatar/decorator/Troll.java | 36 +++++------ .../java/com/iluwatar/decorator/AppTest.java | 33 +++++----- 4 files changed, 100 insertions(+), 91 deletions(-) diff --git a/decorator/src/main/java/com/iluwatar/decorator/App.java b/decorator/src/main/java/com/iluwatar/decorator/App.java index f424f51b9..88e1c7103 100644 --- a/decorator/src/main/java/com/iluwatar/decorator/App.java +++ b/decorator/src/main/java/com/iluwatar/decorator/App.java @@ -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. + *

+ * 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(); + } +} diff --git a/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java b/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java index 9baa729f3..22ba88dc8 100644 --- a/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java +++ b/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java @@ -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(); + } + +} diff --git a/decorator/src/main/java/com/iluwatar/decorator/Troll.java b/decorator/src/main/java/com/iluwatar/decorator/Troll.java index 8d6cd0aa5..11e9b9d1a 100644 --- a/decorator/src/main/java/com/iluwatar/decorator/Troll.java +++ b/decorator/src/main/java/com/iluwatar/decorator/Troll.java @@ -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!"); + } + +} diff --git a/decorator/src/test/java/com/iluwatar/decorator/AppTest.java b/decorator/src/test/java/com/iluwatar/decorator/AppTest.java index 28776c930..b74bd3a06 100644 --- a/decorator/src/test/java/com/iluwatar/decorator/AppTest.java +++ b/decorator/src/test/java/com/iluwatar/decorator/AppTest.java @@ -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); + } +}