博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式C++学习笔记之十三(Decorator装饰模式)
阅读量:5173 次
发布时间:2019-06-13

本文共 4587 字,大约阅读时间需要 15 分钟。

装饰模式,动态地给一个对象添加一些额外的职责。就增加功能来说,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();
}

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

转载于:https://www.cnblogs.com/D-DZDD/p/7308291.html

你可能感兴趣的文章
泥塑课
查看>>
setImageBitmap和setImageResource
查看>>
springMVC4 注解配置实例
查看>>
单片机编程
查看>>
Filter in Servlet
查看>>
Linux--SquashFS
查看>>
Application Pool Identities
查看>>
2017-3-24 开通博客园
查看>>
【MySQL性能优化】MySQL常见SQL错误用法
查看>>
Vue2全家桶之一:vue-cli(vue脚手架)超详细教程
查看>>
Struts 2 常用技术
查看>>
树形DP
查看>>
python flask解决上传下载的问题
查看>>
语法测试
查看>>
CES1
查看>>
java webcontroller访问时报415错误
查看>>
qcow2、raw、vmdk等镜像格式
查看>>
Jzoj5455【NOIP2017提高A组冲刺11.6】拆网线
查看>>
特定字符序列的判断(1028)
查看>>
华为面试
查看>>