//声明文件
//************************************************************************************
//结构型模式:装饰模式
//场景:在显示每个Window前追加显示某个logo的功能。
//************************抽象组件类******************
class CBaseWnd{
public:
virtual ~CBaseWnd();
virtual void Show() = 0;//显示
};
//***********************具体组件角色*******************
class CMainWnd : public CBaseWnd{
public:
~CMainWnd();
void Show();
};
//*****************************装饰器类**********************
class CWndDecorator : public CBaseWnd{
protected:
CBaseWnd* pWnd;
public:
CWndDecorator(CBaseWnd* wnd);
~CWndDecorator();
void Show();
};
//具体的装饰器
class CLoginDlg : public CWndDecorator{
public:
CLoginDlg(CBaseWnd* wnd);
~CLoginDlg();
void Show();
};
class CSettingDlg : public CWndDecorator{
public:
CSettingDlg(CBaseWnd* wnd);
~CSettingDlg();
void Show();
};
//实现文件
//************************************************************************************
//结构型模式:装饰模式
//场景:在显示每个Window前追加显示某个logo的功能。
//************************抽象组件类******************
CBaseWnd::~CBaseWnd(){}
//***********************具体组件角色*******************
CMainWnd::~CMainWnd(){ cout << "~MainWnd" << endl;}
void CMainWnd::Show(){cout << "MainWnd" << endl;}
//*****************************装饰器类**********************
CWndDecorator::CWndDecorator(CBaseWnd* wnd) : pWnd(wnd){}
CWndDecorator::~CWndDecorator(){ if(pWnd != NULL) delete pWnd;}
void CWndDecorator::Show(){ pWnd->Show();}
//具体的装饰器
CLoginDlg::CLoginDlg(CBaseWnd* wnd) : CWndDecorator(wnd){}
CLoginDlg::~CLoginDlg(){cout << "~CLoginDlg" << endl;}
void CLoginDlg::Show(){ cout << "Please input your acc and pwd : " << endl; pWnd->Show();}
CSettingDlg::CSettingDlg(CBaseWnd* wnd) : CWndDecorator(wnd){}
CSettingDlg::~CSettingDlg(){cout << "~CSettingDlg" << endl;}
void CSettingDlg::Show(){ cout << "Thanks for your using. Now begin your setting." << endl; pWnd->Show();}
//测试客户端
void main()
{
CBaseWnd* pMain = new CMainWnd();
CBaseWnd* pSettingDlg = new CSettingDlg(pMain);
CBaseWnd* pLoginDlg = new CLoginDlg(pSettingDlg);
pLoginDlg->Show();
delete pLoginDlg;
}