From 9336284814ce7d1265025be8eb754a4738a81b81 Mon Sep 17 00:00:00 2001 From: Ilkka Seppala Date: Sat, 6 Dec 2014 14:35:18 +0200 Subject: [PATCH] Improved decorator example. --- decorator/src/main/java/com/iluwatar/App.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/decorator/src/main/java/com/iluwatar/App.java b/decorator/src/main/java/com/iluwatar/App.java index 3ff421ffe..8318091da 100644 --- a/decorator/src/main/java/com/iluwatar/App.java +++ b/decorator/src/main/java/com/iluwatar/App.java @@ -6,18 +6,23 @@ package com.iluwatar; * 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(new Troll()); + Hostile smart = new SmartTroll(troll); smart.attack(); smart.fleeBattle(); }