C++原型模式(Protype)

一、目的:

(1) 通过复制这些原型对象创建新对象。

(2)当实例化的类是在运行是指定时就可以通过这种方式

cpp 复制代码
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;

class Something{
public:
        Something(int id,string type):m_id(id),m_type(type){cout<<"Something构造函数被调用"<<endl;}
        virtual ~Something(){cout<<"Something析构函数调用"<<endl;}
        virtual Something* clone()=0;
        virtual string  who()=0;
        virtual int typeId()=0;
        Something(const Something& something){
                this->m_id= something.m_id;
                this->m_type=something.m_type;
                cout<<"Something拷贝构造调用"<<endl;
        }

protected:
        int m_id;
        string m_type;

};

class Human:public Something{
public:
        Human(int id,string type):Something(id,type){cout<<"Human构造数被调用"<<endl;}
        Human(const Human& human):Something(human){cout<<"Human拷贝构造函数调用"<<endl;}
        virtual Something* clone() override {return new Human(*this);}
        virtual string who() override {return m_type;}
        virtual int typeId() override{return m_id;}
        ~Human(){cout<<"Human析构函数被调用"<<endl;}
};


class Dog:public Something{
public:
        Dog(int id,string type):Something(id,type){cout<<"dog构造函数被调用"<<endl;}
        Dog(const Dog& dog):Something(dog){cout<<"dog拷贝构造函数被调用"<<endl;}
        ~Dog(){cout<<"Dog析构被调用"<<endl;}
        virtual string who() override {return m_type;}
        virtual int typeId() override{return m_id;}
        virtual Something* clone() override {return new Dog(*this);}
};

class All{
public:
        ~All(){      //析构函数
                for (auto& x : m_SomeMap) {
                        delete x.second;
                        x.second = nullptr;
                }
        }

        All()  //构造函数
        {
                Human* human = new Human{1, "人类" };
                Dog* dog  = new Dog{2,"狗类"};
                m_SomeMap.emplace(human->who(), human);
                m_SomeMap.emplace(dog->who(), dog);
        }
        //根据你所需要的种类来获得克隆对象
        Something* getSome(string Type){return m_SomeMap[Type]->clone();}

private:
        unordered_map<string, Something*> m_SomeMap;
};
int main(int agv,char* agc[]){
    cout << "开始测试"<<endl;

    All factory;
    // 克隆人类
    Something* p1 = factory.getSome("人类");
    cout << "克隆类型:" << p1->who() << " id:" << p1->typeId() << endl;
    delete p1;

    // 克隆狗
    Something* p2 = factory.getSome("狗类");
    cout << "克隆类型:" << p2->who() << " id:" << p2->typeId() << endl;
    delete p2;

    // 测试不存在类型
    //Something* p3 = factory.getSome("猫类");

    return 0;
}

二、运行结果

复制代码
kickpi@kickpi:~/test$ g++ prototype.cpp -o main2
kickpi@kickpi:~/test$ ./main2
开始测试
Something构造函数被调用
Human构造数被调用
Something构造函数被调用
dog构造函数被调用
Something拷贝构造调用
Human拷贝构造函数调用
克隆类型:人类 id:1
Human析构函数被调用
Something析构函数调用
Something拷贝构造调用
dog拷贝构造函数被调用
克隆类型:狗类 id:2
Dog析构被调用
Something析构函数调用
Dog析构被调用
Something析构函数调用
Human析构函数被调用
Something析构函数调用
相关推荐
月华路几秒前
《模型不玄学》第14章 标签、损失与样本权重
人工智能·算法·机器学习
黄金龙PLUS2 分钟前
SPARKLE置换算法的优缺点
算法·网络安全·密码学·哈希算法·同态加密
不会就选b3 分钟前
算法日常・每日刷题--<贪心>3
数据结构·算法·leetcode
学习星球6 分钟前
编辑距离——二维 DP 的“最小操作“艺术
c++·leetcode·java-consul
OPEN-F6 分钟前
ROS2系列教程:tf2坐标变换详解(C++/Python)
开发语言·c++·python
暖焰核心8 分钟前
迭代器与模板的结合——stl的list
c++·windows·list
行者全栈架构师11 分钟前
WorkBuddy 实战:把一份 200 页年报变成可上台的投资分析 PPT
人工智能·算法·全栈
XR12345678816 分钟前
医院基础网络改造升级选型深度解析
开发语言·网络·php
reasonsummer17 分钟前
【办公类-146-05】20260901《一分园、二分园四大教育》(优化版:标题excle+复制AI文字+Python占位符录入)
开发语言·数据库·c#
微三云生态系统架构师-彭丹25 分钟前
矩阵拼团系统设计:先付款先排队的订单排序与自动返本算法
线性代数·算法·矩阵