提交 1168f6e4 编写于 作者: I Ilkka Seppala

Updated decorator implementation and class diagram. Added comments.

上级 e760858b
decorator/etc/decorator.jpg

19.8 KB | W: | H:

decorator/etc/decorator.jpg

31.8 KB | W: | H:

decorator/etc/decorator.jpg
decorator/etc/decorator.jpg
decorator/etc/decorator.jpg
decorator/etc/decorator.jpg
  • 2-up
  • Swipe
  • Onion skin
package com.iluwatar;
/**
*
* Decorator pattern is more flexible alternative to
* subclassing. The decorator class implements the same
* interface as the target and uses composition to
* "decorate" calls to the target.
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println("A simple looking troll approaches.");
Troll troll = new Troll();
Hostile troll = new Troll();
troll.attack();
troll.fleeBattle();
System.out.println("\nA smart looking troll surprises you.");
Troll smart = new SmartTroll(new Troll());
Hostile smart = new SmartTroll(new Troll());
smart.attack();
smart.fleeBattle();
}
......
package com.iluwatar;
public interface Hostile {
void attack();
void fleeBattle();
}
package com.iluwatar;
public class SmartTroll extends Troll {
public class SmartTroll implements Hostile {
private Troll decorated;
private Hostile decorated;
public SmartTroll(Troll decorated) {
public SmartTroll(Hostile decorated) {
this.decorated = decorated;
}
......
package com.iluwatar;
public class Troll {
public class Troll implements Hostile {
public void attack() {
System.out.println("The troll swings at you with a club!");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册