i have got below code which is working fine:
public abstract class Beverage
{
public string description = "Unknown beverage";
public virtual string getDescription()
{
return description;
}
public abstract double cost();
}
public abstract class condimentDecorator : Beverage
{
// public abstract string getDescription();
}
public class DarkRoast : Beverage
{
public DarkRoast()
{
description = "DarkRoast";
}
public override double cost()
{
return 2.10;
}
}
public class Espresso : Beverage
{
public Espresso()
{
description = "Espresso";
}
public override double cost()
{
return 1.99;
}
}
public class HouseBlend : Beverage
{
public HouseBlend()
{
description = "House Blend Coffee";
}
public override double cost()
{
return .89;
}
}
public class Mocha : condimentDecorator
{
Beverage beverage;
public Mocha(Beverage beverage)
{
this.beverage = beverage;
}
public override string getDescription()
{
return beverage.getDescription() + ", Mocha";
}
public override double cost()
{
return .20 + beverage.cost();
}
}
public class Soy : condimentDecorator
{
Beverage beverage;
public Soy(Beverage beverage)
{
this.beverage = beverage;
}
public override string getDescription()
{
return beverage.getDescription() + ", Soy";
}
public override double cost()
{
return .10 + beverage.cost();
}
}
public class Whip : condimentDecorator
{
Beverage beverage;
public Whip(Beverage beverage)
{
this.beverage = beverage;
}
public override string getDescription()
{
return beverage.getDescription() + ", Whip";
}
public override double cost()
{
return .10 + beverage.cost();
}
}
I am using it in this way:
protected void Page_Load(object sender, EventArgs e)
{
Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
Response.Write ("<br> " + beverage2.getDescription() + " : $" + beverage2.cost().ToString());
}
Problem: i want all child class of "condimentDecorator" to forcefully override getDescription() funciton, for that i have written below code in "condimentDecorator" class:
public abstract string getDescription();
but that makes changes in my current functioning and not give desired result it just shows "Unknown beverage" as value of getDescription() which is value of parent most class.
Normal result:
DarkRoast, Mocha, Mocha, Whip : $2.6
Result after using "public abstract string getDescription()":
Unknown beverage : $2.6
Please suggest me what should i write/change so that i can force child classes of "condimentDecorator" to override "getDescription();" and also gets resutl as its working without it.