如何在不使用虚拟的情况下创建装饰器函数
How to create a decorator function without using virtual
我必须使用基于UML图的装饰器模式。根据我输入的字符串类型,我必须返回价格和描述。我遇到的问题是 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();
}
我很确定我的代码反映了装饰器模式(如果没有,请告诉我(,但我不确定如何根据字符串返回价格。除此之外,UML 图指定 BasicSmoothy 有两种类型,具有两种特定价格(Kiwi 10$,Strawberry 12$(,派生类在最终列出的价格上分别增加 2$ 和 3$。
有没有办法通过函数 getPrice(( 返回价格,而不需要它是虚拟的,也没有在其他类中实现它?
您可以保护价格并在装饰器中覆盖它:
#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();
}
对于任何好奇的人,我都设法找到了解决方案。
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;
}
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在没有数据拷贝的情况下从指针创建一个Eigen VectorXd对象
- 是否可以在没有真实文件的情况下创建 ifstream
- 如何在不使用静态矩形方法的情况下创建 IDOMPathGeometryPtr?
- 在没有默认构造函数的情况下创建的派生对象
- 如何在没有侦听器的情况下创建 TCP 连接?
- 如何在不使用new的情况下保持在其他对象中创建的对象存活?
- 如何在不使用 std::tm 和 mktime() 的情况下为给定日历日期创建 chrono::time_point 对
- 我可以在不创建项目的情况下在Visual Studio 2019中编译和运行HelloWorld.cpp吗?
- 如何在不重新创建现有文件的情况下写入.txt文件
- 如何在不使用 "new" 关键字的情况下解除分配创建的对象的内存?
- 有没有一种方法可以在不复制数据的情况下从string_view创建字符串流
- C ++:如何在不创建对象的情况下在主函数中调用方法
- 在不创建新节点的情况下实现带有映射的trie
- 如何在不创建新配置的情况下对两个不同解决方案使用的一个项目使用不同的 #defines
- 如何在不使用宽字符串的情况下创建目录C++?
- 如何在不迭代的情况下从string_view向量创建字符串向量?
- 变量如何在不创建对象的情况下获得内存
- 在不创建子矩阵的情况下计算C++矩阵的行列式
- 如何在不以不同方式创建每个向量的情况下制作 2-D 向量列表?