Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'll paste my code below:

C++
#include<iostream>
using namespace std;

class Smoothy{
private:
    int price;
public:
    int getPrice(){
        return price;
    }
    virtual ~Smoothy() = default;
    virtual string description() = 0;
};

class BasicSmoothy: public Smoothy{
private:
    string nume;
public:
    BasicSmoothy(string n): nume(n){}
    string description(){
        return nume;
    }
};

class SmoothyDecorator:public Smoothy{
private:
    Smoothy *b;

public:
    SmoothyDecorator(Smoothy* bb){
      b = bb;
    }
    ~SmoothyDecorator(){
        delete b;
    }
    string description(){
        return b->description();
    }
};

class SmoothyWithCream:public SmoothyDecorator{
public:
    SmoothyWithCream(Smoothy *b):SmoothyDecorator(b){

    }
    string description(){

        return SmoothyDecorator::description() + " with Cream!";
    }
};

class SmoothyWithCinnamon:public SmoothyDecorator{
public:
    SmoothyWithCinnamon(Smoothy *b):SmoothyDecorator(b){

    }
    string description(){
        return SmoothyDecorator::description() + " with Cinnamon!";

    }
};

int main(){
     Smoothy* b = new SmoothyWithCinnamon(new BasicSmoothy("Kiwi"));
     cout<<b->description();

}



I implemented a decorator pattern, based on an UML diagram. The diagram specifies that the BasicSmoothy has two types, with two specific prices(Kiwi 10$, Strawberry 12$) and the Derived classes each add 2$ and 3$ to the final listed price.

What I have tried:

What I'm trying to do, is add a Smoothy object(Kiwi or Strawberry) and add Cream or Cinnamon, based on that, my program should output the final price. My question is, how would I add or modify the price, given that I'm not allowed to make the getPrice() function virtual, and the price integer is private. If price were protected, it would be quite easy to modify in each of the derived classes, but since it's private, I have no idea how to modify it.

Appreciate any help!
Posted
Updated 11-Jul-19 5:28am
Comments
Stefan_Lang 12-Jul-19 5:52am    
Given the specification and, most importantly, the restrictions you have, there is no (clean) way at all to set the price as there is no setter and no initializer and no access at all other than reading.

If you're looking for *any* solution no matter how bad, you can of course do some dirty tricks that effectively access the memory holding the price. But I have a feeling you're not supposed to do that either

It's like someone locked the car, threw away the keys, and asks you to get in without breaking a window, a lock, or any laws at all.

So, please, check that specification and UML diagram again to make sure you got it right, or show that diagram and full specifications to us, so we can find out what you're missing. At worst, we can confirm this isn't possible and tell you to go to whoever gave you that task and ask him to review all the (pointless IMHO) restrictions.

1 solution

If you can't use virtual functions, where's your point in using the decorator pattern??

Just store the description and price of each ingredient in a simple struct and create a list or std::vector to hold the ingredients. Problem solved.

If you have a specific reason to use the decorator pattern, please tell us
(a) why do you need it?
(b) why can't you use a virtual function?
 
Share this answer
 
Comments
Member 13608791 11-Jul-19 13:48pm    
I am allowed to use virtual functions( string description() is virtual), but I am not allowed to make the getPrice() function virtual. The only reason I'm using the decorator pattern is because the UML diagram I'm provided matches it perfectly.

I have some constraints written besides my diagram:

I can only use the functions and variables written in the UML diagram and I am NOT to change their visibility(make them protected/public) and I am not to use friend classes. Meaning that I can't change getPrice() from a normal function to a virtual one and I can't add any other functions/variables, other than the ones listed in my code.

From what I understand, there should be a way of assigning the price by using constructors, but I'm not exactly sure how.
Stefan_Lang 12-Jul-19 5:12am    
"I can only use the functions and variables written in the UML diagram"

Then you should show us that diagram! I'm not going to spend half an hour writing down a possible solution that would work only to find out you can't use it.
Stefan_Lang 12-Jul-19 5:17am    
You still haven't told us the why for ony of the restrictions you've mentioned:

- If it's some kind of homework assignment, it would be useful to see the full text of that assignment.

- If it's a restriction imposed by a third party library that you can't change, it would be useful to know which parts are within that library, and which are not (and thus can be changed).

- if it's a customer for a software project you are to develop, and he is imposing these restrictions on you, it would be useful to understand the motivation behind it.
Stefan_Lang 12-Jul-19 5:29am    
Oh, and just as an aside, if you can only use what's in the diagram, and the code you've shown contains all that, there's no way to SET the price - it's private and initialized with an unknown random value!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900