diff --git a/decorator/etc/decorator.jpg b/decorator/etc/decorator.jpg index 9c9110b2b..4e234a9b2 100644 Binary files a/decorator/etc/decorator.jpg and b/decorator/etc/decorator.jpg differ diff --git a/decorator/src/main/java/com/iluwatar/App.java b/decorator/src/main/java/com/iluwatar/App.java index f002c159d..77978844d 100644 --- a/decorator/src/main/java/com/iluwatar/App.java +++ b/decorator/src/main/java/com/iluwatar/App.java @@ -1,17 +1,25 @@ package com.iluwatar; +/** + * + * 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. + * + */ public class App { public static void main( String[] args ) { System.out.println("A simple looking troll approaches."); - Troll troll = new Troll(); + Hostile troll = new Troll(); troll.attack(); troll.fleeBattle(); System.out.println("\nA smart looking troll surprises you."); - Troll smart = new SmartTroll(new Troll()); + Hostile smart = new SmartTroll(new Troll()); smart.attack(); smart.fleeBattle(); } diff --git a/decorator/src/main/java/com/iluwatar/Hostile.java b/decorator/src/main/java/com/iluwatar/Hostile.java new file mode 100644 index 000000000..38b2cad07 --- /dev/null +++ b/decorator/src/main/java/com/iluwatar/Hostile.java @@ -0,0 +1,8 @@ +package com.iluwatar; + +public interface Hostile { + + void attack(); + void fleeBattle(); + +} diff --git a/decorator/src/main/java/com/iluwatar/SmartTroll.java b/decorator/src/main/java/com/iluwatar/SmartTroll.java index 835891be3..d931adff8 100644 --- a/decorator/src/main/java/com/iluwatar/SmartTroll.java +++ b/decorator/src/main/java/com/iluwatar/SmartTroll.java @@ -1,10 +1,10 @@ package com.iluwatar; -public class SmartTroll extends Troll { +public class SmartTroll implements Hostile { - private Troll decorated; + private Hostile decorated; - public SmartTroll(Troll decorated) { + public SmartTroll(Hostile decorated) { this.decorated = decorated; } diff --git a/decorator/src/main/java/com/iluwatar/Troll.java b/decorator/src/main/java/com/iluwatar/Troll.java index f5c10018c..3bcf4ec47 100644 --- a/decorator/src/main/java/com/iluwatar/Troll.java +++ b/decorator/src/main/java/com/iluwatar/Troll.java @@ -1,6 +1,6 @@ package com.iluwatar; -public class Troll { +public class Troll implements Hostile { public void attack() { System.out.println("The troll swings at you with a club!");