X Tutup
Skip to content

Commit e742950

Browse files
committed
added decorator sample
1 parent 1d4f0f1 commit e742950

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

decorator/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<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"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>decorator</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>decorator</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
8+
System.out.println("A simple looking troll approaches.");
9+
Troll troll = new Troll();
10+
troll.attack();
11+
troll.fleeBattle();
12+
13+
System.out.println("\nA smart looking troll surprises you.");
14+
Troll smart = new SmartTroll(new Troll());
15+
smart.attack();
16+
smart.fleeBattle();
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar;
2+
3+
public class SmartTroll extends Troll {
4+
5+
private Troll decorated;
6+
7+
public SmartTroll(Troll decorated) {
8+
this.decorated = decorated;
9+
}
10+
11+
@Override
12+
public void attack() {
13+
System.out.println("The troll throws a rock at you!");
14+
decorated.attack();
15+
}
16+
17+
@Override
18+
public void fleeBattle() {
19+
System.out.println("The troll calls for help!");
20+
decorated.fleeBattle();
21+
}
22+
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.iluwatar;
2+
3+
public class Troll {
4+
5+
public void attack() {
6+
System.out.println("The troll swings at you with a club!");
7+
}
8+
9+
public void fleeBattle() {
10+
System.out.println("The troll shrieks in horror and runs away!");
11+
}
12+
13+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
<module>adapter</module>
2727
<module>bridge</module>
2828
<module>composite</module>
29+
<module>decorator</module>
2930
</modules>
3031
</project>

0 commit comments

Comments
 (0)
X Tutup