我必须基于UML图使用Decorator模式。根据输入的字符串类型,我必须返回价格和说明。我遇到的问题是,UML描述严格指定了价格函数不是虚拟的,而且,它仅应在Smoothy界面中实现,如下代码所示:
#include<iostream>
using namespace std;
class Smoothy{
int price;
public:
int getPrice(){
return price;
}
virtual ~Smoothy() = default;
virtual string description() = 0;
};
class BasicSmoothy: public Smoothy{
private:
string nume;
public:
BasicSmoothy(string n): nume(n){}
string description(){
return nume;
}
};
class SmoothyDecorator:public Smoothy{
private:
Smoothy *b;
public:
SmoothyDecorator(Smoothy* bb){
b = bb;
}
~SmoothyDecorator(){
delete b;
}
string description(){
return b->description();
}
};
class SmoothyWithCream:public SmoothyDecorator{
public:
SmoothyWithCream(Smoothy *b):SmoothyDecorator(b){
}
string description(){
return SmoothyDecorator::description() + " with Cream!";
}
};
class SmoothyWithCinnamon:public SmoothyDecorator{
public:
SmoothyWithCinnamon(Smoothy *b):SmoothyDecorator(b){
}
string description(){
return SmoothyDecorator::description() + " with Cinnamon!";
}
};
int main(){
Smoothy* b = new SmoothyWithCinnamon(new BasicSmoothy("Kiwi"));
cout<<b->description();
}
我很确定我的代码反映了Decorator模式(请问是否不知道),但是我不确定如何根据字符串返回价格。除此之外,UML图还指定BasicSmoothy具有两种类型,具有两种特定的价格(猕猴桃10 $,草莓12 $)和派生类分别为最终列出的价格增加2 $和3 $。
是否有一种方法可以通过getPrice()函数返回价格,而不必将其虚拟化,也可以不在其他类中实现它?
答案 0 :(得分:1)
您可以保护价格并在装饰器中将其覆盖:
#include<iostream>
using namespace std;
class Smoothy{
protected:
int price;
public:
int getPrice(){
return price;
}
virtual ~Smoothy() = default;
virtual string description() = 0;
};
class BasicSmoothy: public Smoothy{
private:
string nume;
public:
BasicSmoothy(string n): nume(n) {
if (nume == "Kiwi") {
price = 10;
} else if (nume == "Strawberry") {
price = 12;
} else {
throw;
}
}
string description(){
return nume;
}
};
class SmoothyDecorator:public Smoothy{
private:
Smoothy *b;
public:
SmoothyDecorator(Smoothy* bb){
b = bb;
}
~SmoothyDecorator(){
delete b;
}
string description(){
return b->description();
}
};
class SmoothyWithCream:public SmoothyDecorator{
public:
SmoothyWithCream(Smoothy *b):SmoothyDecorator(b){
price = b->getPrice() + 2;
}
string description(){
return SmoothyDecorator::description() + " with Cream!";
}
};
class SmoothyWithCinnamon:public SmoothyDecorator{
public:
SmoothyWithCinnamon(Smoothy *b):SmoothyDecorator(b) {
price = b->getPrice() + 3;
}
string description(){
return SmoothyDecorator::description() + " with Cinnamon!";
}
};
int main(){
Smoothy* b = new SmoothyWithCinnamon(new BasicSmoothy("Kiwi"));
cout<<b->description() << std::endl;
cout << b->getPrice();
}
答案 1 :(得分:0)
对于任何好奇的人,我设法找到了解决方法。
class Smoothy
{
public:
Smoothy()
{
}
Smoothy(int n):
price(n)
{
};
virtual ~Smoothy() = default;
int getPrice()
{
return price;
}
virtual string description() = 0;
private:
int price;
};
class BasicSmoothy :
public Smoothy
{
public:
BasicSmoothy(string n) :
Smoothy(n=="Kiwi"?10:12),
nume(n)
{
}
string description()
{
return nume;
}
private:
string nume;
};
class SmoothyDecorator :
public Smoothy
{
public:
SmoothyDecorator(Smoothy* bb, int pret) :
Smoothy(pret + bb->getPrice()), b(bb)
{
}
~SmoothyDecorator()
{
delete b;
}
string description()
{
return b->description();
}
private:
Smoothy* b;
};
class SmoothyWithCream :
public SmoothyDecorator
{
public:
SmoothyWithCream(Smoothy* b) :
SmoothyDecorator(b, 2)
{
}
virtual string description()
{
return SmoothyDecorator::description() + " with Cream!" + to_string(getPrice());
}
};
class SmoothyWithCinnamon :
public SmoothyDecorator
{
public:
SmoothyWithCinnamon(Smoothy* b) :
SmoothyDecorator(b, 3)
{
}
virtual string description()
{
return SmoothyDecorator::description() + " with Cinnamon!" + to_string(getPrice());
}
};
int main()
{
Smoothy* b1 = new SmoothyWithCinnamon(new SmoothyWithCream(new BasicSmoothy("Kiwi")));
Smoothy* b2 = new SmoothyWithCinnamon(new SmoothyWithCream(new BasicSmoothy("Strawberry")));
cout <<b1->description() << std::endl;
cout <<b2->description() << std::endl;
delete b1;
delete b2;
}