1

以下のコードを参照してください。どこが間違っているのかわかりません。私もとても迷っています!どんな助けにも大変感謝しています!

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;
}
}
4

2 に答える 2

3

ここ :


        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抽象化して、特定のデコレーターをそれぞれ継承することができます。

于 2012-05-14T15:37:49.267 に答える
0

問題は、実際には何も飾っていないことです。つまり、コンポーネントをデコレータで「ラップ」していません。

Decorator パターンの例を次に示します。

このパターンの優れた例は、Head First Design Patternsという本にあります。私はこの本が大好きで、まだ持っていない場合は強くお勧めします。

于 2012-05-14T14:46:09.653 に答える