#include<iostream>
using namespace std;
class Component{
public:
Component(string n){name = n;};
Component(){};
virtual void show(){cout<<"A human : "<<name <<endl;};
private:
string name;
};
class Decorate : public Component{
public:
// Component(string s):name(s){};
virtual void show(){component->show();};
void set(Component * c){
component = c;
};
protected:
Component * component;
string name;
};
class Skirt : public Decorate{
public:
// Skirt(string s) : Decorate(s){};
void show(){
component->show();
cout<<"Dress skirt"<<endl;
};
};
class Tshirt : public Decorate{
public:
void show(){
component->show();
cout<<"Dress T-shirt"<<endl;
};
};
int main(){
Component * girl = new Component("girl");
Skirt * skirt = new Skirt;
Tshirt * tshirt = new Tshirt;
skirt->set(girl);
tshirt->set(skirt);
tshirt->show();
return 0;
}
装饰模式c++实现
最新推荐文章于 2024-08-21 00:07:57 发布