临沂网站建设报价,生活常识网站源码,网站怎么拿百度收入,wordpress 访客装饰模式#xff0c;动态地给一个对象添加一些额外的职责。就增加功能来说#xff0c;Decorator模式相比生成子类更为灵活。 13.1.解释 main()#xff0c;老爸 ISchoolReport#xff0c;成绩单接口 CFourthGradeSchoolReport#xff0c;四年级成绩单 ReportDecorator…装饰模式动态地给一个对象添加一些额外的职责。就增加功能来说Decorator模式相比生成子类更为灵活。 13.1.解释 main()老爸 ISchoolReport成绩单接口 CFourthGradeSchoolReport四年级成绩单 ReportDecorator成绩单装饰器基类 HighScoreDecorator最高分装饰器 SortDecorator班级排名装饰器 说明对“四年级成绩单”进行装饰ReportDecorator必然有一个private变量指向ISchoolReport。 注意 看代码 // Decorator.cpp//主程序#include stdafx.h#include ISchoolReport.h#include FouthGradeSchoolReport.h#include SugarFouthGradeSchoolReport.h#include HighScoreDecorator.h#include SortDecorator.h#include iostreamusing std::cout;using std::endl;void DoIt(){ ISchoolReport *psr new CSugarFouthGradeSchoolReport(); psr-Report();//看成绩单 psr-Sign(老三);//很开心就签字了 delete psr;}void DoNew(){ cout ----------分部分进行装饰---------- endl; ISchoolReport *psr new CFouthGradeSchoolReport();//原装成绩单 // ISchoolReport *pssr new CSortDecorator(psr);//又加了成绩排名的说明 ISchoolReport *phsr new CHighScoreDecorator(pssr);//加了最高分说明的成绩单 phsr-Report();//看成绩单 phsr-Sign(老三);//很开心就签字了 //先装饰哪个不重要顺序已经在装饰内部确定好但一定要调用最后一个装饰器的接口。 //ISchoolReport *phsr new CHighScoreDecorator(psr);//加了最高分说明的成绩单 //ISchoolReport *pssr new CSortDecorator(phsr);//又加了成绩排名的说明 //pssr-Report();//看成绩单 //pssr-Sign(老三);//很开心就签字了 delete pssr; delete phsr; delete psr;}int _tmain(int argc, _TCHAR* argv[]){ //在装饰之前可以用继承的办法来进行简单的修饰 DoIt(); //但如果需要修饰的项目太多呢或者装饰的项目不是固定的继承显然会变得更复杂 DoNew(); _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF); _CrtDumpMemoryLeaks(); return 0;} //ISchoolReport.h #pragma once#include iostreamusing 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.hclass CFouthGradeSchoolReport : public ISchoolReport{public: CFouthGradeSchoolReport(void); ~CFouthGradeSchoolReport(void); void Report(); void Sign(string name);}; //FouthGradeSchoolReport.cpp #include StdAfx.h#include FouthGradeSchoolReport.h#include iostreamusing std::cout;using std::endl;using std::string;CFouthGradeSchoolReport::CFouthGradeSchoolReport(void){}CFouthGradeSchoolReport::~CFouthGradeSchoolReport(void){}void CFouthGradeSchoolReport::Report(){ cout 尊敬的XXX家长 endl; cout ...... endl; cout 语文62 数学65 体育98 自然63 endl; cout ...... endl; cout 家长签名 endl;}void CFouthGradeSchoolReport::Sign(string name){ cout 家长签名为 name.c_str() endl;} //ReportDecorator.h #pragma once#include ischoolreport.hclass 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 iostreamusing 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.hclass CHighScoreDecorator : public CReportDecorator{public: CHighScoreDecorator(ISchoolReport *psr); ~CHighScoreDecorator(void); void Report();private: void ReportHighScore();}; //HighScoreDecorator.cpp #include StdAfx.h#include HighScoreDecorator.h#include iostreamusing 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 这次考试语文最高是75 数学是78 自然是80 endl;} //SortDecorator.h #pragma once#include reportdecorator.h#include ISchoolReport.hclass CSortDecorator : public CReportDecorator{public: CSortDecorator(ISchoolReport *psr); ~CSortDecorator(void); void Report();private: void ReportSort();};//SortDecorator.cpp #include StdAfx.h#include SortDecorator.h#include iostreamusing 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();} 这也是一个比较简单的模式属于行为型模式。 转载于:https://www.cnblogs.com/wanggary/archive/2011/04/18/2020254.html