#include <iostream>
#include <string>
using namespace std;
// interface Beverage
class Beverage {
public:
virtual string getDescription() {
if (description == "") {
description = "Unknown Beverage";
}
return description;
}
virtual double cost() = 0;
protected:
string description;
};
// interface CondimentDecorator externs Beverage
class CondimentDecorator : public Beverage {
public:
virtual string getDescription() = 0;
};
// implements Benverage
class Espresso : public Beverage {
public:
Espresso() {
description = "Espresso";
}
double cost() {
return 1.99;
}
};
class HouseBlend : public Beverage {
public:
HouseBlend() {
description = "HouseBlend Coffee";
}
double cost() {
return 0.89;
}
};
// implements CondimentDecorator
class Mocha : public CondimentDecorator {
public:
Mocha(Beverage *beverage) : beverage(beverage) {
}
string getDescription() {
return beverage->getDescription() + ", Mocha";
}
double cost() {
return 0.20 + beverage->cost();
}
protected:
Beverage *beverage;
};
class Soy : public CondimentDecorator {
public:
Soy(Beverage *beverage) : beverage(beverage) {
}
string getDescription() {
return beverage->getDescription() + ", Soy";
}
double cost() {
return 0.11 + beverage->cost();
}
protected:
Beverage *beverage;
};
class Whip : public CondimentDecorator {
public:
Whip(Beverage *beverage) : beverage(beverage) {
}
string getDescription() {
return beverage->getDescription() + ", Whip";
}
double cost() {
return 0.5 + beverage->cost();
}
protected:
Beverage *beverage;
};
// Example
int main(int argc, char **argv) {
Beverage *beverage = new Espresso();
cout << beverage->getDescription() << " $" << beverage->cost() << endl;
Beverage *beverage2 = new HouseBlend();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
cout << beverage2->getDescription() << " $" << beverage2->cost() << endl;
return 0;
}