java - 无法使装饰器模式工作

标签 java design-patterns

请看下面的代码。我看不出哪里出错了。我也很困惑!非常感谢您的帮助!

package code;

public class Client {

 public static void main(String[] args){

     proxyPlane plane = new proxyPlane(new Pilot(18));
        plane.flyPlane();

        plane = new proxyPlane(new Pilot(25));
        plane.flyPlane();

        DecoratedPilot decPilot = new Pilot(35);

        System.out.println("Experienced Pilot of age " + Pilot.Age() + " " + decPilot.getDecotation());   
    }

}


package code;

public interface DecoratedPilot {

public String getDecotation();
}


package code;

public class Decoration extends PilotDecorator {

public Decoration(Pilot pilot) {
    super(pilot);
    // TODO Auto-generated constructor stub
}

@Override
public String getDecotation() {
    return "Pilot has earned his Commercial Wings";
}
}


package code;

public abstract class PilotDecorator implements DecoratedPilot {

public PilotDecorator(Pilot pilot)
{
    apilot = pilot;
}
}


package code;

public class Pilot implements DecoratedPilot {

private static int age;

public static int Age() {

    return age;  
}

public Pilot(int age){

    Pilot.age = age;
}

public String getDecotation() {
    // TODO Auto-generated method stub
    return null;
}
}

最佳答案

这里:


        package code;

        public class Client {

            public static void main(String[] args) {
                // -- A standard pilot
                Pilot standardPilot = new StandardPilot(35);
                System.out.println("Pilot : " + standardPilot.getAge() + " - " + standardPilot.getDescription());
                // -- A decorated pilot
                Pilot decoratedPilot = new DecoratedPilot(standardPilot);
                System.out.println("Pilot : " + decoratedPilot.getAge() + " - " + decoratedPilot.getDescription());
            }
        }

        package code;

        public interface Pilot {
            int getAge();
            String getDescription();
        }

    package code;

    public class StandardPilot implements Pilot {

        private int age;

        public StandardPilot(int age) {
            this.age = age;
        }

        @Override
        public int getAge() {
            return age;
        }

        @Override
        public String getDescription() {
            return "Standard Pilot";
        }


package code;

public class DecoratedPilot implements Pilot {

    private Pilot pilot;

    public DecoratedPilot(Pilot pilot) {
        // todo : check not null
        this.pilot = pilot;
    }

    @Override
    public int getAge() {
        return pilot.getAge();
    }

    @Override
    public String getDescription() {
        return "Decorated Pilot";
    }
}

如果您需要多个装饰器,您可以使 DecoratedPilot 抽象并从中继承每个特定的装饰器。

关于java - 无法使装饰器模式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585300/

相关文章:

java - Mule ftp 入站没有 sizeCheckWaitTime 属性

java - Swagger UI - 可为空值

c# - 是检测异常并抛出它们还是让运行时抛出它们更好?

java - Web服务中请求的多种内容类型

language-agnostic - 有人可以给我一个过度使用设计模式的例子吗?

java - 添加非原始类型数据作为节点和关系中的属性值

java - 集成测试的数据加载和数据库初始化策略

java - @CucumberOptions 中的路径将在 Idea 或 mvn verify 中工作,但不能同时工作

c# - 支持 IoC 的基类方法的接口(interface)声明

design-patterns - 使用值对象链接方法