Java 无法使装饰器模式正常工作
请参阅下面的代码。我看不出我错在哪里。我也很困惑!非常感谢您的帮助Java 无法使装饰器模式正常工作,java,design-patterns,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));
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;
}
}
问题是你实际上没有装饰任何东西。也就是说,您没有使用装饰器“包装”组件 下面是一个例子 这本书中有一个很好的例子:。我非常喜欢这本书,如果你还没有,我强烈推荐。这里:
}
如果您需要几个decorator,您可以将
DecoratedPilot
抽象并从中继承每个特定的decorator。您得到了什么输出,以及您希望得到什么输出?请正确缩进代码并设置其格式,这样读起来非常困难。感谢您重新格式化,我对这个论坛还是一个新手——我得到的输出来自于实验课,它需要来自装饰课。另外,当我删除getDection()方法时,Pilot类一直在请求它。教程不会让我有任何进展…@AljoshaBre-这可能会让我从以前的尝试中感到困惑,我很抱歉,我需要客户端从decorator获取字符串“Pilot has Enward…”,而不向Pilot类添加任何内容…事实上,我需要从中删除“public string getDecotation()”。这不是问题所在。首先,尝试在链接中编写示例,了解其工作原理,然后实现您自己的解决方案。如何让一个类包装另一个类?@user1360809,通过向构造函数传递一个对象,就像Yanfela在回答中所做的那样。如下所示:Pilot-decoratedPilot=新的decoratedPilot(standardPilot);需要注意的是,这里的修饰方法是getAge()。通常,对于装饰方法,装饰器将完成其部分工作,然后对包含(内部或包装)对象调用装饰方法。谢谢你的精确性。
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";
}
}