C++设计模式——原型 (克隆)模式

一、什么是原型模式

Prototype模式说简单点,就是提供了一个clone, 通过已存在对象进行新对象创建。clone()实现和具体的实现语言相关,在C++中我们通过拷贝构造函数实现。

那为啥要写clone的接口来实现这个目的呢?直接使用拷贝构造不香么,偶然在知乎中看到陈硕大佬对此的一个回答,觉得豁然开朗。

Prototype 的意义在于,你拿到一个 基类指针 Base* ,它指向某个 派生类 Derived 对象,你想克隆出 Derived对象,但代码中不写出 Derived 的具体类型,因为有很多派生类,这种情况下你用构造函数是搞不定的。type-switch 是 bad smells 。另外,这里考虑 virtual 的性能损失是主次不分,构造对象需要分配内存,这开销比一次虚函数调用大多了。

--陈硕在知乎中的回答

二、代码示例

cpp 复制代码
#include <iostream>
#include <memory>
#include <cassert>
using namespace std;

class Prototype
{
public:

	/// 这里引入NVI模式
    std::unique_ptr<Prototype> clone(){ return clone_impl();}

    void noloop() const{ return noloop_impl();}

private:
    virtual std::unique_ptr<Prototype> clone_impl() const = 0;
    virtual void noloop_impl() const = 0;
public:
    Prototype() = default;
    virtual ~Prototype() = default;
protected:
    Prototype(const Prototype&) = default;
    Prototype(Prototype&&) = default;
    Prototype& operator =(const Prototype&) = default;
    Prototype& operator=(Prototype&&) = default;
};

class Derived : public Prototype
{
private:
    /// 本质实现还是通过拷贝构造函数,在本例中默认是浅拷贝
    std::unique_ptr<Prototype> clone_impl() const
    {
        return std::make_unique<Derived>(*this);
    }

    void noloop_impl() const{ cout << __FUNCTION__ << endl;}
};



int main()
{

    std::unique_ptr<Prototype> p0 = std::make_unique<Derived>();
    std::unique_ptr<Prototype> p1 = p0->clone();
    p1->noloop();

    assert((typeid(*p0) == typeid(*p1)) && (typeid(*p1) == typeid(Derived)));
    
    /// 从结果可以看出,p0和p1指向的均为Derived类型对象

    return 0;
}

三、总结

Prototype 模式的结构和实现都很简单,其关键就是(C++中)拷贝构造函数 的实现方

式,这也是C++实现技术层面上的事情。由于在示例代码中不涉及到深层拷贝(主要指有指针、复合对象的情况),因此我们通过编译器提供的默认的拷贝构造函数(按位拷贝)的方式进行实现。说明的是这一切只是为了实现简单起见,也因为本篇的重点不在拷贝构造函数的实现技术,而在Prototype模式本身的思想。

Prototype 模式通过复制原型(Prototype)而获得新对象创建的功能,这里Prototype本身就是"对象工厂"(因为能够生产对象),实际上Prototype 模式和Builder 模式、AbstractFactory 模式都是通过一个类(对象实例)来专门负责对象的创建工作(工厂对象),它们之间的区别是:Builder 模式重点在对复杂对象的一步步创建(并不直接返回对象),AbstractFactory 模式重在产生多个相互依赖类的对象,而Prototype模式重在从自身复制自己创建新类。

相关推荐
cpp_25016 小时前
P8448 [LSOT-1] 暴龙的土豆
数据结构·c++·算法·题解·洛谷
MSTcheng.6 小时前
【C++】C++智能指针
开发语言·c++·智能指针
云深处@6 小时前
【C++11】部分特性
开发语言·c++
独望漫天星辰6 小时前
C++ 树结构进阶:从工程化实现到 STL 底层与性能优化
开发语言·c++
HellowAmy6 小时前
我的C++规范 - 鸡蛋工厂
开发语言·c++·代码规范
仰泳的熊猫6 小时前
题目1453:蓝桥杯历届试题-翻硬币
数据结构·c++·算法·蓝桥杯
rainbow68896 小时前
C++STL list容器模拟实现详解
开发语言·c++·list
会叫的恐龙6 小时前
C++ 核心知识点汇总(第11日)(排序算法)
c++·算法·排序算法
符哥20086 小时前
C++ 适合初学者的学习笔记整理
c++·笔记·学习
星火开发设计6 小时前
this 指针:指向对象自身的隐含指针
开发语言·数据结构·c++·学习·指针·知识