added decorator sample

This commit is contained in:
Ilkka Seppala
2014-08-13 21:45:19 +03:00
parent 1d4f0f1e29
commit e742950c19
5 changed files with 78 additions and 0 deletions

View File

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

View File

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

View File

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