装饰器模式:为什么装饰两次后属性消失?

时间:2018-11-26 18:06:23

标签: java decorator delegation

我刚刚了解了装饰器模式。我正在尝试实现一个始终导致NullPointerException的方法。我一直在寻找缺少的参数,但找不到任何东西。我尝试将两个Decorator应用于Duck Object时,QuackBehavior似乎“消失了”。预先感谢!

PS:当我将属性(Duck)设置为public并使吸气剂保持不变时,NullPointerException似乎没有出现。但这对我不起作用,因为我以后要解析为XML文件。

Exception in thread "main" java.lang.NullPointerException
    at de.ostfalia.swt.progaufgabe_4.HoarseDuckDecorator.quack(HoarseDuckDecorator.java:12)
    at de.ostfalia.swt.progaufgabe_4.App.main(App.java:25)

鸭子

public abstract class Duck{

    private QuackBehavior quack;
    private FlyBehavior fly;
    private DuckState state;

    public Duck() {
        setState(DuckState.STANDING);
    }


    public void fly() {
        getFly().fly(this);
    }

    public void land() {
        getFly().land(this);
    }


    public String quack() {
        return quack.quack();
    }


    public void setQuackBehavior (QuackBehavior quack) {
        this.quack = quack;
    }

    public void setFlyBehavior (FlyBehavior fly) {
        this.setFly(fly);
    }

    public QuackBehavior getQuackBehavior() {
        return this.quack;
    }

    public void setState (DuckState state) {
        this.state = state;
    }

    public DuckState getState() {
        return state;
    }

    public FlyBehavior getFly() {
        return fly;
    }

    public void setFly(FlyBehavior fly) {
        this.fly = fly;
    }


}

QuackBehavior

public abstract class QuackBehavior {
    public String geräusch;
    public QuackBehavior() {
        this.geräusch = "";
    }

    public String quack() {
        return this.geräusch;

    }

    public void setGeräusch(String s) {
    }

}

Quack(某些Quack行为)

public class Quack extends QuackBehavior {

    String geräusch = "Quack!";

    public String quack() {
        return geräusch;
    }

    public void setGeräusch(String s) {
        this.geräusch = s;
    }

}

Quiek(某些QuackBehavior)

public class Quiek extends QuackBehavior {

    String geräusch = "Quiek!";

    public String quack() {
        return geräusch;
    }

    public void setGeräusch(String s) {
        this.geräusch = s;
    }

FlyBehavior

public abstract class FlyBehavior {

    public void fly(Duck parent) {
    }
    public void land(Duck parent) {
    }

}

FlyWithWings(某些FlyBehavior)

public class FlyWithWings extends FlyBehavior{

    public void fly(Duck parent) {
        if (parent.getState().equals(DuckState.STANDING)){
            parent.setState(DuckState.FLYING);
        }
    }

    public void land(Duck parent) {
        if (parent.getState().equals(DuckState.FLYING)){
            parent.setState(DuckState.STANDING);
        }
    }

}

FlyNot(某些FlyBehavior)

public class FlyNot extends FlyBehavior {

    public void fly(Duck parent) {

    }

    public void land(Duck parent) {

    }

}

DuckState

public enum DuckState {
    FLYING, STANDING;
}

RedheadDuck(实际鸭子)

public class RedheadDuck extends Duck{

    private FlyWithWings fly;
    private Quack quack;

    public RedheadDuck() {
        this.fly = new FlyWithWings();
        this.quack = new Quack();
    }

}

RubberDuck(实际上不是真正的鸭子)

public class RubberDuck extends Duck {

    private FlyNot fly;
    private Quiek quack;

    public RubberDuck() {
        this.fly = new FlyNot();
        this.quack = new Quiek();
    }

}

应用程序(我正在运行的代码段)

public class App {
    public static void main(String[] args) {

        Duck duck = new ChattyDuckDecorator(new MallardDuck()); 
        duck = new HoarseDuckDecorator(duck);
        ducksim.addDuck(duck);
        System.out.println(duck.quack());  //NullPointerException   
    }

}

DuckDecorator

public abstract class DuckDecorator extends Duck {

    public Duck specialDuck;

    public DuckDecorator(Duck ducky) {
        this.specialDuck = ducky;
    }

    public String quack(Duck ducky) {
        return specialDuck.quack();
    }

}

HoarseDuckDecorator(删除aeiou /这是NullPointerException发生的地方)

public class HoarseDuckDecorator extends DuckDecorator {

    public HoarseDuckDecorator(Duck ducky) {
        super(ducky);
    }


    public String quack() {
        this.specialDuck.getQuackBehavior().setGeräusch(specialDuck.getQuackBehavior().quack().replaceAll("(?i)[aeiou]", "")); //NullPointerException
        return specialDuck.quack();
    }

}

ChattyDuckDecorator(乘以字符串)

public class ChattyDuckDecorator extends DuckDecorator {
    public ChattyDuckDecorator(Duck ducky) {
        super(ducky);
    }

    public String quack() {
        specialDuck.getQuackBehavior().setGeräusch(specialDuck.getQuackBehavior().quack() + specialDuck.getQuackBehavior().quack());
        return specialDuck.quack();
    }
}

0 个答案:

没有答案