//修饰模式
//这个是基于类的修饰。作者用父亲看成绩,然后签字来举例
//需要对成绩进行修饰才能,让父亲开心,然后签字。
//抽象修饰类
class SchoolReport {
public:
SchoolReport();
virtual void Report() {}//家长看报告
virtual void Sign(string name) {}//家长签字
};
class FouthGradeSchoolReport : public SchoolReport {
public:
void Report() {
cout << "dear xxx";
cout << "math 70 Languages 75 Physical 80";
cout << "sign: ";
}
void Sign(string name) {
cout << name << "sign";
}
};
class Decorator : public SchoolReport {
protected:
SchoolReport* m_pSchool;
public:
Decorator(SchoolReport* psc) {
m_pSchool = psc;
}
void Report() {
m_pSchool->Report();
}
void Sign(string name) {
m_pSchool->Sign(name);
}
};
class HighScoreDecorator : public Decorator {
public:
HighScoreDecorator(SchoolReport* psc): Decorator(psc) {
}
private:
void ReportHighScore() {
cout << "the most score subject" << endl;
}
public:
void Report() {
ReportHighScore();
m_pSchool->Report();
}
};
class SortDecorator : public Decorator {
public:
SortDecorator(SchoolReport* psc): Decorator(psc) {}
private:
void ReportSort() {
cout << "my ranklist";
}
void Report() {
m_pSchool->Report();
ReportSort();
}
};
class Father {
static void main() {
SchoolReport* pscr = new FouthGradeSchoolReport;
pscr = new HighScoreDecorator(pscr);
pscr = new SortDecorator(pscr);
pscr->Report();
pscr->Sign("da");
}
};
//核心:具体构件(要修饰的东西)
//:修饰角色(实现接口等的方法,必有private 的抽象构件)
//:具体修饰角色
设计模式观后(c++还原之十四 修饰模式)
最新推荐文章于 2022-07-25 15:24:55 发布