Question

I have some C# code as shown below, and I want to remove decorate object after adding, for example I have a tea with milk and sugar plus biscuit and I want to remove sugar from Order class, how to do that with the decorating design pattern:

public abstract class Order
{
    private int _Cost = 0;
    private string _Description = "";
    public virtual int GetCost()
    {
        return _Cost;
    }
    public virtual string GetDescription()
    {
        return _Description;
    }
}

public class Coffee : Order
{
    int _Cost = 3000;
    string _Description = "Coffee";
    public override int GetCost()
    {
        return _Cost;
    }
    public override string GetDescription()
    {
        return _Description;
    }
}

public class Tea : Order
{
    int _Cost = 2000;
    string _Description = "Tea";
    public override int GetCost()
    {
        return _Cost;
    }
    public override string GetDescription()
    {
        return _Description;
    }
}

public abstract class Options : Order
{
    int _Cost = 0;
    string _Description = "No Option";
    public override int GetCost()
    {
        return _Cost;
    }
    public override string GetDescription()
    {
        return _Description;
    }
}

public class Honey : Order
{
    int _Cost = 800;
    string _Description = "Honey";
    public Honey(Order order)
    {
        _Cost = _Cost + order.GetCost();
        _Description = order.GetDescription() + " And " + _Description;
    }
    public override int GetCost()
    {
        return _Cost;
    }
    public override string GetDescription()
    {
        return _Description;
    }
}

public class Biscuit : Order
{
    int _Cost = 400;
    string _Description = "Biscuit";
    public Biscuit(Order order)
    {
        _Cost = _Cost + order.GetCost();
        _Description = order.GetDescription() + " And " + _Description;
    }
    public override int GetCost()
    {
        return _Cost;
    }
    public override string GetDescription()
    {
        return _Description;
    }
}

public class Sugar : Order
{
    int _Cost = 300;
    string _Description = "Sugar";
    public Sugar(Order order)
    {
        _Cost = _Cost + order.GetCost();
        _Description = order.GetDescription() + " And " + _Description;
    }
    public override int GetCost()
    {
        return _Cost;
    }
    public override string GetDescription()
    {
        return _Description;
    }
}

public class Milk : Order
{
    int _Cost = 600;
    string _Description = "Milk";
    public Milk(Order order)
    {
        _Cost = _Cost + order.GetCost();
        _Description = order.GetDescription() + " And " + _Description;
    }
    public override int GetCost()
    {
        return _Cost;
    }
    public override string GetDescription()
    {
        return _Description;
    }
}

I appreciate if you answer me ASAP...

Was it helpful?

Solution

You can't and should not. Purpose of decorators is to be completely transparent to their clients (including other decorators). Also decorators by their intent should be used for (as specified by GoF):

Attaching additional responsibilities to an object dynamically.

Btw I don't think that your Biscuit or Tea should be decorators of order (and considered as orders). Create Order which has list of items. I.e. use aggregation instead of inheritance. Consider of orders which waiters create:

  • Tea with sugar and lemon x 2
  • Coffee with milk
  • Biscuit

I think your domain model should look same way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top