提交 e742950c 编写于 作者: I Ilkka Seppala

added decorator sample

上级 1d4f0f1e
<?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>
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();
}
}
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();
}
}
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!");
}
}
......@@ -26,5 +26,6 @@
<module>adapter</module>
<module>bridge</module>
<module>composite</module>
<module>decorator</module>
</modules>
</project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册