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

0 阅读4分钟

前言

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


代码仓库


原型模式(Prototype)

结构

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

核心

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

代码

#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>
#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++ 代码示例模板。


参考资料


作者的话

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