设计模式C++学习笔记之十三(Decorator装饰模式)

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

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

时间: 2024-08-02 15:13:26

设计模式C++学习笔记之十三(Decorator装饰模式)的相关文章

《Head First 设计模式》学习笔记——适配器模式 + 外观模式

在ADO.NET中,对于我们从数据库中取出的数据都要放到一个DataSet中,不管你是Access的数据库,还是SQL的数据库,或者是Oracle的数据库都要放到DataSet中..NET中并没有提供如:SqlDataSet.OleDbDataSet.OracleDataSet等,它只提供了一种DataSet就是用SqlDataAdapte等去填充数据:为什么这一个DataSet能存放不同的数据呢?就是有这些适配器来适配.----题记 设计模式 适配器模式:将一个类的接口,转换成客户期待的另一个

《Head First 设计模式》学习笔记——模板方法模式

模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现.这就是模板方法模式的用意. 设计模式 模板方法模式:在一个方法中定义一个算法的框架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结果的情况下,重新定义算法中的某些步骤. 模板就是一个方法,这个方法将算法定义成一组步骤,其中的任何步骤都可以是抽象的,由子类负责实现.这样可以确

angular学习笔记(二十三)-$http(1)-api

之前说到的$http.get和$http.post,都是基于$http的快捷方式.下面来说说完整的$http: $http(config) $http接受一个json格式的参数config: config的格式如下: { method:字符串 , url:字符串, params:json对象, data:请求数据, headers:请求头, transformRequest:函数,转换post请求的数据的格式, transformResponse:函数,转换响应到的数据的格式, cache:布尔

《Head First 设计模式》学习笔记——策略模型

我们全都使用别人设计好的库与框架.我们讨论库与框架.利用他们的API编译成我们的程序.享受运用别人的代码所带来的长处.看看java api它所带来的功能:网络.GUI.IO等.库与框架长久以来,一直扮演着软件开发过程的重要角色,我们从中挑选所要的组件,把他们放进合适的地方.可是,库与框架无法帮助我们将应用组织成easy了解.easy维护.具有弹性的架构,所以要设计模式. 设计模式不会直接进入你的代码中,而是先进入你的"大脑"中.一旦你先在脑海中装入了很多关于模式的知识,就行開始新设计中

【Unity 3D】学习笔记三十三:游戏元素——天空盒子

天空盒子 一般的3D游戏都会有着北京百年一遇的蓝天,让人惊叹不已.其实天空这个效果没有什么神秘的只需用到天空盒子这个组件就行,可以将天空设想成一个巨大的盒子,这个盒子将整个游戏视图和所有的游戏元素包含其中.在unity中制作天空盒子非常方便,只需要像设置其他组件一样设置下就行了.在制作天空盒子之前,我们需要搜集一些天空的贴图,不过,unity很方便的为开发者提供了天空盒子的资源包.首先在project视图中点击鼠标右键,然后选择import package--skyboxes即可添加天空盒子的资

Android学习笔记(十三)——碎片(一)

碎片 碎片可看作另外一种形式的活动,可以创建碎片来包含视图. 碎片总是嵌入在活动中,一般有两种常见形式: 1.碎片A和碎片B分别处于不同的活动中,当选择碎片A中的某一项时,触发碎片B启动: 2.碎片A和碎片B处于同一个活动中,共享同一活动,以创建更佳的用户体验. 点此下载完整源码~(代码适用于本文章所讲) 1.创建一个名为"Fragments"的项目,在res/layout文件夹下,分别新建fragment1.xml.fragment2.xml:在当前包名下,分别新建Fragment1

VSTO 学习笔记(十三)谈谈VSTO项目的部署

原文:VSTO 学习笔记(十三)谈谈VSTO项目的部署 一般客户计算机专业水平不高,但是有一些Office水平相当了得,尤其对Excel的操作非常熟练.因此如果能将产品的一些功能集成在Office中,将会有很好的基础. 但是由于客户安装的Office版本不一,所以VSTO项目的部署问题显得尤为重要,需要考虑很多问题. 测试代码下载 本系列所有测试代码均在Visual Studio 2010 Ultimate SP1 + Office 2010 Professional Plus x64 SP1

UI学习笔记---第十三天可视化设计 XIB, StoryBoard

一.XIB Xib是一种苹果提供的快速构建界面的编程方式,主要是为了简化MVC中的V的构建 Xib提供可视化的编辑界面,能大大提升页面布局效率 Xib常用操作 为控件关联事件 为空间指定delegate 为控件关联实例变量或者属性 二.StoryBoard的使用 StoryBoard的注意事项 在AppDelegate的-application: didFinishLaunchingWithOptions: 的方法中不要再用代码初始化一个window 将创建好的StoryBoard在应用程序配置

【Unity 3D】学习笔记四十三:布料

布料 布料是特殊的组件,它可以变化成任意形状,比如说:随风飘的旗子,窗帘等 创建布料的方法有两种:创建布料对象,在游戏对象中添加布料组件.前者通过hierarchy视图中选择create--cloth即可,创建后,系统会自动将互动布料组件(interactive clothe)与布料渲染组件(cloth renderer)添加值该对象中.后者是在导航菜单中选component--physics--interactive cloth菜单项即可. 交互布料组件是由网格组成的布料,只要用于布料的逻辑判