大话设计模式之修饰模式

动态的给一个对象添加额外的职责,就增加功能而言,装饰模式比生成子类更为灵活。


//设计模式学习
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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值