设计模式C++学习笔记之十三(Decorator装饰模式)
裝飾模式,動(dòng)態(tài)地給一個(gè)對(duì)象添加一些額外的職責(zé)。就增加功能來(lái)說(shuō),Decorator模式相比生成子類(lèi)更為靈活。
13.1.解釋
main(),老爸
ISchoolReport,成績(jī)單接口
CFourthGradeSchoolReport,四年級(jí)成績(jī)單
ReportDecorator,成績(jī)單裝飾器基類(lèi)
HighScoreDecorator,最高分裝飾器
SortDecorator,班級(jí)排名裝飾器
說(shuō)明:對(duì)“四年級(jí)成績(jī)單”進(jìn)行裝飾,ReportDecorator必然有一個(gè)private變量指向ISchoolReport。
注意:
看代碼:
// Decorator.cpp//主程序
#include "stdafx.h"
#include "ISchoolReport.h"
#include "FouthGradeSchoolReport.h"
#include "SugarFouthGradeSchoolReport.h"
#include "HighScoreDecorator.h"
#include "SortDecorator.h"
#include <iostream>
using std::cout;
using std::endl;
void DoIt()
{
??? ISchoolReport *psr = new CSugarFouthGradeSchoolReport();
??? psr->Report();//看成績(jī)單
??? psr->Sign("老三");//很開(kāi)心,就簽字了
??? delete psr;
}
void DoNew()
{
??? cout << "----------分部分進(jìn)行裝飾----------" << endl;
??? ISchoolReport *psr = new CFouthGradeSchoolReport();//原裝成績(jī)單
??? //
??? ISchoolReport *pssr = new CSortDecorator(psr);//又加了成績(jī)排名的說(shuō)明
??? ISchoolReport *phsr = new CHighScoreDecorator(pssr);//加了最高分說(shuō)明的成績(jī)單
??? phsr->Report();//看成績(jī)單
??? phsr->Sign("老三");//很開(kāi)心,就簽字了
??? 
??? //先裝飾哪個(gè)不重要,順序已經(jīng)在裝飾內(nèi)部確定好,但一定要調(diào)用最后一個(gè)裝飾器的接口。
??? //ISchoolReport *phsr = new CHighScoreDecorator(psr);//加了最高分說(shuō)明的成績(jī)單
??? //ISchoolReport *pssr = new CSortDecorator(phsr);//又加了成績(jī)排名的說(shuō)明
??? //pssr->Report();//看成績(jī)單
??? //pssr->Sign("老三");//很開(kāi)心,就簽字了
??? delete pssr;
??? delete phsr;
??? delete psr;
}
int _tmain(int argc, _TCHAR* argv[])
{
??? //在裝飾之前,可以用繼承的辦法,來(lái)進(jìn)行簡(jiǎn)單的修飾
??? DoIt();
??? //但如果需要修飾的項(xiàng)目太多呢?或者裝飾的項(xiàng)目不是固定的,繼承顯然會(huì)變得更復(fù)雜
??? DoNew();
??? _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
??? _CrtDumpMemoryLeaks();
??? return 0;
}
//ISchoolReport.h
#pragma once
#include <iostream>
using std::string;
class ISchoolReport
{
public:
??? ISchoolReport(void)
??? {
??? }
??? virtual ~ISchoolReport(void)
??? {
??? }
??? virtual void Report() = 0;
??? virtual void Sign(string name) = 0;
};
//FouthGradeSchoolReport.h
#pragma once
#include "ischoolreport.h"
class CFouthGradeSchoolReport :
??? public ISchoolReport
{
public:
??? CFouthGradeSchoolReport(void);
??? ~CFouthGradeSchoolReport(void);
??? void Report();
??? void Sign(string name);
};
//FouthGradeSchoolReport.cpp
#include "StdAfx.h"
#include "FouthGradeSchoolReport.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
CFouthGradeSchoolReport::CFouthGradeSchoolReport(void)
{
}
CFouthGradeSchoolReport::~CFouthGradeSchoolReport(void)
{
}
void CFouthGradeSchoolReport::Report()
{
??? cout << "尊敬的XXX家長(zhǎng):" << endl;
??? cout << "......" << endl;
??? cout << "語(yǔ)文62? 數(shù)學(xué)65? 體育98? 自然63" << endl;
??? cout << "......" << endl;
??? cout << "??????????????? 家長(zhǎng)簽名:" << endl;
}
void CFouthGradeSchoolReport::Sign(string name)
{
??? cout << "家長(zhǎng)簽名為:" << name.c_str() << endl;
}
//ReportDecorator.h
#pragma once
#include "ischoolreport.h"
class CReportDecorator :
??? public ISchoolReport
{
public:
??? CReportDecorator(ISchoolReport *psr);
??? virtual ~CReportDecorator(void);
??? void Report();
??? void Sign(string name);
private:
??? ISchoolReport *m_pSchoolReport;
};
//ReportDecorator.cpp
#include "StdAfx.h"
#include "ReportDecorator.h"
#include <iostream>
using std::string;
CReportDecorator::CReportDecorator(ISchoolReport *psr)
{
??? this->m_pSchoolReport = psr;
}
CReportDecorator::~CReportDecorator(void)
{
}
void CReportDecorator::Report()
{
??? this->m_pSchoolReport->Report();
}
void CReportDecorator::Sign( string name )
{
??? this->m_pSchoolReport->Sign(name);
}
//HighScoreDecorator.h
#pragma once
#include "reportdecorator.h"
#include "ISchoolReport.h"
class CHighScoreDecorator :
??? public CReportDecorator
{
public:
??? CHighScoreDecorator(ISchoolReport *psr);
??? ~CHighScoreDecorator(void);
??? void Report();
private:
??? void ReportHighScore();
};
//HighScoreDecorator.cpp
#include "StdAfx.h"
#include "HighScoreDecorator.h"
#include <iostream>
using std::cout;
using std::endl;
CHighScoreDecorator::CHighScoreDecorator( ISchoolReport *psr ) : CReportDecorator(psr)
{
}
CHighScoreDecorator::~CHighScoreDecorator(void)
{
}
void CHighScoreDecorator::Report()
{
??? this->ReportHighScore();
??? this->CReportDecorator::Report();
}
void CHighScoreDecorator::ReportHighScore()
{
??? cout << "這次考試語(yǔ)文最高是75, 數(shù)學(xué)是78, 自然是80" << endl;
}
//SortDecorator.h
#pragma once
#include "reportdecorator.h"
#include "ISchoolReport.h"
class CSortDecorator :
??? public CReportDecorator
{
public:
??? CSortDecorator(ISchoolReport *psr);
??? ~CSortDecorator(void);
??? void Report();
private:
??? void ReportSort();
};
//SortDecorator.cpp
#include "StdAfx.h"
#include "SortDecorator.h"
#include <iostream>
using std::cout;
using std::endl;
CSortDecorator::CSortDecorator( ISchoolReport *psr ) : CReportDecorator(psr)
{
}
CSortDecorator::~CSortDecorator(void)
{
}
void CSortDecorator::ReportSort()
{
??? cout << "我是排名第38名..." << endl;
}
void CSortDecorator::Report()
{
??? this->CReportDecorator::Report();
??? this->ReportSort();
}
這也是一個(gè)比較簡(jiǎn)單的模式,屬于行為型模式。
轉(zhuǎn)載于:https://www.cnblogs.com/wanggary/archive/2011/04/18/2020254.html
總結(jié)
以上是生活随笔為你收集整理的设计模式C++学习笔记之十三(Decorator装饰模式)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 中信小白Two卡权益介绍 丰富权益嗨到爆
- 下一篇: 工行宇宙星座信用卡额度一般是多少?资质决
