大话设计模式 C++实现 第九章

本文对比了三种版本的原型模式代码,从基础的V1版到V2的显式复制构造函数和移动构造函数,再到V3的引用计数管理。通过实例展示了如何使用原型模式创建可克隆的对象并修改其属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小菜第一版代码

//第九章原型模式
//V1版本
#include<string>
#include<iostream>
using namespace std;

class Resume {
private:
	string name;
	string sex;
	string age;
	string timeArea;
	string company;

public:
	Resume(string name) {
		this->name = name;
	}

	//设置个人信息
	void SetPersonalInfo(string sex, string age) {
		this->sex = sex;
		this->age = age;
	}

	//设置工作经历
	void SetWorkExperience(string timeArea, string company) {
		this->timeArea = timeArea;
		this->company = company;
	}

	void Display() {
		cout << name << "," << sex << "," << age << endl;
		cout << "工作经历" << timeArea << "," << company << endl;
	}
};

//客户端代码
int main() {
	Resume a{ "大鸟" };
	a.SetPersonalInfo("男","29");
	a.SetWorkExperience("1998-2000", "xx公司");

	Resume b{ "大鸟" };
	b.SetPersonalInfo("男", "29");
	b.SetWorkExperience("1998-2000", "xx公司");

	Resume c{ "大鸟" };
	c.SetPersonalInfo("男", "29");
	c.SetWorkExperience("1998-2000", "xx公司");

	a.Display();
	b.Display();
	c.Display();
	
	return 0;

}

小菜第二版代码

//第九章原型模式
//V2版本
#include<string>
#include<iostream>
using namespace std;
class Resume {
private:
	string name;
	string sex;
	string age;
	string timeArea;
	string company;
public:
	Resume(Resume& re) = default;	//V1.5显式声明合成复制构造函数
	Resume(Resume&& re) = default;	//V2 显式声明合成移动构造函数

	Resume(string name) {
		this->name = name;
	}

	//设置个人信息
	void SetPersonalInfo(string sex, string age) {
		this->sex = sex;
		this->age = age;
	}

	//设置工作经历
	void SetWorkExperience(string timeArea, string company) {
		this->timeArea = timeArea;
		this->company = company;
	}

	void Display() {
		cout << name << "," << sex << "," << age << endl;
		cout << "工作经历" << timeArea << "," << company << endl;
	}

	//V2 拷贝本身
	Resume Clone() {
		return Resume(*this);
	}

};

//客户端代码
int main() {
	Resume a{ "大鸟" };
	a.SetPersonalInfo("男", "29");
	a.SetWorkExperience("1998-2000", "xx公司");

	Resume b{ a.Clone() };
	b.SetWorkExperience("1998-2006", "yy公司");

	Resume c{ a.Clone() };
	c.SetPersonalInfo("男", "24");

	a.Display();
	b.Display();
	c.Display();

	return 0;

}

小菜第三版代码

//第九章原型模式
//V3版本
#include<string>
#include<iostream>
#include<utility>
using namespace std;

class WorkExperience {
public:
	string workDate;
	string company;
};

class Resume {
private:
	string name;
	string sex;
	string age;
	shared_ptr<WorkExperience> work;//V3

public:
	Resume(Resume& re) = default;	//V1.5显式声明合成复制构造函数
	Resume(Resume&& re) = default;	//V2 显式声明合成移动构造函数

	Resume(string name)  {
		work.reset(new WorkExperience);	//V3
		this->name = name;
	}

	//设置个人信息
	void SetPersonalInfo(string sex, string age) {
		this->sex = sex;
		this->age = age;
	}

	//设置工作经历
	void SetWorkExperience(string timeArea, string company) {
		work->workDate = timeArea;
		work->company = company;
	}

	void Display() {
		cout << name << "," << sex << "," << age << endl;
		cout << "工作经历" << work->workDate << "," << work->company << endl;//V3
	}

	//V2 V3 拷贝本身
	Resume Clone() {
		Resume re(*this);
		re.work.reset(new WorkExperience{ work->workDate,work->company });//V3
		return re;
	}

};

//客户端代码
int main() {
	Resume a{ "大鸟" };
	a.SetPersonalInfo("男", "29");
	a.SetWorkExperience("1998-2000", "xx公司");

	Resume b{ a.Clone() };
	b.SetWorkExperience("1998-2006", "yy公司");

	Resume c{ a.Clone() };
	c.SetPersonalInfo("男", "24");

	a.Display();
	b.Display();
	c.Display();

	return 0;

}

由于作者水平有限,本书错漏缺点在所难免,希望读者批评指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值