原型和建造者模式(创建型设计模式)的 C++ 代码示例模板

前言

原型和建造者模式(创建型设计模式)的 C++ 代码示例模板。


代码仓库


原型模式(Prototype)

结构

  • 抽象原型类
  • 具体原型类
  • 具体原型对象的克隆方法通过拷贝构造函数创建当前对象的副本

核心

  • 继承
  • 多态
  • 拷贝构造函数注意浅拷贝和深拷贝问题
  • 克隆方法通过拷贝构造函数创建副本

代码(普通版)

#include <iostream>

using std::cout;
using std::endl;

// 抽象原型类
class AbstractPrototype
{
public:
    // 注意:
    // 抽象类不能实例化,不需要显式定义构造方法
    // 如果抽象类的派生类有指针等需要动态内存分配的属性,为了正确释放基类和派生类的资源,派生类需要重写析构方法,基类需要定义虚析构方法
    virtual ~AbstractPrototype() = default;

    virtual AbstractPrototype *clone() const = 0; // 克隆方法
    virtual void func() = 0;
};

// 具体原型类
class ConcretePrototype : public AbstractPrototype
{
public:
    ConcretePrototype(int data) : data(new int(data)) {}
    ~ConcretePrototype() override
    {
        delete this->data;
    }

    // 拷贝构造函数
    // 注意浅拷贝和深拷贝问题

    // 非指针属性:不存在浅拷贝和深拷贝问题
    // 静态内存分配/栈区的指针属性:不存在浅拷贝和深拷贝问题
    // 动态内存分配/堆区的指针属性:存在浅拷贝和深拷贝问题,不应使用浅拷贝,应使用深拷贝

    // 浅拷贝:复制值
    // 深拷贝:创建新对象再复制值

    // unique_ptr<> 可以移动,不可以(浅)拷贝
    // shared_ptr<> 可以(浅/深)拷贝

    // ConcretePrototype(const ConcretePrototype &other) : data(other.data) // 浅拷贝
    // {
    //     cout << "Shallow copy" << endl;
    // }
    ConcretePrototype(const ConcretePrototype &other) : data(new int(*(other.data))) // 深拷贝
    {
        cout << "Deep copy" << endl;
    }

    // 克隆方法创建当前具体原型对象的副本
    AbstractPrototype *clone() const override
    {
        return new ConcretePrototype(*this);
    }

    void func() override
    {
        cout << "ConcretePrototype: " << *(this->data) << endl;
    }

private:
    int *data;
};

// 客户端
int main()
{
    // 创建具体原型对象
    ConcretePrototype *original = new ConcretePrototype(10);
    original->func();

    // 克隆具体原型对象
    AbstractPrototype *clone = original->clone();
    clone->func();

    delete clone;
    delete original;

    return 0;
}
/*
输出:
ConcretePrototype: 10
Deep copy
ConcretePrototype: 10
*/

代码

#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::make_unique;
using std::unique_ptr;

// 抽象原型类
class AbstractPrototype
{
public:
    AbstractPrototype() = default;
    virtual ~AbstractPrototype() = default;

    virtual inline unique_ptr<AbstractPrototype> clone() const = 0; // 克隆方法

    virtual void func() = 0;
};

// 具体原型类
class ConcretePrototype : public AbstractPrototype
{

public:
    ConcretePrototype(int data) : data(make_unique<int>(data)) {}
    ~ConcretePrototype() override = default;

    // 拷贝构造函数
    // 注意浅拷贝和深拷贝问题

    // 非指针属性:不存在浅拷贝和深拷贝问题
    // 静态内存分配/栈区的指针属性:不存在浅拷贝和深拷贝问题
    // 动态内存分配/堆区的指针属性:存在浅拷贝和深拷贝问题,不应使用浅拷贝,应使用深拷贝

    // 浅拷贝:复制值
    // 深拷贝:创建新对象再复制值

    // unique_ptr<> 可以移动,不可以(浅)拷贝
    // shared_ptr<> 可以(浅/深)拷贝

    // ConcretePrototype(const ConcretePrototype &other) : data(other.data) // 浅拷贝
    // {
    //     cout << "Shallow copy" << endl;
    // }
    ConcretePrototype(const ConcretePrototype &other) : data(make_unique<int>(*(other.data))) // 深拷贝
    {
        cout << "Deep copy" << endl;
    }

    // 克隆方法创建当前具体原型对象的副本
    inline unique_ptr<AbstractPrototype> clone() const override
    {
        return make_unique<ConcretePrototype>(*this); // 调用拷贝构造函数(类比:ConcretePrototype clone(*this))
    }

    void func() override
    {
        cout << "ConcretePrototype: " << *this->data << endl;
    }

private:
    unique_ptr<int> data;
};

// 客户端
int main()
{
    // 创建具体原型对象
    unique_ptr<ConcretePrototype> original = make_unique<ConcretePrototype>(10);
    original->func();

    // 克隆具体原型对象
    unique_ptr<AbstractPrototype> clone = original->clone();
    clone->func();

    return 0;
}
/*
输出:
ConcretePrototype: 10
Deep copy
ConcretePrototype: 10
*/

建造者模式(Builder)

结构

  • 产品类
  • 抽象建造者类
  • 具体建造者类
  • 指挥者类
  • 创建具体建造者对象(建造准备1)
  • 创建具体建造者对象时,创建产品对象(建造准备2)
  • 使用具体建造者对象,创建指挥者对象(建造准备3)
  • 指挥者对象调用建造方法(建造过程1)
  • 建造方法中,具体建造者对象调用建造产品方法(建造过程2)
  • 建造产品方法中,产品对象调用相关方法(建造过程3)
  • 具体建造者对象调用获取产品方法(获取结果)
  • 产品对象调用展示方法(展示结果)

核心

  • 继承
  • 多态
  • 产品,表现需要建造的内容(如建造桌子,需要建造桌面,需要建造桌腿)
  • 建造者 建造 产品,实现建造过程的内容(如建造桌子,建造桌面是光滑的,建造桌腿是稳固的)
  • 指挥者 指挥 建造者,控制建造过程的时序(如建造桌子,先建造桌面,后建造桌腿)

代码(普通版)

#include <string>
#include <iostream>

using std::cout;
using std::endl;
using std::string;

// 产品类
class Product
{
public:
    void set_part_a(const string &part_a)
    {
        this->part_a = part_a;
    }

    void set_part_b(const string &part_b)
    {
        this->part_b = part_b;
    }

    inline void show() const
    {
        cout << "Product parts: " << this->part_a << ", " << this->part_b << endl;
    }

private:
    string part_a;
    string part_b;
};

// 抽象建造者类
class AbstractBuilder
{
public:
    virtual void build_part_a() = 0; // 建造产品方法
    virtual void build_part_b() = 0;
    virtual Product get_product() = 0; // 获取产品方法
};

// 具体建造者类
class ConcreteBuilder : public AbstractBuilder
{
public:
    ConcreteBuilder() : product() {} // 使用默认构造函数创建产品对象

    void build_part_a() override
    {
        this->product.set_part_a("Part A");
    }

    void build_part_b() override
    {
        this->product.set_part_b("Part B");
    }

    inline Product get_product() override
    {
        return this->product;
    }

private:
    Product product;
};

// 指挥者类
class Director
{
public:
    Director(AbstractBuilder *abstract_builder) : abstract_builder(abstract_builder){};

    void build() // 建造方法
    {
        this->abstract_builder->build_part_a();
        this->abstract_builder->build_part_b();
    }

private:
    AbstractBuilder *abstract_builder;
};

// 客户端
int main()
{
    // 创建具体建造者对象
    AbstractBuilder *concrete_builder = new ConcreteBuilder();

    // 创建指挥者对象
    Director director(concrete_builder);

    // 建造过程
    director.build();

    // 获取产品
    Product product = concrete_builder->get_product();

    // 展示产品
    product.show();

    delete concrete_builder;

    return 0;
}
/*
输出:
Product parts: Part A, Part B
*/

代码

#include <string>
#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::make_shared;
using std::shared_ptr;
using std::string;

// 产品类
class Product
{
public:
    Product() = default;
    ~Product() = default;

    void set_part_a(const string &part_a)
    {
        this->part_a = part_a;
    }

    void set_part_b(const string &part_b)
    {
        this->part_b = part_b;
    }

    inline void show() const
    {
        cout << "Product parts: " << this->part_a << ", " << this->part_b << endl;
    }

private:
    string part_a;
    string part_b;
};

// 抽象建造者类
class AbstractBuilder
{
public:
    AbstractBuilder() = default;
    virtual ~AbstractBuilder() = default;

    virtual void build_part_a() = 0; // 建造产品方法
    virtual void build_part_b() = 0;
    virtual Product get_product() = 0; // 获取产品方法
};

// 具体建造者类
class ConcreteBuilder : public AbstractBuilder
{
public:
    ConcreteBuilder() : product() {} // 使用默认构造函数创建产品对象
    ~ConcreteBuilder() override = default;

    void build_part_a() override
    {
        this->product.set_part_a("Part A");
    }

    void build_part_b() override
    {
        this->product.set_part_b("Part B");
    }

    inline Product get_product() override
    {
        return this->product;
    }

private:
    Product product;
};

// 指挥者类
class Director
{
public:
    Director(shared_ptr<AbstractBuilder> abstract_builder) : abstract_builder(abstract_builder){};
    ~Director() = default;

    void build() // 建造方法
    {
        abstract_builder->build_part_a();
        abstract_builder->build_part_b();
    }

private:
    shared_ptr<AbstractBuilder> abstract_builder;
};

// 客户端
int main()
{
    // 创建具体建造者对象
    shared_ptr<AbstractBuilder> concrete_builder = make_shared<ConcreteBuilder>();

    // 创建指挥者对象
    Director director(concrete_builder);

    // 建造过程
    director.build();

    // 获取产品
    Product product = concrete_builder->get_product();

    // 展示产品
    product.show();

    return 0;
}
/*
输出:
Product parts: Part A, Part B
*/

总结

原型模式和建造者模式(创建型设计模式)的 C++ 代码示例模板。


参考资料


作者的话

  • 感谢参考资料的作者/博主
  • 作者:夜悊
  • 版权所有,转载请注明出处,谢谢~
  • 如果文章对你有帮助,请点个赞或加个粉丝吧,你的支持就是作者的动力~
  • 文章在描述时有疑惑的地方,请留言,定会一一耐心讨论、解答
  • 文章在认识上有错误的地方, 敬请批评指正
  • 望读者们都能有所收获

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值