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模式重在从自身复制自己创建新类。

相关推荐
感哥14 小时前
C++ 多态
c++
沐怡旸21 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
幂简集成explinks1 天前
e签宝签署API更新实战:新增 signType 与 FDA 合规参数配置
后端·设计模式·开源
River4161 天前
Javer 学 c++(十三):引用篇
c++·后端
感哥1 天前
C++ std::set
c++
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
博笙困了1 天前
AcWing学习——差分
c++·算法
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
努力也学不会java1 天前
【设计模式】抽象工厂模式
java·设计模式·oracle·抽象工厂模式
青草地溪水旁1 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式