0

So, I need to write an example code using the decorator dessign pattern and this is my example, it's very simple but idk why it doesn't print what I want. I'm new here, this is my first question so idk if im doing this in the right way but here's the code in C++:

include < iostream >

using namespace std;

class CafeAbstracto {
protected:
    int size;
    int precio;
public:

    void setsize(int n) {
        this->size = n;
        this->precio = n*10;
    }

    void setprecio(int p) {
        this->precio += p;
    }
    int getprecio(){
        return this->precio;
    }
    virtual void imprimirprecio(void) = 0;
};

class Cafe : public CafeAbstracto {
public:  

    void imprimirprecio() {
       int prec=getprecio();
        cout << "El precio del cafe es de: " << prec << endl;
    }
};

class CafeDecorator : public CafeAbstracto {
protected:

    CafeAbstracto *Cafe_con_extras;
public:

    CafeDecorator(CafeAbstracto *nuevo_cafe) {
        Cafe_con_extras = nuevo_cafe;
    }
    virtual void imprimirprecio()=0;
};

class Leche : public CafeDecorator {
public:

    Leche(CafeAbstracto* nuevo_cafe, bool tiene) : CafeDecorator(nuevo_cafe) {
    }

     void imprimirprecio() {        
        cout << "El cafe tiene leche" << endl;
        Cafe_con_extras->setprecio(5240);
        Cafe_con_extras->imprimirprecio();
    }
};

class Azucar : public CafeDecorator {
public:

    Azucar(CafeAbstracto* nuevo_cafe, bool tiene) : CafeDecorator(nuevo_cafe) {
    }

    virtual void imprimirprecio() {
        Cafe_con_extras->setprecio(50);
        cout << "El cafe tiene azucar" << endl;
        Cafe_con_extras->imprimirprecio();
    }
};

class Descafeinado : public CafeDecorator {
public:

    Descafeinado(CafeAbstracto* nuevo_cafe, bool es) : CafeDecorator(nuevo_cafe) {
    }
    void imprimirprecio() { 
        Cafe_con_extras->setprecio(100);
        cout << "El cafe es descafeinado" << endl;
        Cafe_con_extras->imprimirprecio();
    }
};

int main(int argc, char *argv[]) {

    Cafe *coffee = new Cafe();
    coffee->setsize(6);
    Descafeinado *cafedescaf = new Descafeinado(coffee, true);
    cafedescaf->imprimirprecio();

    Leche *descafyconleche = new Leche(cafedescaf, true);
    descafyconleche->imprimirprecio();
    Azucar *cafe_des_leche= new Azucar(descafyconleche, true);
    cafe_des_leche->imprimirprecio();

    delete coffee;
    delete cafedescaf;
    delete descafyconleche;
    delete cafe_des_leche;

    return (0);

}

this is the output

El cafe es descafeinado El precio del cafe es de: 160 //this one is correct

El cafe tiene leche El cafe es descafeinado El precio del cafe es de: 260 //incorrect

El cafe tiene azucar El cafe tiene leche El cafe es descafeinado El precio del cafe es de: 360 //incorrect

shengy
  • 9,461
  • 4
  • 37
  • 61

1 Answers1

1
Leche *descafyconleche = new Leche(cafedescaf, true);
descafyconleche->imprimirprecio();
Azucar *cafe_des_leche= new Azucar(descafyconleche, true);
cafe_des_leche->imprimirprecio();

should be:

Leche *descafyconleche = new Leche(coffee, true);
descafyconleche->imprimirprecio();
Azucar *cafe_des_leche= new Azucar(coffee, true);
cafe_des_leche->imprimirprecio();

Because you need to use the decorator to decorate your coffe (your concrete component), not to decorate another decorator.

shengy
  • 9,461
  • 4
  • 37
  • 61