设计模式 - Builder

意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

class Builder
{
public:
    Builder()
    {}
    virtual ~Builder()
    {}
    virtual void BuilderPartA(const string& str) = 0;
    virtual void BuilderPartB(const string& str) = 0;
    virtual string GetResult()
    {
        return product;
    }

protected:
    string product;
};

class ConcreteBuilder: public Builder
{
public:
    ConcreteBuilder()
    {}
    ~ConcreteBuilder()
    {}
    void BuilderPartA(const string& str)
    {
        product.append(str);
    }
    void BuilderPartB(const string& str)
    {
        product.append(str);
    }
};

class Director
{
public:
    Director(Builder* builder)
    {
        this->builder = builder;
    }
    ~Director()
    {}
    void Construct()
    {
        builder->BuilderPartA("winson ");
        builder->BuilderPartB("leung");
        string result = builder->GetResult();
        cout<<result<<endl;
    }

private:
    Builder* builder;
};

 

posted @ 2013-05-04 21:21  Leung文  阅读(117)  评论(0编辑  收藏  举报