您的位置:首页 > 其它

大话设计模式之修饰模式

2016-08-19 16:08 453 查看
动态的给一个对象添加额外的职责,就增加功能而言,装饰模式比生成子类更为灵活。



//设计模式学习
class Compent{
public:
virtual void dipFushi() = 0 ;
protected:

};
class Person : public Compent{
public:
Person(string str): str_name(str){

}
virtual void dipFushi(){
cout <<str_name<< "装扮:"<<endl;
}
protected:
string str_name;
};
class Fushi : public Compent{
public:
void SetCompent(Compent *pcom){
this->pCompent = pcom;
}
virtual void dipFushi(){
if (NULL != pCompent)
{
pCompent->dipFushi();

}
}
protected:
Compent *pCompent;
};
class Yifu : public Fushi{
public:
Yifu(string str): str_name(str){

}
virtual void dipFushi(){
Fushi::dipFushi();
cout << str_name << " ";
}
private:
string str_name;
};
int main()
{
int n1 =9,n2=3;
Yifu *f1 = new Yifu("大衣");
Yifu *f2 = new Yifu("围巾");
Yifu *f3 = new Yifu("短裤");
Yifu *f4 = new Yifu("靴子");
Yifu *f5 = new Yifu("皮鞋");
Yifu *f6 = new Yifu("公文包");
Yifu *f7 = new Yifu("西服");
Yifu *f8 = new Yifu("领带");
Person *p1 =  new Person("小明");
Person *p2 = new Person("小菜");
f1->SetCompent(p1);
f2->SetCompent(f1);
f2->dipFushi();

f7->SetCompent(p2);
f8->SetCompent(f7);
f3->SetCompent(f8);
f5->SetCompent(f3);
f6->SetCompent(f5);
f6->dipFushi();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: