dimanche 6 mars 2016

How to remove a layer from a decorated item?

How could I remove items that have been added using the decorator design pattern? And such things as remove the lowest/highest costing item or point to it?

Say, for example, I had:

class Beverage
{
private:
    string description;
    string cost;
public:
    virtual string getDescription() { return description; }
    virtual double cost() { return cost };
};

class Espresso : public Beverage
{
public:
    string getDescription()
    {
            return "Espresso";
    }
    double cost()
    {
        return 1.99;
    }
};

class condimentDecorator : public Beverage
{
public:
    virtual string getDescription() = 0;
};

class Whip : public condimentDecorator
{
private:
    Beverage* beverage;
public:
    Whip(Beverage* beverage)
    {
        this->beverage = beverage;
    }
    double cost()
    {
        return beverage->cost() + 0.20;
    }
};

class Milk : public condimentDecorator
{
private:
    Beverage* beverage;
public:
    Milk(Beverage* beverage)
    {
        this->beverage = beverage;
    }
    string getDescription()
    {
        return beverage->getDescription() + ", Milk";

    }
    double cost()
    {
            return beverage->cost() + 0.10;
    }
};

class Soy : public condimentDecorator
{
private:
    Beverage* beverage;
public:
    Soy(Beverage* beverage)
    {
        this->beverage = beverage;
    }
    string getDescription()
    {
        return beverage->getDescription() + ", Soy";

    }
    double cost()
    {
            return beverage->cost() + 0.15;
    }
};

And I were to do:

Beverage* coffee = new Espresso();
coffee = new Whip(coffee);
coffee = new Milk(coffee);
coffee = new Soy(coffee);

How could I point to whip as the highest costing item and how could I remove it?

Aucun commentaire:

Enregistrer un commentaire