Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorator design pattern, function bug

This is homework...I'm not asking for answers, I just have a bug I'm not sure what to do with. Thanks!

The bug in question probably has nothing to do with the assignment itself, but here is the assignment description anyways:

I'm working on an assignment (in C++) meant to teach use of the decorator design pattern through the classic example of a pizza with toppings. (My professor may as well have lifted it straight from http://simplestcodings.com/2010/12/26/decorator-design-pattern-example-ni-c/). I'm running into a little problem that I was wondering if someone could help me with.

I have a main menu(pizzeria) object that takes input from the user and performs the desired actions on a pizza. Users start with a basic pizza and then can add toppings to it until they're done. So the first thing that my "newPizza" function does is declare the new Pizza as a Plain, which is a subclass of abstract class Pizza.

Then they get to enter their choice of toppings. Each time, a pointer to the same Pizza object is sent to the addToppings() function, the new decoration is added, and the pointer is returned. Each decoration inherits from a price category, which inherits from pizzaToppings, which inherits from Pizza.

This is the relevant part of the main order function:

Pizza* Menu::newPizza()
{
cout << "\nNew Pizza";

//accept the next choice
int choose = 0;

//create the new pizza
Plain * currentPizza = new Plain();

//until they choose to end the order
while (choose != 3)
{
    //accept the choice
    cin >> choose;

    switch (choose)
    {
        //if they want to add a new topping
    case 1:
        {
            //add topping to current pizza
           //and this is where the problem is spotted by the compiler
            addTopping(currentPizza);
            break;
        }

The issue is that when I try to send the pointer currentPizza to the function addTopping(), I get "Run-Time Check Failure #3 - The variable 'currentPizza' is being used without being initialized."

Didn't I just initialize it right there on line 7?

If I hit "continue", the program keeps going, and works, but I get that same error every time I call the function. Is it just a syntax error somewhere, or do I have some actual issue here?

Thanks!!

[edit:]

The addTopping() function:

Pizza* Menu::addTopping(Pizza* thisPizza)
{
cout << "\nAdd topping";

//declare choose int
int choose = 0;

//accept number of topping
cin >> choose;

//decide which one to add
switch (choose)
{

//mozzarella
case 1:
    {
        thisPizza = new Mozzarella(thisPizza);
        break;
    }
//mushrooms
case 2:
    {
        thisPizza = new Mushrooms(thisPizza);
        break;
    }

//another 13 possible toppings, won't bore you with the details ;)

}

cout << "\nEnd add topping\n";

return thisPizza;
}
like image 209
BIU Avatar asked Jun 09 '11 10:06

BIU


People also ask

What problem is solved by the decorator pattern?

The problem of inheritance can be solved by using a decorator pattern where you can add functionality to an object at run time.

What is a disadvantage of the decorator pattern?

However, development using the decorator pattern does have some disadvantages. Using the pattern increases the complexity of the software. The decorator interface, in particular, usually contains a lot of code and is often linked to many terms, rendering it far from beginner friendly.

What is the purpose of a decorator pattern?

The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new Decorator class that wraps the original class.

How does the Decorator design pattern work?

Decorator patterns allow a user to add new functionality to an existing object without altering its structure. So, there is no change to the original class. The decorator design pattern is a structural pattern, which provides a wrapper to the existing class.


3 Answers

Do you have currentPizza also declared as an field of the Pizza class and you are using that somewhere else? If so, the currentPizza you are updating in newPizza is specific to that method, and you need to do just currentPizza = new Plain(); instead of declaring a new currentPizza variable in the scope of the method.

Also, in your addTopping method, you are only updating the argument thisPizza, which is a copy of the pointer currentPizza.

You need to do:

currentPizza = addTopping(currentPizza);
like image 82
MGwynne Avatar answered Oct 19 '22 19:10

MGwynne


If you pass in a pointer by value (which is what you're doing) it will take that pointer value and assign the new pizza to it. That value is not the same as that which is found in line 7 above. For instance:

int bar = new int(3);
void  doSomething(int *foo){ foo = new int(5); } //memory leak here
doSomething(bar);

bar is still 3. That's effectively what you're doing.

You want to pass in the pointer by reference:

void doSomething(int **foo){ delete *foo; *foo = new int(5); }

Update:

Seeing as you'd like a nested class structure, wherein class Child retains a record of a class Base in a polymorphic manner...

void doSomething(MyClass **foo){ *foo = new MyChildClass(*foo); }

I hope that as part of your definition within the child classes you've made sure you're handling the release of resources (i.e. the pointers) correctly. I'd suggest looking at incorporating a smart pointer but that might be more than you need for this assignment.

like image 26
wheaties Avatar answered Oct 19 '22 19:10

wheaties


One mistake is that in Menu::newPizza() you do not do this : currentPizza = addTopping(currentPizza);

Also you got a memory leak, since you create new object on heap, without deleting old.

By the way, sounds like a bad design return new pizza from addTopping method.

like image 1
BЈовић Avatar answered Oct 19 '22 18:10

BЈовић