Builder设计模式对我不起作用

时间:2015-11-27 15:29:06

标签: c++ builder

我刚才写的c ++代码有问题。代码是Builder设计模式的示例。我创建了一个抽象构建器类,以及从这个类继承的两个类:MonsterBuilder和RuffianBuilder。我创建了一个Builder类,这个类接收一个Monster或一个RuffianBuilder,并构造这些类的新实例。问题出现在这里:如果MonsterBuilder类用于构建新实例,程序将终止并显示错误(a.exe已停止工作)。如果Builder收到RuffianBuilder,它会构造一个没有错误的新实例。以下是示例代码:

#include <iostream>

class Character
{
private:
    // Attributes
    int dex;
    int str;
    int end;

    // skills
    int lockpick;
    int guns;
    int sneak;

    /*****************************************  Setters  ********************************************************/
    // Attribute setters
public:
    void setStrength(const int &s)
    {
        this->str = s;
    }

    void setDexterity(const int &d)
    {
        this->dex = d;
    }

    void setEndurance(const int &e)
    {
        this->str = e;
    }

    // Skill setters
    void setLockpick(const int &s)
    {
        this->lockpick = s;
    }

    void setSneak(const int &s)
    {
        this->sneak = s;
    }

    void setGuns(const int &s)
    {
        this->guns = s;
    }

    int getGuns()
    {
        return this->guns;
    }

    int getStrength()
    {
        return this->str;
    }
};


/* Abstract builder */
class CharacterBuilder
{
protected:
    Character * int_character;
public: 
    Character * getCharacter()
    {
        return int_character;
    }

    void buildCharacter()
    {
        int_character = new Character;
    }

    virtual void buildSkills() = 0; 
    virtual void buildAttributes() = 0;

};

class MonsterBuilder : public CharacterBuilder
{
public:
    virtual void buildSkills()
    {
        int_character->setLockpick(10);
        int_character->setSneak(12);
        int_character->setGuns(50);
    }

    virtual void buildAttributes()
    {
        int_character->setStrength(5);
        int_character->setDexterity(5);
        int_character->setEndurance(5);
    }
};

class RuffianBuilder : public CharacterBuilder
{
public:
    virtual void buildSkills()
    {
        int_character->setLockpick(10);
        int_character->setSneak(12);
        int_character->setGuns(50);
    }

    virtual void buildAttributes()
    {
        int_character->setStrength(5);
        int_character->setDexterity(5);
        int_character->setEndurance(5);
    }
};

class Builder
{
public:
    void setBuilder(CharacterBuilder * builder)
    {
        this->builder = builder;
    }

    Character * getCharacter()
    {
        return builder->getCharacter();
    }

    void buildCharacter()
    {
        //std::cout << builder->buildSkills;
        builder->buildSkills();
        builder->buildAttributes();     
    }

private:
    CharacterBuilder * builder;
};

int main()
{
    Builder B;
    RuffianBuilder R;
    MonsterBuilder Mo;

    B.setBuilder(&R);
    B.buildCharacter();
    std::cout << B.getCharacter()->getGuns();
    std::cout << B.getCharacter()->getStrength();

    B.setBuilder(&Mo);
    B.buildCharacter();
    //std::cout << B.getCharacter()->getStrength();
    return 0;
}

是什么导致了这个问题?有人可以解释一下吗?

1 个答案:

答案 0 :(得分:2)

读取uninitlalized变量将导致未定义的行为。

我已将builder->buildCharacter();添加到Builder::buildCharacter(),然后此代码似乎运作良好。

class Builder
{
public:
    void setBuilder(CharacterBuilder * builder)
    {
        this->builder = builder;
    }

    Character * getCharacter()
    {
        return builder->getCharacter();
    }

    void buildCharacter()
    {
        //std::cout << builder->buildSkills;
        builder->buildCharacter(); // add this line
        builder->buildSkills();
        builder->buildAttributes();     
    }

private:
    CharacterBuilder * builder;
};