DrinkDecorator
public abstract class DrinkDecorator extends Drink {
public Drink drink;
}
Drink
public abstract class Drink {
String description;
int cost;
abstract int getCost();
String getDesc() {
return this.description;
}
}
Vodka
public class Vodka extends Drink {
String description = " Vodka ";
int cost = 100;
@Override
int getCost() {
return this.cost;
}
}
public class OrangeJuice extends DrinkDecorator {
String description = " fresh orenge juice ";
int cost = 5;
public OrangeJuice(Drink myVodka) {
this.drink = myVodka;
}
@Override
int getCost() {
return this.cost + this.drink.getCost();
}
}
Client Code
//Make Vodka with orange
//make vodka
Drink myVodka = new Vodka();
//mix orenge and vodka
DrinkDecorator orengeVodka= new OrangeJuice(myVodka);
//mix lime juie in the orengeVodka
DrinkDecorator LimeOrengeVodka= new LimeJuice(orengeVodka);
//the cost of the final vodka should be that of lime
//orenge and vodka cost
System.out.println(LimeOrengeVodka.getCost());
No comments:
Post a Comment