cpp: Prototype Pattern
// Gold.h : 此文件包含 "Gold" 类。原型模式 Prototype Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit.
#pragma once
#ifndef GOLD_H
#define GOLD_H
#include <iostream>
using namespace std;
namespace DuJewelryPrototypePattern
{
/// <summary>
/// 黄金
/// </summary>
class Gold
{
public:
/// <summary>
/// 构造函数
/// </summary>
/// <param name="life"></param>
/// <param name="magic"></param>
/// <param name="attack"></param>
Gold(int technology, int business, int talents) :Gtechnology(technology), Gbusiness(business), Gtalents(talents) {}
/// <summary>
/// 做父类时析构函数应该为虚函数
/// </summary>
virtual ~Gold() {}
public:
//virtual Monster* creatGold() = 0; //具体的实现在子类中进行
/// <summary>
/// 具体的实现在子类中进行
/// </summary>
/// <returns></returns>
virtual Gold* clone() = 0;
protected: //可能被子类访问的成员,用protected修饰
//属性
/// <summary>
/// 技术
/// </summary>
int Gtechnology;
/// <summary>
/// 业务
/// </summary>
int Gbusiness;
/// <summary>
/// 人才
/// </summary>
int Gtalents;
};
}
#endif
// GoldCorporation.h : 此文件包含 "GoldCorporation" 类。原型模式 Prototype Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit.
#pragma once
#ifndef GOLDCORPORATION_H
#define GOLDCORPORATION_H
#include <iostream>
#include "Gold.h"
using namespace std;
namespace DuJewelryPrototypePattern
{
/// <summary>
/// 黄金公司
/// </summary>
class GoldCorporation:public Gold
{
public:
/// <summary>
/// 构造函数
/// </summary>
/// <param name="technology"></param>
/// <param name="business"></param>
/// <param name="talents"></param>
GoldCorporation(int technology, int business, int talents) :Gold(technology, business, talents)
{
cout << "创建了一个黄金公司" << endl;
}
public:
/// <summary>
/// 拷贝构造函数
/// </summary>
/// <param name="tmpobj"></param>
GoldCorporation(const GoldCorporation& tmpobj) :Gold(tmpobj)
{
cout << "调用了GoldCorporation::GoldCorporation(const GoldCorporation& tmpobj)拷贝构造函数创建了一个黄金公司" << endl;
}
/// <summary>
/// 拷贝构造函数
/// </summary>
/// <returns></returns>
virtual Gold* clone()
{
//return new GoldCorporation(800, 950, 880); //创建黄金公司
return new GoldCorporation(*this); //触发拷贝构造函数的调用来创建了一个黄金公司
}
//其他代码略....
};
}
#endif
// GoldCommerce.h : 此文件包含 "GoldCommerce" 类。原型模式 Prototype Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit.
#pragma once
#ifndef GOLDCOMMERCE_H
#define GOLDCOMMERCE_H
#include <iostream>
#include "Gold.h"
using namespace std;
namespace DuJewelryPrototypePattern
{
/// <summary>
/// 商贸商会
/// </summary>
class GoldCommerce :public Gold
{
public:
/// <summary>
/// 构造函数
/// </summary>
/// <param name="technology"></param>
/// <param name="business"></param>
/// <param name="talents"></param>
GoldCommerce(int technology, int business, int talents) :Gold(technology, business, talents)
{
cout << "创建了一个商贸商会" << endl;
}
public:
/// <summary>
/// 拷贝构造函数
/// </summary>
/// <param name="tmpobj"></param>
GoldCommerce(const GoldCommerce& tmpobj) :Gold(tmpobj) //初始化列表中注意对父类子对象的初始化
{
cout << "调用了GoldCommerce::GoldCommerce(const GoldCommerce& tmpobj)拷贝构造函数创建了一个商贸商会" << endl;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
virtual Gold* clone()
{
//return new GoldCommerce(800, 180, 990); //创建商贸商会
return new GoldCommerce(*this);
}
//其他代码略....
};
}
#endif
// GoldShop.h : 此文件包含 "GoldShop" 类。原型模式 Prototype Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit.
#pragma once
#ifndef GOLDSHOP_H
#define GOLDSHOP_H
#include <iostream>
#include "Gold.h"
using namespace std;
namespace DuJewelryPrototypePattern
{
/// <summary>
/// 连锁店舖
/// </summary>
class GoldShop :public Gold
{
public:
/// <summary>
/// 构造函数
/// </summary>
/// <param name="technology"></param>
/// <param name="business"></param>
/// <param name="talents"></param>
GoldShop(int technology, int business, int talents) :Gold(technology, business, talents)
{
cout << "创建了一个连锁店舖" << endl;
}
public:
/// <summary>
/// 拷贝构造函数
/// </summary>
/// <param name="tmpobj"></param>
GoldShop(const GoldShop& tmpobj) :Gold(tmpobj)
{
cout << "调用了GoldShop::GoldShop(const GoldShop& tmpobj)拷贝构造函数创建了一个连锁店舖" << endl;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
virtual Gold* clone()
{
//return new GoldShop(800, 10, 110); //创建连锁店舖
return new GoldShop(*this);
}
//其他代码略....
};
}
#endif
// GeovinDu.h : 此文件包含 "GeovinDu" 类。。原型模式 Prototype Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du edit.
#pragma once
#ifndef GEOVINDU_H
#define GEOVINDU_H
using namespace std;
namespace DuJewelryPrototypePattern
{
class GeovinDu
{
private:
public:
/// <summary>
///
/// </summary>
void displayGold();
};
}
#endif
// GeovinDu.cpp : 此文件包含 "GeovinDu" 类。。原型模式 Prototype Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du edit.
#include "GeovinDu.h"
#include "Gold.h"
#include "GoldCommerce.h"
#include "GoldCorporation.h"
#include "GoldShop.h"
namespace DuJewelryPrototypePattern
{
/// <summary>
/// 全局的用于创建对象的函数
/// </summary>
/// <param name="pGold"></param>
void GlobalCreateGold(Gold* pGold)
{
/*
Gold* ptmpco = nullptr;
if (dynamic_cast<GoldCommerce*>(pGold) != nullptr)
{
ptmpco = new GoldCommerce(300, 50, 80); //创建商贸商会
}
else if (dynamic_cast<GoldCorporation*>(pGold) != nullptr)
{
ptmpco = new GoldCorporation(200, 80, 100); //创建黄金公司
}
else if (dynamic_cast<GoldShop*>(pGold) != nullptr)
{
ptmpco = new GoldShop(400, 0, 110); //创建连锁店舖
}
if (ptmpco != nullptr)
{
//这里可以针对ptmpobj对象实现各种业务逻辑
//......
//不要忘记释放资源
delete ptmpco;
}
*/
Gold* ptmpobj = pGold->clone();//根据已有对象直接创建新对象,不需要知道已有对象所属的类型
//这里可以针对ptmpobj对象进行实现各种业务逻辑
//......
//不要忘记释放资源
delete ptmpobj;
}
/// <summary>
///
/// </summary>
void GeovinDu::displayGold()
{
/**/
DuJewelryPrototypePattern::GoldShop myPropMecGold(400, 0, 110); //创建连锁店舖对象作为原型对象以用于克隆目的
DuJewelryPrototypePattern::Gold* pmyPropGoldCommerce = new DuJewelryPrototypePattern::GoldCommerce(200, 80, 100); //创建商贸商会对象作为原型对象以用于克隆目的,这里可以直接new,也可以通过工厂模式创建原型对象,取决于程序员自己的喜好
//......
DuJewelryPrototypePattern::Gold* p_CloneObj1 = myPropMecGold.clone(); //使用原型对象克隆出新的连锁店舖对象
DuJewelryPrototypePattern::Gold* p_CloneObj2 = pmyPropGoldCommerce->clone(); //使用原型对象商贸商会对象
//可以对p_CloneObj1、p_CloneObj2所指向的对象进行各种操作(实现具体业务逻辑)
//......
//释放资源
//释放克隆出来的怪物对象
delete p_CloneObj1;
delete p_CloneObj2;
//释放原型对象(堆中的)
delete pmyPropGoldCommerce;
DuJewelryPrototypePattern::Gold* pMonsterObj = new DuJewelryPrototypePattern::GoldCommerce(200, 80, 100);
GlobalCreateGold(pMonsterObj);
delete pMonsterObj;
}
}
调用:
// ConsoleDuPrototypePattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 原型模式 Prototype Pattern
// ConsoleDuPrototypePattern.cpp : 此文件包含 "ConsoleDuPrototypePattern" 类。 Prototype Pattern C++ 14
// 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. 文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
#define _UNICODE
#include <iostream>
#include "GeovinDu.h"
#ifdef _DEBUG //只在Debug(调试)模式下
#ifndef DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) //重新定义new运算符
#define new DEBUG_NEW
#endif
#endif
//#include <boost/type_index.hpp>
using namespace std;
//#pragma warning(disable : 4996)
using namespace DuJewelryPrototypePattern;
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);//程序退出时检测内存泄漏并显示到“输出”窗口
std::cout << "Hello World!!Programa Olá Mundo!涂聚文 Geovin Du\n";
GeovinDu geovindu;
geovindu.displayGold();
system("pause");
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
#define UNICODE
输出
Hello World!!Programa Olá Mundo!涂聚文 Geovin Du 创建了一个连锁店舖 创建了一个商贸商会 调用了GoldShop::GoldShop(const GoldShop& tmpobj)拷贝构造函数创建了一个连锁店舖 调用了GoldCommerce::GoldCommerce(const GoldCommerce& tmpobj)拷贝构造函数创建了一个商贸商会 创建了一个商贸商会 调用了GoldCommerce::GoldCommerce(const GoldCommerce& tmpobj)拷贝构造函数创建了一个商贸商会 请按任意键继续. . .

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号