diff --git a/decorator/pom.xml b/decorator/pom.xml new file mode 100644 index 000000000..471a73bfe --- /dev/null +++ b/decorator/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.0-SNAPSHOT + + com.iluwatar + decorator + 1.0-SNAPSHOT + decorator + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/decorator/src/main/java/com/iluwatar/App.java b/decorator/src/main/java/com/iluwatar/App.java new file mode 100644 index 000000000..f002c159d --- /dev/null +++ b/decorator/src/main/java/com/iluwatar/App.java @@ -0,0 +1,18 @@ +package com.iluwatar; + +public class App +{ + public static void main( String[] args ) + { + + System.out.println("A simple looking troll approaches."); + Troll troll = new Troll(); + troll.attack(); + troll.fleeBattle(); + + System.out.println("\nA smart looking troll surprises you."); + Troll smart = new SmartTroll(new Troll()); + smart.attack(); + smart.fleeBattle(); + } +} diff --git a/decorator/src/main/java/com/iluwatar/SmartTroll.java b/decorator/src/main/java/com/iluwatar/SmartTroll.java new file mode 100644 index 000000000..835891be3 --- /dev/null +++ b/decorator/src/main/java/com/iluwatar/SmartTroll.java @@ -0,0 +1,23 @@ +package com.iluwatar; + +public class SmartTroll extends Troll { + + private Troll decorated; + + public SmartTroll(Troll 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/Troll.java b/decorator/src/main/java/com/iluwatar/Troll.java new file mode 100644 index 000000000..f5c10018c --- /dev/null +++ b/decorator/src/main/java/com/iluwatar/Troll.java @@ -0,0 +1,13 @@ +package com.iluwatar; + +public class Troll { + + 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/pom.xml b/pom.xml index 0a74b8682..b6853a3c0 100644 --- a/pom.xml +++ b/pom.xml @@ -26,5 +26,6 @@ adapter bridge composite + decorator \ No newline at end of file