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

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

    原文:VSTO 学习笔记(十三)谈谈VSTO项目的部署 一般客户计算机专业水平不高,但是有一些Office水平相当了得,尤其对Excel的操作非常熟练.因此如果能将产品的一些功能集成在Office中, ...

  2. Python学习笔记(十三)

    Python学习笔记(十三): 模块 包 if name == main 软件目录结构规范 作业-ATM+购物商城程序 1. 模块 1. 模块导入方法 import 语句 import module1 ...

  3. python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码

    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...

  4. 《Head first设计模式》学习笔记 – 迭代器模式

    <Head first设计模式>学习笔记 – 迭代器模式 代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 爆炸性新闻:对象村餐厅和对象村煎饼屋合并了!真是个 ...

  5. Android学习笔记(十三)——广播机制

     //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容 ...

  6. Dynamic CRM 2013学习笔记(十三)附件上传 / 上传附件

    上传附件可能是CRM里比较常用的一个需求了,本文将介绍如何在CRM里实现附件的上传.显示及下载.包括以下几个步骤: 附件上传的web页面 附件显示及下载的附件实体 调用上传web页面的JS文件 实体上 ...

  7. JavaScript学习笔记(十三)——生成器(generator)

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...

  8. ASP.NET Core 2 学习笔记(十三)Swagger

    Swagger也算是行之有年的API文件生成器,只要在API上使用C#的<summary />文件注解标签,就可以产生精美的线上文件,并且对RESTful API有良好的支持.不仅支持生成 ...

  9. 【转】Pro Android学习笔记(十三):用户界面和控制(1):UI开发

    目录(?)[-] UI开发 方式一通过XML文件 方式二通过代码 方式三XML代码 UI开发 先理清一些UI概念: view.widget.control:这三个名词其实没有什么区别,都是一个UI元素 ...

随机推荐

  1. java 中数据的强制转换 和计算的补码运算

    原码 反码 补码的定义与运算 1原码: 原码是将十进制或者其他进制的数转换为二进制表示(且要根据数据的类型转换) 如:130 (默认是Int类型,则是4个字节) 原码是:00000000 000000 ...

  2. nfs环境搭建报错clnt_create: RPC: Program not registered

    有时候搭建完成后,使用showmount -e ip检测服务端服务器情况的是,会出现clnt_create: RPC: Program not registered 这个错误,表示rpc程序为注册成功 ...

  3. python 线程(创建2种方式,锁,死锁,递归锁,GIL锁,守护进程)

    ###############总结############ 线程创建的2种方式(重点) 进程:资源分配单位    线程:cpu执行单位(实体) 线程的创建和销毁的开销特别小 线程之间资源共享,是同一个 ...

  4. POJ - 2057 The Lost House(树形DP+贪心)

    https://vjudge.net/problem/POJ-2057 题意 有一只蜗牛爬上某个树枝末睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面,.现在这只蜗牛要求寻找 ...

  5. IO流--与properties集合配合使用

    IO流--与properties集合配合使用: 注:生产上主要用于常量文件的配置,读取常量文件: 1:properties集合的放值与取值: /* * properties集合继承自hashTable ...

  6. python要点

    1.数据类型 字符串: ''或""表示单行,写三对符合表示多行, r'这种写法转义符不生效' 布尔值:True, False 空值: None 类型转换 print ), ), b ...

  7. 安装Blend+SketchFlow Preview for Visual Studio 2012出现错误

    安装Blend+SketchFlow Preview for Visual Studio 2012出现如下错误: 首先是这个网址:http://msdn.microsoft.com/en-us/exp ...

  8. GCC编译器原理(一)------GCC 工具:addr2line、ar、as、c++filt和elfedit

    1.3 GCC 工具 1.3.1 binutils 工具集 工具 描述 addr2line 给出一个可执行文件的内部地址,addr2line 使用文件中的调试信息将地址翻译成源代码文件名和行号. ar ...

  9. 二十五、Linux 进程与信号---exec函数

    25.1 介绍 在用 fork 函数创建子进程后,子进程往往要调用一种 exec 函数以执行另一个程序 当进程调用一种 exec 函数时,该进程完全由新程序代换,替换原有进程的正文,而新程序则从其 m ...

  10. win10和Ubuntu双系统安装过程中遇到的问题

    1.安装过程 注意分区问题,很重要 https://blog.csdn.net/baobei0112/article/details/77996570 https://blog.csdn.net/s7 ...