Skip to content

Commit

Permalink
pattern "Decorator" use smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudryashov Dmitrii committed Nov 5, 2021
1 parent 5bb2887 commit 34eab46
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions Decorator/decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ class Espresso : public Beverage {

class CondimentDecorator : public Beverage {
protected:
Beverage* beverage_ = nullptr;
unique_ptr<Beverage> beverage_;
};

class Milk : public CondimentDecorator {
public:
Milk(Beverage* beverage) {
beverage_ = beverage;
Milk(unique_ptr<Beverage> beverage) {
beverage_ = move(beverage);
}

string getDescription() override {
Expand All @@ -83,8 +83,8 @@ class Milk : public CondimentDecorator {

class Mocha : public CondimentDecorator {
public:
Mocha(Beverage* beverage) {
beverage_ = beverage;
Mocha(unique_ptr<Beverage> beverage) {
beverage_ = move(beverage);
}

string getDescription() override {
Expand All @@ -98,8 +98,8 @@ class Mocha : public CondimentDecorator {

class Whip : public CondimentDecorator {
public:
Whip(Beverage* beverage) {
beverage_ = beverage;
Whip(unique_ptr<Beverage> beverage) {
beverage_ = move(beverage);
}

string getDescription() override {
Expand All @@ -113,8 +113,8 @@ class Whip : public CondimentDecorator {

class Soy : public CondimentDecorator {
public:
Soy(Beverage* beverage) {
beverage_ = beverage;
Soy(unique_ptr<Beverage> beverage) {
beverage_ = move(beverage);
}

string getDescription() override {
Expand All @@ -126,6 +126,25 @@ class Soy : public CondimentDecorator {
}
};

class Caramel : public CondimentDecorator {
public:
Caramel(unique_ptr<Beverage> beverage) {
beverage_ = move(beverage);
}

string getDescription() override {
return beverage_->getDescription() + string(", Caramel");
}

float cost() override {
return beverage_->cost() + 0.15f;
}
};

#define M_MakeEspresso make_unique<Espresso>()
#define M_AddMocha(X) make_unique<Mocha>(X)
#define M_AddMilk(X) make_unique<Milk>(X)

int main() {
vector<unique_ptr<Beverage>> orders;

Expand All @@ -134,18 +153,13 @@ int main() {
orders.push_back(make_unique<HouseBlend>());
orders.push_back(make_unique<DarkRoast>());

unique_ptr<Beverage> house_blend = make_unique<HouseBlend>();
orders.push_back(make_unique<Milk>(house_blend.release()));

house_blend = make_unique<HouseBlend>();
orders.push_back(make_unique<Whip>(house_blend.release()));

house_blend = make_unique<HouseBlend>();
orders.push_back(make_unique<Mocha>(house_blend.release()));
orders.push_back(make_unique<Milk>(make_unique<HouseBlend>()));
orders.push_back(make_unique<Whip>(make_unique<Decaf>()));
orders.push_back(make_unique<Mocha>(make_unique<HouseBlend>()));

house_blend = make_unique<HouseBlend>();
unique_ptr<Beverage> house_blend_w_mocha = make_unique<Mocha>(house_blend.release());
orders.push_back(make_unique<Mocha>(house_blend_w_mocha.release()));
orders.push_back(make_unique<Milk>(make_unique<Mocha>(make_unique<Espresso>())));
// or with macro
orders.push_back(M_AddMilk(M_AddMocha(M_MakeEspresso)));

for (const auto& beverage : orders) {
cout << beverage->getDescription() << '\t' << beverage->cost() << '\n';
Expand Down

0 comments on commit 34eab46

Please sign in to comment.