设计模式- 装饰模式
来源:互联网 发布:ptc软件 编辑:程序博客网 时间:2024/05/29 09:43
// 装饰模式:动态的给一个对象添加一些额外的职责,对于增加功能来说,装饰模式比生产子类更灵活;
// 人
class Person
{
public:
Person(void);
~Person(void);
Person(std::string name)
{
m_name = name;
}
virtual void Show()
{
cout<<"装扮的"<<m_name<<endl;
}
private:
string m_name;
};
// 装饰类;
class Finery : public Person
{
public:
Finery(void);
virtual~Finery(void);
void Decorate(Person *pPerson)
{
m_pPerson = pPerson;
}
void Show()
{
if (m_pPerson)
{
m_pPerson->Show();
}
}
private:
Person *m_pPerson;
};
class BigTrouser : public Finery
{
public:
BigTrouser(void);
~BigTrouser(void);
void Show()
{
cout<<"垮裤"<<endl;
Finery::Show();
}
};
class Sneakers : public Finery
{
public:
Sneakers(void);
~Sneakers(void);
void Show()
{
cout<<"破球鞋"<<endl;
Finery::Show();
}
};
class TShirts : public Finery
{
public:
TShirts(void);
~TShirts(void);
void Show()
{
cout<<"大T衣"<<endl;
Finery::Show();
}
};
#include "Sneakers.h"
#include "BigTrouser.h"
#include "TShirts.h"
// 装饰模式:动态的给一个对象添加一些额外的职责,对于增加功能来说,装饰模式比生产子类更灵活;
int _tmain(int argc, _TCHAR* argv[])
{
Person *pPerson = new Person("小菜");
Sneakers* pSneakers = new Sneakers;
BigTrouser* pBigTrouser = new BigTrouser;
TShirts* pTShirts = new TShirts;
pSneakers->Decorate(pPerson);
pBigTrouser->Decorate(pSneakers);
pTShirts->Decorate(pBigTrouser);
pTShirts->Show();
if (pPerson)
{
delete pPerson;
pPerson = NULL;
}
if (pSneakers)
{
delete pSneakers;
pSneakers = NULL;
}
if (pBigTrouser)
{
delete pBigTrouser;
pBigTrouser = NULL;
}
if (pTShirts)
{
delete pTShirts;
pTShirts = NULL;
}
return 0;
}
- 设计模式---装饰模式
- 设计模式:装饰模式
- 设计模式------装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式 装饰模式
- 设计模式----装饰模式
- 设计模式-【装饰模式】
- 设计模式--装饰模式
- 设计模式装饰模式
- 设计模式 - 装饰模式
- 设计模式-----装饰模式.
- 设计模式-----装饰模式.
- [ 设计模式 ] 装饰模式!
- 设计模式:装饰模式
- 设计模式 - 装饰模式
- 设计模式 - 装饰模式
- JavaWeb之Cookie&&Session详解
- centos7(x64) samba 4.2.10的配置
- 华为OJ——将真分数分解为埃及分数
- ubuntu12.04安装vnc,并使用ssh加密隧道连接
- springmvc底层调用流程
- 设计模式- 装饰模式
- 【完整的App项目】颖火虫笔记
- Keepalived+Nginx负载均衡双机热备
- 操作系统学习之BIOS
- 华为OJ——二维数组操作
- Java SE之集合学习
- set
- hdu5832-ccpc网络赛 -高精度取余
- 华为OJ——统计大写字母个数