Skip to content

Commit 1168f6e

Browse files
committed
Updated decorator implementation and class diagram. Added comments.
1 parent e760858 commit 1168f6e

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

decorator/etc/decorator.jpg

12 KB
Loading

decorator/src/main/java/com/iluwatar/App.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* Decorator pattern is more flexible alternative to
6+
* subclassing. The decorator class implements the same
7+
* interface as the target and uses composition to
8+
* "decorate" calls to the target.
9+
*
10+
*/
311
public class App
412
{
513
public static void main( String[] args )
614
{
715

816
System.out.println("A simple looking troll approaches.");
9-
Troll troll = new Troll();
17+
Hostile troll = new Troll();
1018
troll.attack();
1119
troll.fleeBattle();
1220

1321
System.out.println("\nA smart looking troll surprises you.");
14-
Troll smart = new SmartTroll(new Troll());
22+
Hostile smart = new SmartTroll(new Troll());
1523
smart.attack();
1624
smart.fleeBattle();
1725
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar;
2+
3+
public interface Hostile {
4+
5+
void attack();
6+
void fleeBattle();
7+
8+
}

decorator/src/main/java/com/iluwatar/SmartTroll.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.iluwatar;
22

3-
public class SmartTroll extends Troll {
3+
public class SmartTroll implements Hostile {
44

5-
private Troll decorated;
5+
private Hostile decorated;
66

7-
public SmartTroll(Troll decorated) {
7+
public SmartTroll(Hostile decorated) {
88
this.decorated = decorated;
99
}
1010

decorator/src/main/java/com/iluwatar/Troll.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.iluwatar;
22

3-
public class Troll {
3+
public class Troll implements Hostile {
44

55
public void attack() {
66
System.out.println("The troll swings at you with a club!");

0 commit comments

Comments
 (0)