Updated decorator implementation and class diagram. Added comments.

This commit is contained in:
Ilkka Seppala 2014-08-31 09:17:43 +03:00
parent e760858bb6
commit 1168f6e41f
5 changed files with 22 additions and 6 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

@ -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();
}

View File

@ -0,0 +1,8 @@
package com.iluwatar;
public interface Hostile {
void attack();
void fleeBattle();
}

View File

@ -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;
}

View File

@ -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!");