added decorator sample
This commit is contained in:
18
decorator/src/main/java/com/iluwatar/App.java
Normal file
18
decorator/src/main/java/com/iluwatar/App.java
Normal 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();
|
||||
}
|
||||
}
|
23
decorator/src/main/java/com/iluwatar/SmartTroll.java
Normal file
23
decorator/src/main/java/com/iluwatar/SmartTroll.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
13
decorator/src/main/java/com/iluwatar/Troll.java
Normal file
13
decorator/src/main/java/com/iluwatar/Troll.java
Normal 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!");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user