added decorator sample
This commit is contained in:
parent
1d4f0f1e29
commit
e742950c19
23
decorator/pom.xml
Normal file
23
decorator/pom.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>decorator</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>decorator</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
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!");
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user