C++ 设计模式之装饰者模式
2016-09-19 15:52
375 查看
1 装饰模式的类图:
2装饰着模式(Decorator): 动态的给一个对象添加一些额外的职责.
比如java.io包. BufferedInputStream封装了FileInputStream, 它们都实现了InputStream接口, 但前者实现了readLine方法.
3 代码实例:
#include<iostream>
#include<string>
using namespace std;
class Phone
{
public:
virtual void ShowDecorator(){};
};
class NokiaPhone :public Phone
{
private:
string m_name;
public:
NokiaPhone(string name):m_name(name){}
void ShowDecorator()
{
cout<<m_name <<"’s Decorator"<<endl;
}
};
class iPhone:public Phone
{
private:
string m_name;
public:
iPhone(string name):m_name(name)
{
}
void ShowDecorator()
{
cout<<m_name <<"’s Decorator"<<endl;
}
};
class DecoratorPhone:public Phone
{
private:
Phone *m_phone; // 要装饰的手机
public:
DecoratorPhone(Phone *phone):m_phone(phone)
{
}
virtual void ShowDecorator()
{
m_phone->ShowDecorator();
}
};
class DecoratorPhoneA:public DecoratorPhone
{
private:
void AddDecorate()
{
cout<<" Add GUA JIAN "<<endl;
}
public:
DecoratorPhoneA(Phone *phone):DecoratorPhone(phone)
{
}
void ShowDecorator()
{
DecoratorPhone::ShowDecorator();
AddDecorate();
}
};
class DecoratorPhoneB:public DecoratorPhone
{
private:
void AddDecorate()
{
cout<<" PING MU Tie Mo "<<endl;
}
public:
DecoratorPhoneB(Phone *phone):DecoratorPhone(phone)
{
}
void ShowDecorator()
{
DecoratorPhone::ShowDecorator();
AddDecorate();
}
};
//主程序:
// Decorator.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "PhoneDecorator.hpp"
/*
装饰者模式
*/
int _tmain(int argc, _TCHAR* argv[])
{
Phone *iphone = new iPhone("Apple Phone");
iphone->ShowDecorator();
cout<<endl;
Phone *dqa = new DecoratorPhoneA(iphone);
dqa->ShowDecorator();
cout<<endl;
Phone *dqb = new DecoratorPhoneB(dqa);
dqb->ShowDecorator();
system("pause");
return 0;
}
//运行结果:
2装饰着模式(Decorator): 动态的给一个对象添加一些额外的职责.
比如java.io包. BufferedInputStream封装了FileInputStream, 它们都实现了InputStream接口, 但前者实现了readLine方法.
3 代码实例:
#include<iostream>
#include<string>
using namespace std;
class Phone
{
public:
virtual void ShowDecorator(){};
};
class NokiaPhone :public Phone
{
private:
string m_name;
public:
NokiaPhone(string name):m_name(name){}
void ShowDecorator()
{
cout<<m_name <<"’s Decorator"<<endl;
}
};
class iPhone:public Phone
{
private:
string m_name;
public:
iPhone(string name):m_name(name)
{
}
void ShowDecorator()
{
cout<<m_name <<"’s Decorator"<<endl;
}
};
class DecoratorPhone:public Phone
{
private:
Phone *m_phone; // 要装饰的手机
public:
DecoratorPhone(Phone *phone):m_phone(phone)
{
}
virtual void ShowDecorator()
{
m_phone->ShowDecorator();
}
};
class DecoratorPhoneA:public DecoratorPhone
{
private:
void AddDecorate()
{
cout<<" Add GUA JIAN "<<endl;
}
public:
DecoratorPhoneA(Phone *phone):DecoratorPhone(phone)
{
}
void ShowDecorator()
{
DecoratorPhone::ShowDecorator();
AddDecorate();
}
};
class DecoratorPhoneB:public DecoratorPhone
{
private:
void AddDecorate()
{
cout<<" PING MU Tie Mo "<<endl;
}
public:
DecoratorPhoneB(Phone *phone):DecoratorPhone(phone)
{
}
void ShowDecorator()
{
DecoratorPhone::ShowDecorator();
AddDecorate();
}
};
//主程序:
// Decorator.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "PhoneDecorator.hpp"
/*
装饰者模式
*/
int _tmain(int argc, _TCHAR* argv[])
{
Phone *iphone = new iPhone("Apple Phone");
iphone->ShowDecorator();
cout<<endl;
Phone *dqa = new DecoratorPhoneA(iphone);
dqa->ShowDecorator();
cout<<endl;
Phone *dqb = new DecoratorPhoneB(dqa);
dqb->ShowDecorator();
system("pause");
return 0;
}
//运行结果:
相关文章推荐
- hdu4006 The kth great number 优先队列
- 我的C语言之路
- C++三大特性之继承简述
- C\C++代码优化的27个建议
- C++Windows编程之创建窗口
- C++类中一个构造函数调用另一个构造函数
- COGS 2060 除法表达式
- 《C++高效编程:内存与性能优化》第十二章:优化IO
- hadoop 3.0.0 上运行pipes c++ 程序
- C语言状态机学习笔记二
- C语言状态机学习笔记一
- 安全起见,小心使用C语言realloc()函数
- C语言:哲学家就餐问题
- C++静态类型成员变量的初始化顺序
- Win10 VC++运行库集合|VC++ 2005 2008 2010 2012 2015
- c++枚举和搜索注册表
- PAT乙级1002
- PAT乙级1001
- C/C++指针字符串常见面试题
- C++的当型循环和直到型循环中的逻辑判断