I learning design pattern in java. I tried to implement the code but the getDescription()
method is not executing. Can anyone please help me?
This is my Bevarage Class:
public abstract class Bevarage {
public String description = "Unknown Bevarage";
public String getDescripltion() {
return description;
}
public abstract double cost();
}
This is condimentDecorator extending Bevarage
public abstract class CondimentDecorator extends Bevarage {
public abstract String getDescrption();
}
This is the decorator class extending CondimentDecorator which has only a abstract getDescription methiod.
public class Mocha extends CondimentDecorator {
Bevarage bevarage;
public Mocha(Bevarage bevarage) {
this.bevarage = bevarage;
}
@Override
public double cost() {
// TODO Auto-generated method stub
return 0.20 + bevarage.cost();
}
@Override
public String getDescrption() {
System.out.println("reached Mocha");
return bevarage.getDescripltion() + ", Mocha";
}
}
Implementing with the decoratorators, The cost() method is working fine but description is not.
Bevarage bevarage2 = new DarkRoast();
// System.out.println(bevarage2.getDescripltion());
bevarage2 = new Mocha(bevarage2);
bevarage2 = new Mocha(bevarage2);
bevarage2 = new Whip(bevarage2);
System.out.println("Coffee: " + bevarage2.getDescripltion() + " $" + bevarage2.cost());
OUTPUT
Coffee: Espresso $1.99
Coffee: Unknown Bevarage $1.49
Coffee: Unknown Bevarage $1.34
it is calculating the cost correctly but i can't figure out what's wrong with the getDiscription()
Aucun commentaire:
Enregistrer un commentaire