public class person // component
{
public:
person(){};
~person(){};
show() {cout<<"i wear";}
}
public class cloth // decorator
{
private:
person *temp;
public:
cloth(){};
decorator(person *t){temp=t;}
~cloth(){};
}
public class tshirt :public cloth //concrete decorator1
{
public:
tshirt(person * t):d(t){};
void show()
{temp->show();
cout<< "tshirt";
}
}
public class skirt :public cloth //concrete decorator12
{
public:
skirt (person * t):d(t){};
void show()
{temp->show();
cout<< "tkirt";
}
}
main()
{
person *p=new person();
tshirt *ten=new tshirt();
ten->decorator(p);
ten->show();
tskirt *tn=new tskirt();
tn->decorator(p);
tn->show();
}
给基本类component动态的增加新功能,形成具体的decorator类。
阅读(858) | 评论(0) | 转发(0) |