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

來源:互聯網
上載者:User

標籤:xxx   char   主程   cpp   iostream   設計   行為型   leak   one   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.