大话设计模式-装饰模式(C++)
来源:互联网 发布:新速特软件站快优影音 编辑:程序博客网 时间:2024/04/29 17:51
《大话设计模式》的装饰模式的C++实现。
结构图:
Decorator.h:
#ifndef _DECORATOR_H#define _DECORATOR_H#include <iostream>#include <string>using namespace std;//ConcreteComponent类class CPerson{public:CPerson(){};CPerson(string str):m_sName(str){};virtual void Show();protected:private:string m_sName;};class CFinery : public CPerson{public://CFinery(CPerson *tmp):m_pComponent(tmp){};void Decorate(CPerson *component);void Show();protected:private:CPerson *m_pComponent;};//ConcreteDecoratorclass CTShirts :public CFinery{public://CTShirts(CPerson *tmp):m_pComponent(tmp){};void Show();protected:private:};class CBigTrouser : public CFinery{public://CBigTrouser(CPerson *tmp):m_pComponent(tmp){};void Show();protected:private:};#endif
#include "Decorator.h"void CPerson::Show(){cout<<"装饰人:"<<m_sName<<endl;}void CFinery::Show(){if (m_pComponent != NULL){m_pComponent->Show();}}void CFinery::Decorate( CPerson *component ){m_pComponent = component;}void CTShirts::Show(){CFinery::Show();cout<<"TShirts"<<endl;}void CBigTrouser::Show(){CFinery::Show();cout<<"垮裤"<<endl;}
#include <iostream>#include "Decorator.h"using namespace std;int main(){//装饰模式CPerson person("lin");CBigTrouser bigtrouser;CTShirts Tshirts;bigtrouser.Decorate(&person);Tshirts.Decorate(&bigtrouser);Tshirts.Show();return 0;}
0 0
- 大话设计模式-装饰模式(C++)
- 大话设计模式-装饰模式
- 大话设计模式-装饰模式
- 大话设计模式-装饰模式
- 大话设计模式--装饰模式
- 大话设计模式----装饰模式
- 大话设计之装饰模式
- 大话设计模式之装饰设计模式
- 我也大话设计模式--装饰模式
- 大话设计模式----装饰模式笔记
- 大话设计模式(四)装饰模式
- 大话设计模式之-----装饰模式
- 大话设计模式-装饰者模式
- 大话设计模式之装饰模式
- 大话设计模式_装饰模式
- 大话设计模式——装饰模式
- 大话设计模式读书笔记3 装饰模式
- 大话设计模式之装饰模式
- 复杂数据类型
- 在windows下搭建python环境,安装python、pip、virtualen、web.py
- 大数据时代的生物医学
- 鼠标碰撞检测与事件监听
- HDU 5209 Relief grain 解题报告(树链剖分 + 线段树)
- 大话设计模式-装饰模式(C++)
- [iOS] 使用UIRefreshControl 实现 UITableView下拉刷新(Swift版本)
- sqlServer增加一字段并给此字段添加注释
- 技术面试官经验分享
- C++ STL 中 map 容器的说明和使用技巧
- UVA 10859Placing Lampposts(树上的DP)
- 实习笔记4 java swing组件 JDialog模态化
- eclipse中server location为灰色,不能修改
- 黑马程序员——IO流