Repository: iluwatar/java-design-patterns
Issue: Decorator pattern: SmartTroll should be SmartHostile
Labels: info: help wanted
info: good first issue

As the SmartTroll decorates any Hostile object, it should be named SmartHostile instead. Because if there's a Goblin class implementing the Hostile interface, the SmartTroll class is technically able to decorate it too, even though it is named SmartTroll (which indicates that it should only be a Troll).


Issue comments:
I would suggest that we change the example so that `SmartTroll extends Troll`. It wouldn't hurt to add `Goblin` to the example too :smile: 

@iluwatar 
That would be a possible solution too, but the SmartTroll doesn't do anything Troll-specific (allthough the string "The troll throws a rock at you!" is hardcoded). It just increases the attack power.

Pl tell me what should I do....I am new to Open source and i want to make it as my first commit
Thankyou

@opg7371 
You could rename the SmartTroll-class to SmartHostile and replace the hardcoded strings in that class with generic examples (e.g. "It throws a rock at you!").

Since this issue hasn't been update for a while, I would like to give it a try. I renamed the SmartTroll-class to SmartHostile and replaced the hardcoded strings in that class to "It throws a rock at you!"
I also updated the class naming in the UML digram.

 I would like to submit a pull request. :)


Pull request review comments:
This method should be renamed to testSmartHostile


Patch that resolved the issue:
diff --git a/decorator/etc/decorator.ucls b/decorator/etc/decorator.ucls
index 7adb8c3a685a..a5353d4ecf4a 100644
--- a/decorator/etc/decorator.ucls
+++ b/decorator/etc/decorator.ucls
@@ -1,18 +1,18 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true" 
-  realizations="true" associations="true" dependencies="false" nesting-relationships="true">  
+<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true" 
+  associations="true" dependencies="false" nesting-relationships="true" router="FAN">  
   <interface id="1" language="java" name="com.iluwatar.decorator.Hostile" project="decorator" 
     file="/decorator/src/main/java/com/iluwatar/decorator/Hostile.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="106" width="134" x="116" y="408"/>    
+    <position height="-1" width="-1" x="554" y="188"/>    
     <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
       sort-features="false" accessors="true" visibility="true">      
       <attributes public="true" package="true" protected="true" private="true" static="true"/>      
       <operations public="true" package="true" protected="true" private="true" static="true"/>    
     </display>  
   </interface>  
-  <class id="2" language="java" name="com.iluwatar.decorator.SmartTroll" project="decorator" 
-    file="/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="124" width="134" x="116" y="244"/>    
+  <class id="2" language="java" name="com.iluwatar.decorator.App" project="decorator" 
+    file="/decorator/src/main/java/com/iluwatar/decorator/App.java" binary="false" corner="BOTTOM_RIGHT">    
+    <position height="-1" width="-1" x="117" y="202"/>    
     <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
       sort-features="false" accessors="true" visibility="true">      
       <attributes public="true" package="true" protected="true" private="true" static="true"/>      
@@ -21,29 +21,46 @@
   </class>  
   <class id="3" language="java" name="com.iluwatar.decorator.Troll" project="decorator" 
     file="/decorator/src/main/java/com/iluwatar/decorator/Troll.java" binary="false" corner="BOTTOM_RIGHT">    
-    <position height="124" width="134" x="290" y="244"/>    
+    <position height="-1" width="-1" x="315" y="100"/>    
     <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
       sort-features="false" accessors="true" visibility="true">      
       <attributes public="true" package="true" protected="true" private="true" static="true"/>      
       <operations public="true" package="true" protected="true" private="true" static="true"/>    
     </display>  
   </class>  
-  <realization id="4">    
+  <class id="4" language="java" name="com.iluwatar.decorator.SmartHostile" project="decorator" 
+    file="/decorator/src/main/java/com/iluwatar/decorator/SmartHostile.java" binary="false" corner="BOTTOM_RIGHT">    
+    <position height="-1" width="-1" x="318" y="315"/>    
+    <display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
+      sort-features="false" accessors="true" visibility="true">      
+      <attributes public="true" package="true" protected="true" private="true" static="true"/>      
+      <operations public="true" package="true" protected="true" private="true" static="true"/>    
+    </display>  
+  </class>  
+  <dependency id="5">    
     <end type="SOURCE" refId="2"/>    
+    <end type="TARGET" refId="4"/>  
+  </dependency>  
+  <realization id="6">    
+    <end type="SOURCE" refId="3"/>    
     <end type="TARGET" refId="1"/>  
   </realization>  
-  <association id="5">    
-    <end type="SOURCE" refId="2" navigable="false">      
-      <attribute id="6" name="decorated"/>      
-      <multiplicity id="7" minimum="0" maximum="1"/>    
+  <association id="7">    
+    <end type="SOURCE" refId="4" navigable="false">      
+      <attribute id="8" name="decorated"/>      
+      <multiplicity id="9" minimum="0" maximum="1"/>    
     </end>    
     <end type="TARGET" refId="1" navigable="true"/>    
     <display labels="true" multiplicity="true"/>  
   </association>  
-  <realization id="8">    
-    <end type="SOURCE" refId="3"/>    
+  <realization id="10">    
+    <end type="SOURCE" refId="4"/>    
     <end type="TARGET" refId="1"/>  
   </realization>  
+  <dependency id="11">    
+    <end type="SOURCE" refId="2"/>    
+    <end type="TARGET" refId="3"/>  
+  </dependency>  
   <classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" 
     sort-features="false" accessors="true" visibility="true">    
     <attributes public="true" package="true" protected="true" private="true" static="true"/>    
diff --git a/decorator/src/main/java/com/iluwatar/decorator/App.java b/decorator/src/main/java/com/iluwatar/decorator/App.java
index d58d3b61a930..242e72d11bbf 100644
--- a/decorator/src/main/java/com/iluwatar/decorator/App.java
+++ b/decorator/src/main/java/com/iluwatar/decorator/App.java
@@ -8,7 +8,7 @@
  * runtime.
  * <p>
  * In this example we show how the simple {@link Troll} first attacks and then flees the battle.
- * Then we decorate the {@link Troll} with a {@link SmartTroll} and perform the attack again. You
+ * Then we decorate the {@link Troll} with a {@link SmartHostile} and perform the attack again. You
  * can see how the behavior changes after the decoration.
  * 
  */
@@ -30,7 +30,7 @@ public static void main(String[] args) {
 
     // 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);
+    Hostile smart = new SmartHostile(troll);
     smart.attack();
     smart.fleeBattle();
     System.out.printf("Smart troll power %d.\n", smart.getAttackPower());
diff --git a/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java b/decorator/src/main/java/com/iluwatar/decorator/SmartHostile.java
similarity index 56%
rename from decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java
rename to decorator/src/main/java/com/iluwatar/decorator/SmartHostile.java
index 93927237dfd8..93f494688e1a 100644
--- a/decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java
+++ b/decorator/src/main/java/com/iluwatar/decorator/SmartHostile.java
@@ -1,34 +1,34 @@
 package com.iluwatar.decorator;
 
 /**
- * SmartTroll is a decorator for {@link Hostile} objects. The calls to the {@link Hostile} interface
+ * SmartHostile 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 {
+public class SmartHostile implements Hostile {
 
   private Hostile decorated;
 
-  public SmartTroll(Hostile decorated) {
+  public SmartHostile(Hostile decorated) {
     this.decorated = decorated;
   }
 
   @Override
   public void attack() {
-    System.out.println("The troll throws a rock at you!");
+    System.out.println("It throws a rock at you!");
     decorated.attack();
   }
 
   @Override
   public int getAttackPower() {
-    // decorated troll power + 20 because it is smart
+    // decorated hostile's power + 20 because it is smart
     return decorated.getAttackPower() + 20;
   }
 
   @Override
   public void fleeBattle() {
-    System.out.println("The troll calls for help!");
+    System.out.println("It calls for help!");
     decorated.fleeBattle();
   }
 }


Test patch:
diff --git a/decorator/src/test/java/com/iluwatar/decorator/SmartTrollTest.java b/decorator/src/test/java/com/iluwatar/decorator/SmartHostileTest.java
similarity index 85%
rename from decorator/src/test/java/com/iluwatar/decorator/SmartTrollTest.java
rename to decorator/src/test/java/com/iluwatar/decorator/SmartHostileTest.java
index faafc4a824f0..e5be32eae15a 100644
--- a/decorator/src/test/java/com/iluwatar/decorator/SmartTrollTest.java
+++ b/decorator/src/test/java/com/iluwatar/decorator/SmartHostileTest.java
@@ -11,15 +11,15 @@
  *
  * @author Jeroen Meulemeester
  */
-public class SmartTrollTest {
+public class SmartHostileTest {
 
   @Test
-  public void testSmartTroll() throws Exception {
+  public void testSmartHostile() throws Exception {
     // Create a normal troll first, but make sure we can spy on it later on.
     final Hostile simpleTroll = spy(new Troll());
 
     // Now we want to decorate the troll to make it smarter ...
-    final Hostile smartTroll = new SmartTroll(simpleTroll);
+    final Hostile smartTroll = new SmartHostile(simpleTroll);
     assertEquals(30, smartTroll.getAttackPower());
     verify(simpleTroll, times(1)).getAttackPower();