views:

82

answers:

3

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.

A: 

you could create a new method like so:

public abstract class condimentDecorator : Beverage
{
     public override string getDescription()
     {
          return getDescriptionInternal();
     }
     protected abstract string getDescriptionInternal();
}

That way, all classes would have to implement getDescription indirectly.

testalino
+3  A: 

I think your class hierarchy could use some rethinking.

How about this:

My suggestion is that you create an interface, IBeverage that you implement on everything that is drinkable. Then you create a base class for the "fundamental" beverages - DarkRoast, Espresso, HouseBlend - just like you did now.

For the Condiments, you implement a new abstract base class implementing IBeverage, but not providing a default GetDescription implementation. This class could also take a IBeverage in its constructor to force other condiments to do the same.

Something like this should work I think (untested, uncompiled - but you get the idea)

public interface IBeverage
{
  string GetDescription ();
}

public abstract class BeverageBase : IBeverage
{
  public virtual string GetDescription () { return "Unknown Beverage"; }
}

public class DarkRoast : BeverageBase { ... }
public class HouseBlend : BeverageBase { ...}

public abstract class CondimentBase : IBeverage
{
  public CondimentBase(IBeverage beverage)
  {
    Beverage = beverage;
  }
  protected IBeverage Beverage { get; set; }
  public abstract string GetDescription ();
}

public class Mocha : CondimentBase 
{
  public Mocha(IBeverage beverage)
    : base (beverage)
  { }
  public string GetDescription()
  {
    return Beverage.GetDescription() + ", Mocha";
  }
}
Isak Savo
+1  A: 

Quick google turned out this: http://codebetter.com/blogs/darrell.norton/archive/2005/05/18/63332.aspx

I didnt try the code itself.

Euphoric