C++二十三种设计模式之原型模式

C++二十三种设计模式之原型模式

一、组成

抽象原型类 :声明克隆接口。
具体原型类:实现克隆接口。

二、特点

1、通过具体原型类克隆的对象只是部分属性值不同。

2、克隆函数内部可用拷贝构造函数赋值。

三、目的

通过已有对象来复制新对象,不同对象之间仅部分属性有差异。

四、缺点

1、场景限制问题,如果对象内部存在循环引用或动态分配的资源会比较麻烦。

2、深拷贝和浅拷贝问题,对象有嵌套结构则需要复杂的深拷贝逻辑。

五、示例代码

javascript 复制代码
#include<iostream>
#include <vector>
#include <list>
#include <string>
#include <mutex>
#include <map>
#include<stack>

using namespace std;

class Prototype;//抽象原型类
class BaseCharacter;//抽象类
class Player;//具体原型类
class Enemy;//具体原型类

class Prototype {
public:
	Prototype() {}
	virtual shared_ptr<Prototype> clone() = 0;
	virtual void setHealth(int health) = 0;
	virtual void setAttack(int attack) = 0;
	virtual void setDefense(int defense) = 0;
	virtual void setName(string name) = 0;
	virtual void print() = 0;
};

class BaseCharacter : public Prototype {
public:
	BaseCharacter() {}
	BaseCharacter(string name, int health, int attack, int defense) :m_name(name), m_health(health), m_attack(attack), m_defense(defense) {};
	void setHealth(int health) {
		m_health = health;
	};
	void setAttack(int attack) {
		m_attack = attack;
	};
	void setDefense(int defense) {
		m_defense = defense;
	};
	void setName(string name) {
		m_name = name;
	};
	void print() {
		cout << "name:" << m_name << ",health:" << m_health << endl;
	}
protected:
	int m_health;
	int m_attack;
	int m_defense;
	string m_name;
};

class Player : public BaseCharacter {
public:
	Player(string name, int health, int attack, int defense) : BaseCharacter(name, health, attack, defense) {};

	~Player() {
		cout << "~Player " << m_name << endl;
	}
	shared_ptr<Prototype> clone() {
		return make_shared<Player>(*this);
	};
};

class Enemy : public BaseCharacter {
public:
	Enemy(string name, int health, int attack, int defense) : BaseCharacter(name, health, attack, defense) {};
	~Enemy() {
		cout << "~Enemy " << m_name << endl;
	}

	shared_ptr<Prototype> clone() {
		return make_shared<Enemy>(*this);
	};
};
void test() {

	auto player1 = make_shared<Player>("Player1", 1, 2, 3);
	player1->setHealth(100);
	auto player2 = player1->clone();
	player2->setName("Player2");
	player1->setHealth(1000);

	player2->print();
	player1->print();
}
int main() {
	test();
}
相关推荐
saltymilk1 天前
使用 C++ 模拟 ShaderLanguage 的 swizzle
c++·模板元编程
xlp666hub2 天前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
程序员Terry2 天前
同事被深拷贝坑了3小时,我教他原型模式的正确打开方式
java·设计模式
得物技术2 天前
搜索 C++ 引擎回归能力建设:从自测到工程化准出|得物技术
c++·后端·测试
刀法如飞3 天前
AI时代,程序员都应该是算法思想工程师
人工智能·设计模式·程序员
xlp666hub3 天前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
会员源码网3 天前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
xlp666hub3 天前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
在西安放羊的牛油果3 天前
我把 2000 行下单代码,重构成了一套交易前端架构
前端·设计模式·架构
不想写代码的星星3 天前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++