1. 程式人生 > >設計模式C++學習筆記之十三(Decorator裝飾模式)

設計模式C++學習筆記之十三(Decorator裝飾模式)

com img c++ 進行 done 設計 out set 筆記

裝飾模式,動態地給一個對象添加一些額外的職責。就增加功能來說,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 <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();
}

技術分享

這也是一個比較簡單的模式,屬於行為型模式。

設計模式C++學習筆記之十三(Decorator裝飾模式)