Decorating mode, dynamically adding some additional responsibilities to an object. For added functionality, the decorator mode is more flexible than generating subclasses.
13.1. Explanation
Main (), dad
Ischoolreport, Transcript interface
Cfourthgradeschoolreport, Grade four transcripts
Reportdecorator, transcript decorator base class
Highscoredecorator, top point decorator
Sortdecorator, class ranking decorator
Description: To decorate the "four grade transcripts", Reportdecorator must have a private variable pointing to Ischoolreport.
Attention:
Look at the code:
decorator.cpp//Main Program
#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 ();//See Transcripts
Psr->sign ("Old Three");//Very happy, signed.
Delete PSR;
}
void Donew ()
{
cout << "----------part of the decoration----------" << Endl;
Ischoolreport *PSR = new Cfouthgradeschoolreport ();//Original transcript
//
Ischoolreport *PSSR = new Csortdecorator (PSR);//Add a note to the rank of the score
Ischoolreport *PHSR = new Chighscoredecorator (PSSR);//Report card with highest score
Phsr->report ();//See Transcripts
Phsr->sign ("Old Three");//Very happy, signed.
First decorate which is not important, the order has been in the interior of the decoration OK, but be sure to call the last adorner interface.
Ischoolreport *PHSR = new Chighscoredecorator (PSR);//transcripts with the highest score indicated
Ischoolreport *PSSR = new Csortdecorator (PHSR);//Add a note on the rank of the score
Pssr->report ();//See Transcripts
Pssr->sign ("Old Three");//Very happy, signed.
Delete PSSR;
Delete PHSR;
Delete PSR;
}
int _tmain (int argc, _tchar* argv[])
{
Before decorating, you can use the inheritance method to perform simple retouching.
DoIt ();
But what if there are too many items to decorate? or decorative items are not fixed, succession will obviously become more complex
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 << "Dear xxx Parents:" << Endl;
cout << "..." << Endl;
cout << "Chinese 62 Mathematics 65 Sport 98 Natural" << Endl;
cout << "..." << Endl;
cout << "Parent signature:" << Endl;
}
void Cfouthgradeschoolreport::sign (string name)
{
cout << "Parent signature:" << 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 << "The test language is the highest of 75, mathematics is 78, nature is the" << 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 << "I am the 38th place ..." << Endl;
}
void Csortdecorator::report ()
{
This->creportdecorator::report ();
This->reportsort ();
}
This is also a relatively simple pattern, which belongs to the behavioral pattern.
Design mode C + + Learning Note 13 (Decorator decoration mode)