装饰模式,动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
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 <iostream>
using 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 <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家长:" << 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.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 << "这次考试语文最高是75, 数学是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();
}
这也是一个比较简单的模式,属于行为型模式。