user1360809
user1360809

Reputation: 745

cannot get decorator pattern to work

Please see code below. I cannot see where I am going wrong. I am also very confused! Much obliged for any help!

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

Upvotes: 1

Views: 150

Answers (2)

Brady
Brady

Reputation: 10357

The problem is you're not actually decorating anything. That is, you're not "wrapping" a Component with a Decorator.

Here's an example of the Decorator pattern.

An excellent example of this pattern is in the book: Head First Design Patterns. I really like this book and highly recommend it if you dont already have it.

Upvotes: 0

Yanflea
Yanflea

Reputation: 3934

Here :


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

If you need several decorators, you can make DecoratedPilot abstract and inherits each specific decorator from it.

Upvotes: 3

Related Questions