[英]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;
double 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? 我该如何将鞭子列为成本最高的物品,如何删除它?
How could I locate with highest costing item without knowing which one it is?. 如何在不知道是哪一项的情况下找到成本最高的项目? How would I even know it has a whip in the first place, for example? 例如,我什至怎么会知道它有鞭子? Is there a way a checking what extras it has then removing highest costing item? 有没有一种方法可以检查它有什么额外功能,然后删除成本最高的项目?
You would need Beverage to implement a getBaseBeverage()
method, returning itself, and condimentDecorators would need to override the method, returning beverage
. 您将需要Beverage来实现getBaseBeverage()
方法,并返回自身,而getBaseBeverage()
将需要重写该方法,并返回beverage
。
Beverage *base = coffee->getBaseBeverage();
if (base != coffee) {
double cost = coffee->cost() - base->cost();
}
That gets the cost of the last decorator. 那得到了最后一个装饰器的成本。 You could repeat the operation to get the cost of each previous decorator, perhaps in a loop, and record the largest decorator cost. 您可以重复该操作以循环获取每个先前装饰器的成本,并记录最大的装饰器成本。
Removing a decorator would require creating a new decorator chain that skips the item to be removed, linking instead to that item's getBaseBeverage(). 删除装饰器将需要创建一个新的装饰器链,以跳过要删除的项目,而是链接到该项目的getBaseBeverage()。
None of this is easy, without making large changes to the classes you gave. 如果不对您提供的类进行大的更改,这都不容易。 You could add a function to Beverage that returns the list of decorators. 您可以在饮料中添加一个返回装饰器列表的函数。 You could add a function to return the incremental cost of each decorator. 您可以添加一个函数来返回每个装饰器的增量成本。 You could even add a function to remove a particular decorator from the beverage. 您甚至可以添加一个功能来从饮料中删除特定的装饰器。 But then you have gone far beyond a "decorator" type pattern, to something entirely different. 但是,您已经远远超出了“装饰器”类型的模式,完全不同了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.