包装模式(Decorate)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <sstream> using namespace std; template <class T> string MyConvertToStr(T obj) { stringstream ss; ss << obj; return ss.str(); } class Beverage { public: virtual string getDescription() = 0; virtual double cost() = 0; protected: string m_Description; }; class CondimentDecorate :public Beverage { public: virtual string getDescription() = 0; protected: CondimentDecorate(){} }; //装饰类1:Soy class Soy :public CondimentDecorate { public: Soy(Beverage *pBeverage):m_pBeverage(pBeverage){} string getDescription() { return m_pBeverage->getDescription() + ",加了酱油了"; } double cost() { return 0.1 + m_pBeverage->cost(); } private: //持有被装饰的对象 Beverage *m_pBeverage; }; //装饰类2:Milk class Milk :public CondimentDecorate { public: Milk(Beverage *pBeverage) :m_pBeverage(pBeverage) {} string getDescription() { return m_pBeverage->getDescription() + ",加了酱油了"; } double cost() { return 5+ m_pBeverage->cost(); } private: //持有被装饰的对象 Beverage *m_pBeverage; }; class Lattee :public Beverage { public: Lattee() { m_Description = "拿铁"; } string getDescription() { return m_Description; } double cost() { return 13; } }; class Expresso :public Beverage { public: Expresso() { m_Description = "浓缩咖啡"; } string getDescription() { return m_Description; } double cost() { return 13; } }; class Mocha :public Beverage { public: Mocha() { m_Description = "摩卡"; } string getDescription() { return m_Description; } double cost() { return 13; } }; int main(void) { Beverage* pBeverage = new Expresso(); cout << pBeverage->getDescription() << "价格是" << MyConvertToStr(pBeverage->cost()) << endl; Beverage* pBeverage2 = new Mocha(); pBeverage2 = new Soy(pBeverage2); pBeverage2 = new Soy(pBeverage2); cout << pBeverage2->getDescription() << "价格是" << MyConvertToStr(pBeverage2->cost()) << endl; system("pause"); return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <windows.h> using namespace std; class CView { public: virtual void Draw(void){} }; //抽象装饰类 class CDecorate :public CView { public: CDecorate(CView *pView):m_pView(pView){} virtual void Draw(void) { m_pView->Draw(); } private: CView* m_pView; }; class CScrollBars :public CDecorate { public: CScrollBars(CView *pView):CDecorate(pView){} protected: //绘制滚动条 void DrawScrollBars() { cout << ">绘制了一个滚动条" << endl; } public: virtual void Draw() { CDecorate::Draw(); DrawScrollBars(); } }; class CBorder :public CDecorate { public: CBorder(CView *pView) :CDecorate(pView) {} protected: //绘制滚动条 void DrawBorder() { cout << ">绘制了一个边框" << endl; } public: virtual void Draw() { CDecorate::Draw(); DrawBorder(); } }; //context->主要的被装饰对象 class CTextView :public CView { public: CTextView(char* s) { SetWindowText(s); } protected: virtual void DrawTextView() { cout << ">绘制了一个文本视图,文本的内容为" << m_Text; } protected: char m_Text[MAX_PATH]; public: virtual void Draw() { CView::Draw(); DrawTextView(); } void SetWindowText(char* s) { strcpy_s(m_Text, _countof(m_Text), s); } char* GetWindowText() { return m_Text; } }; class CWindows { public: CWindows() { ZeroMemory(m_arrWindow, sizeof(CView*) * 10); } void Draw(){ for (int i = 0;i < 10;i++) { if (m_arrWindow[i] != NULL) m_arrWindow[i]->Draw(); } } void AddControl(CView* pView) { for (int i = 0;i < 10;i++) { if (m_arrWindow[i] == NULL){ m_arrWindow[i] = pView; break; } } } void RemoveControl(CView* pView) { for (int i = 0;i < 10;i++) { if (m_arrWindow[i] == pView) { m_arrWindow[i] = NULL; break; } } } protected: CView* m_arrWindow[10]; }; int main(void) { //定义一个窗口对象 CWindows windowsObj; //定义一个普通的文本视图 CTextView TextView("我是一个文本视图"); windowsObj.AddControl(&TextView); windowsObj.Draw(); cout << "*****************" << endl; windowsObj.RemoveControl(&TextView); CScrollBars* pScroll = new CScrollBars(&TextView); windowsObj.AddControl(pScroll); windowsObj.Draw(); cout << "*****************" << endl; windowsObj.RemoveControl(pScroll); CBorder *pBorder = new CBorder(&TextView); windowsObj.AddControl(pBorder); windowsObj.Draw(); system("pause"); return 0; }