设计模式04——建造者模式

 

实例:

//builder.cpp
//建造者模式:用于创建一些复杂对象,这些对象内部构建间的
//建造顺序通常是稳定的,但是对象内部的构建通常面临着复杂的变化。

#include "gtest/gtest.h"
#include <string>

//产品
class Product
{
public:
    Product() {}
    ~Product() {}
    std::string value;
};

//
class Builder
{
public:
    Builder() {}
    virtual ~Builder() {}

    virtual void BuildPartA() = 0;
    virtual void BuildPartB() = 0;
    virtual Product* GetResult() = 0;
};

class ConcreteBuilder1 : public Builder
{
public:
    ConcreteBuilder1() 
    {
        product = new Product();
    }

    ~ConcreteBuilder1() {}

    void BuildPartA()
    {
        product->value += "A1";
    }

    void BuildPartB()
    {
        product->value += "B1";
    }

    Product* GetResult()
    {
        return product;
    }

private:
    Product* product;
};

class ConcreteBuilder2 : public Builder
{
public:
    ConcreteBuilder2() 
    {
        product = new Product();
    }

    ~ConcreteBuilder2() {}

    void BuildPartA()
    {
        product->value += "A2";
    }

    void BuildPartB()
    {
        product->value += "B2";
    }

    Product* GetResult()
    {
        return product;
    }

private:
    Product* product;
};


//指挥类
class Director
{
public:
    Director() {}
    ~Director() {}

    void Construct(Builder* builder)
    {
        builder->BuildPartA();
        builder->BuildPartB();
    }
};

TEST(Builder,builder)
{
    Director* director = new Director();
    Builder* builder1 = new ConcreteBuilder1();
    Builder* builder2 = new ConcreteBuilder2();

    director->Construct(builder1);
    Product* p1 = builder1->GetResult();
    EXPECT_STREQ("A1B1",p1->value.c_str());

    director->Construct(builder2);
    Product* p2 = builder2->GetResult();
    EXPECT_STREQ("A2B2",p2->value.c_str());

    delete director;
    delete builder1;
    delete builder2;
    delete p1;
    delete p2;
}

 

转载于:https://www.cnblogs.com/TheImportanceOfLiving/p/8215573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值