结构型模式のDecorator装饰模式

装饰模式,动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

Decorator提供了一种给类增加职责的方法,不是通过继承实现的,而是通过组合。(优先使用组合而非继承)

13.1.解释

main(),老爸

ISchoolReport,成绩单接口

CFourthGradeSchoolReport,四年级成绩单

ReportDecorator,成绩单装饰器基类

HighScoreDecorator,最高分装饰器

SortDecorator,班级排名装饰器

说明:对“四年级成绩单”进行装饰,ReportDecorator必然有一个private变量指向ISchoolReport。

注意:

看代码:

 1 // Decorator.cpp//主程序
 2 #include "stdafx.h"
 3 #include "ISchoolReport.h"
 4 #include "FouthGradeSchoolReport.h"
 5 #include "SugarFouthGradeSchoolReport.h"
 6 #include "HighScoreDecorator.h"
 7 #include "SortDecorator.h"
 8 #include <iostream>
 9 using std::cout;
10 using std::endl;
11 void DoIt()
12 {
13     ISchoolReport *psr = new CFouthGradeSchoolReport();
14     psr->Report();//看成绩单
15     psr->Sign("老三");//很开心,就签字了
16     delete psr;
17 }
18 void DoNew()
19 {
20     cout << "----------分部分进行装饰----------" << endl;
21     ISchoolReport *psr = new CFouthGradeSchoolReport();//原装成绩单
22     //
23     ISchoolReport *pssr = new CSortDecorator(psr);//又加了成绩排名的说明
24     ISchoolReport *phsr = new CHighScoreDecorator(pssr);//加了最高分说明的成绩单
25     phsr->Report();//看成绩单
26     phsr->Sign("老三");//很开心,就签字了
27    
28     //先装饰哪个不重要,顺序已经在装饰内部确定好,但一定要调用最后一个装饰器的接口。
29     //ISchoolReport *phsr = new CHighScoreDecorator(psr);//加了最高分说明的成绩单
30     //ISchoolReport *pssr = new CSortDecorator(phsr);//又加了成绩排名的说明
31     //pssr->Report();//看成绩单
32     //pssr->Sign("老三");//很开心,就签字了
33 
34     delete pssr;
35     delete phsr;
36     delete psr;
37 }
38 int _tmain(int argc, _TCHAR* argv[])
39 {
40     //在装饰之前,可以用继承的办法,来进行简单的修饰
41     DoIt();
42 
43     //但如果需要修饰的项目太多呢?或者装饰的项目不是固定的,继承显然会变得更复杂
44     DoNew();
45 
46     _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
47     _CrtDumpMemoryLeaks();
48     return 0;
49 }
 1 //ISchoolReport.h
 2 
 3 #pragma once
 4 #include <iostream>
 5 using std::string;
 6 class ISchoolReport
 7 {
 8 public:
 9     ISchoolReport(void)
10     {
11     }
12     virtual ~ISchoolReport(void)
13     {
14     }
15     virtual void Report() = 0;
16     virtual void Sign(string name) = 0;
17 };

 1 //FouthGradeSchoolReport.h
 2 
 3 #pragma once
 4 #include "ischoolreport.h"
 5 class CFouthGradeSchoolReport :
 6     public ISchoolReport
 7 {
 8 public:
 9     CFouthGradeSchoolReport(void);
10     ~CFouthGradeSchoolReport(void);
11     void Report();
12     void Sign(string name);
13 };
14 
15 //FouthGradeSchoolReport.cpp
16 
17 #include "StdAfx.h"
18 #include "FouthGradeSchoolReport.h"
19 #include <iostream>
20 using std::cout;
21 using std::endl;
22 using std::string;
23 CFouthGradeSchoolReport::CFouthGradeSchoolReport(void)
24 {
25 }
26 CFouthGradeSchoolReport::~CFouthGradeSchoolReport(void)
27 {
28 }
29 void CFouthGradeSchoolReport::Report()
30 {
31     cout << "尊敬的XXX家长:" << endl;
32     cout << "......" << endl;
33     cout << "语文62  数学65  体育98  自然63" << endl;
34     cout << "......" << endl;
35     cout << "                家长签名:" << endl;
36 }
37 void CFouthGradeSchoolReport::Sign(string name)
38 {
39     cout << "家长签名为:" << name.c_str() << endl;
40 }
 1 //ReportDecorator.h
 2 
 3 #pragma once
 4 #include "ischoolreport.h"
 5 class CReportDecorator :
 6     public ISchoolReport
 7 {
 8 public:
 9     CReportDecorator(ISchoolReport *psr);
10     virtual ~CReportDecorator(void);
11     void Report();
12     void Sign(string name);
13 private:
14     ISchoolReport *m_pSchoolReport;
15 };
16 
17 //ReportDecorator.cpp
18 
19 #include "StdAfx.h"
20 #include "ReportDecorator.h"
21 #include <iostream>
22 using std::string;
23 CReportDecorator::CReportDecorator(ISchoolReport *psr)
24 {
25     this->m_pSchoolReport = psr;
26 }
27 CReportDecorator::~CReportDecorator(void)
28 {
29 }
30 void CReportDecorator::Report()
31 {
32     this->m_pSchoolReport->Report();
33 }
34 void CReportDecorator::Sign( string name )
35 {
36     this->m_pSchoolReport->Sign(name);
37 }
 1 //HighScoreDecorator.h
 2 
 3 #pragma once
 4 #include "reportdecorator.h"
 5 #include "ISchoolReport.h"
 6 class CHighScoreDecorator :
 7     public CReportDecorator
 8 {
 9 public:
10     CHighScoreDecorator(ISchoolReport *psr);
11     ~CHighScoreDecorator(void);
12     void Report();
13 private:
14     void ReportHighScore();
15 };
16 
17 //HighScoreDecorator.cpp
18 
19 #include "StdAfx.h"
20 #include "HighScoreDecorator.h"
21 #include <iostream>
22 using std::cout;
23 using std::endl;
24 CHighScoreDecorator::CHighScoreDecorator( ISchoolReport *psr ) : CReportDecorator(psr)
25 {
26 }
27 CHighScoreDecorator::~CHighScoreDecorator(void)
28 {
29 }
30 void CHighScoreDecorator::Report()
31 {
32     this->ReportHighScore();
33     this->CReportDecorator::Report();
34 }
35 void CHighScoreDecorator::ReportHighScore()
36 {
37     cout << "这次考试语文最高是75, 数学是78, 自然是80" << endl;
38 }
 1 //SortDecorator.h
 2 
 3 #pragma once
 4 #include "reportdecorator.h"
 5 #include "ISchoolReport.h"
 6 class CSortDecorator :
 7     public CReportDecorator
 8 {
 9 public:
10     CSortDecorator(ISchoolReport *psr);
11     ~CSortDecorator(void);
12     void Report();
13 private:
14     void ReportSort();
15 };
16 //SortDecorator.cpp
17 
18 #include "StdAfx.h"
19 #include "SortDecorator.h"
20 #include <iostream>
21 using std::cout;
22 using std::endl;
23 CSortDecorator::CSortDecorator( ISchoolReport *psr ) : CReportDecorator(psr)
24 {
25 }
26 CSortDecorator::~CSortDecorator(void)
27 {
28 }
29 void CSortDecorator::ReportSort()
30 {
31     cout << "我是排名第38名..." << endl;
32 }
33 void CSortDecorator::Report()
34 {
35     this->CReportDecorator::Report();
36     this->ReportSort();
37 }

这也是一个比较简单的模式,属于行为型模式。

本文转自:~~~

原文地址:https://www.cnblogs.com/labi/p/3601978.html