vendredi 22 mai 2015

Is the Decorator Pattern a suitable choice here?

Consider the code below. Through A::doit(), a B object is supposed to increase total by 3. A Decorated1 object is supposed to increase total by 4, and a Decorated2 object is supposed to increase total by 5. An A object that is a combination of these derived types shall still carry out their "special actions" but is to increase total by the max (not sum) of the individual increases in total. But the decorator pattern is obtaining the sum instead of the max. Do I have to abandon the Decorator Pattern here?

#include <iostream>

int total = 0;

struct A {
public:
    virtual void doIt() = 0;
};

struct Decorator : public A {
    A* a;
    Decorator (A* a_) : a(a_) {}
    virtual void doIt() override {a->doIt();}
};

struct B : public A {
    virtual void doIt() override {
        total += 3;
        std::cout << "Special actions by B carried out.\n";
    }
};

struct Decorated1 : public Decorator {
    using Decorator::Decorator;
    virtual void doIt() override {
        Decorator::doIt();
        total += 4;
        std::cout << "Special actions by Decorated1 carried out.\n";
    }
};

struct Decorated2 : public Decorator {
    using Decorator::Decorator;
    virtual void doIt() override {
        Decorator::doIt();
        total += 5;
        std::cout << "Special actions by Decorated2 carried out.\n";
    }
};

int main() {
    A* decorated1_2 = new Decorated2(new Decorated1(new B));
    decorated1_2->doIt();
    std::cout << "total = " << total << std::endl;
}

Output:

Special actions by B carried out.  // Good I want this.
Special actions by Decorated1 carried out.  // Good I want this.
Special actions by Decorated2 carried out.  // Good I want this.
total = 12  // No, it is supposed to be 5, not the sum 3+4+5.

Aucun commentaire:

Enregistrer un commentaire