📄 decreator.cpp
字号:
/* *装饰模式
*/
#include <iostream>
#include <string>
using namespace std;
class Goat
{
private: //??
string description;
string getDescription()
{
return description;
}
public:
Goat() {string description="I'm a goat.";}
virtual void function()=0;
};
class BlackGoat:public Goat
{
private:
string description;
string getDescription()
{
return description;
}
public:
BlackGoat(){string description="I'm a black goat.";}
};
class WhiteGoat:public Goat
{
private:
string description;
string getDescription()
{
return description;
}
public:
WhiteGoat()
{string description="I'm a white goat.";}
};
class Decreator:public Goat //Decreator基类根本没有用,搞不懂为什么还要继承他而不是Goat.添加新函数用?
{
public:
//virtual void function()=0;
void extraout()
{
cout << "extraout!"<<endl;
}
};
class Plus:public Decreator
{
private:
Goat* drt; //基类指针,因为第一次传入的是Goat
//换为Decreator后添加for goat的构造函数
public:
Plus(Goat* drt) //继承构造函数 //基类指针,因为要支持传入Goat
{
this->drt=drt;
}
void function()
{
cout << "I can do plus!"<<endl;
if(drt!=NULL) drt->function();
}
};
class Minus:public Decreator
{
private:
Goat* drt;
public:
Minus(Goat* drt) //继承构造函数
{
this->drt=drt;
}
void function()
{
cout << "Minus!"<<endl;
if(drt!=NULL) drt->function();
}
};
void decreator_main()
{
Goat *g=NULL; //自动NULL?
g=new Plus(g);
g=new Minus(g);
g=new Minus(g);
Minus k(NULL);
Goat *p=&k;
//p->extraout();
g->function();
delete g;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -