【发布时间】:2010-06-07 08:41:14
【问题描述】:
谁能给我一个 C++ 中装饰器设计模式的例子? 我遇到过它的 Java 版本,但发现很难理解它的 C++ 版本(从我找到的示例中)。
谢谢。
【问题讨论】:
标签: c++ design-patterns decorator
谁能给我一个 C++ 中装饰器设计模式的例子? 我遇到过它的 Java 版本,但发现很难理解它的 C++ 版本(从我找到的示例中)。
谢谢。
【问题讨论】:
标签: c++ design-patterns decorator
Vince Huston Design Patterns,尽管它的布局很糟糕,但对于四人帮一书中的大多数设计模式都有 C++ 实现。
点击Decorator。
与 Java 没有太大区别,除了你最好用智能指针包装的手动内存处理:)
【讨论】:
我发现Sourcemaking 网站对于explaining different Design Patterns 来说是一个相当不错的网站。
Decorator 设计模式具有 C++ 示例,例如 overview example、“before and after”和 example with packet encoding/decoding。
【讨论】:
#include <iostream>
using namespace std;
class Computer
{
public:
virtual void display()
{
cout << "I am a computer..." << endl;
}
};
class CDDrive : public Computer
{
private:
Computer* c;
public:
CDDrive(Computer* _c)
{
c = _c;
}
void display()
{
c->display();
cout << "with a CD Drive..." << endl;
}
};
class Printer : public Computer
{
private:
CDDrive* d;
public:
Printer(CDDrive* _d)
{
d = _d;
}
void display()
{
d->display();
cout << "with a printer..." << endl;
}
};
int main()
{
Computer* c = new Computer();
CDDrive* d = new CDDrive(c);
Printer* p = new Printer(d);
p->display();
}
【讨论】:
我最近在学习c++和设计模式,我举一个来自Head First Design Pattern>的例子。
main.cpp 在这里:
#include <iostream>
#include "Decaf.h"
#include "Espresso.h"
#include "MochaDecorator.h"
#include "MilkDecorator.h"
using std::cout;
using std::endl;
int main(){
cout << "---------------------------" << endl;
// only Decaf, no addsOn
Beverage* decafOnly = new Decaf();
cout << decafOnly->getDescription() << endl;
cout << decafOnly->cost() << endl;
delete decafOnly;
cout << "---------------------------" << endl;
// Decaf + Mocha
Beverage* decaf = new Decaf();
MochaDecorator mocha_decaf = MochaDecorator(decaf);
cout << mocha_decaf.getDescription() << endl;
cout << mocha_decaf.cost() << endl;
delete decaf;
cout << "---------------------------" << endl;
// Espresso + Milk
Beverage* espresso = new Espresso();
MilkDecorator milk_espresso = MilkDecorator(espresso);
cout << milk_espresso.getDescription() << endl;
cout << milk_espresso.cost() << endl;
delete espresso;
cout << "---------------------------" << endl;
// Note: decorators can wrap not only components but the other decorators as well
// Esresso + Mocha + Milk + Milk, method 1-------------------------------
// Beverage* espresso2 = new Espresso(); // Espresso
// MochaDecorator* mocha_espresso = new MochaDecorator(espresso2); // Espresso + Mocha
// MilkDecorator* milk_deco = new MilkDecorator(mocha_espresso); // Espresso + Mocha + Milk
// MilkDecorator* milk_deco2 = new MilkDecorator(milk_deco); // Espresso + Mocha + Milk + Milk
// cout << milk_deco2->getDescription() << endl;
// cout << milk_deco2->cost() << endl;
// delete espresso2;
// delete mocha_espresso;
// delete milk_deco;
// delete milk_deco2;
// Esresso + Mocha + Milk + Milk, method 2------------------------------
Beverage* espresso2 = new MilkDecorator(new MilkDecorator(new MochaDecorator(new Espresso())));
cout << espresso2->getDescription() << endl;
cout << espresso2->cost() << endl;
delete espresso2;
cout << "---------------------------" << endl;
return 0;
}
其他文件请看我的github:
https://github.com/jwbecalm/Head-First-Design-Patterns-in-CPP/tree/main/ch03_Decorator
【讨论】: