设计模式之 原型模式(prototype)(C++实现 深拷贝 + 浅拷贝版本[bug])

本文介绍设计模式中的原型模式。


本质上其实就是克隆。


下面以个人简历为例进行举例说明:


深拷贝版本:

#include <iostream>
#include <string>
#include <memory>

using namespace std;

template<class T>
class ICloneable
{
public:
	virtual T* clone() = 0;
};

class CWorkExperience
{
public:
	CWorkExperience(){}

	CWorkExperience(const string& company,
		const string& workTime)
	{
		m_strCompany = company;
		m_strWorkTime = workTime;
	}

	CWorkExperience(const CWorkExperience& right)
	{
		m_strCompany = right.m_strCompany;
		m_strWorkTime = right.m_strWorkTime;
	}

	~CWorkExperience()
	{
		cout << "CWorkExperience析构" << endl;
		printInfo();
	}

	void setCompany(const string& company)
	{
		m_strCompany = company;
	}

	const string& getCompany() const
	{
		return m_strCompany;
	}

	void setWorkTime(const string& workTime)
	{
		m_strWorkTime = workTime;
	}

	const string& getWorkTime() const
	{
		return m_strWorkTime;
	}

	void printInfo()
	{
		cout << "Company: " << m_strCompany << endl;
		cout << "WorkTime: " << m_strWorkTime << endl;
	}

private:
	string m_strCompany;		// company name
	string m_strWorkTime;		// work time
};

// 简历类
class CResume : public ICloneable<CResume>
{
public:
	CResume(){}
	~CResume()
	{
		cout << "CResume析构 " << m_name << endl;
	}

	void setInfo(const string& name, const string& sex
		, int age)
	{
		m_name = name;
		m_sex = sex;
		m_age = age;
	}

	void setExperience(const string& company, const string& workTime)
	{
		m_experience.setCompany(company);
		m_experience.setWorkTime(workTime);
	}

	CResume* clone()
	{
		CResume* resume = new CResume;
		resume->setInfo(m_name, m_sex, m_age);
		resume->setExperience(m_experience.getCompany(), m_experience.getWorkTime());
		return resume;
	}

	void printInfo()
	{
		cout << "Name: " << m_name << endl;
		cout << "Sex: " << m_sex << endl;
		cout << "Age: " << m_age << endl;
		cout << "Experience: " << endl;
		m_experience.printInfo();
		cout << endl;
	}
protected:
	string m_name;
	string m_sex;
	int m_age;

	CWorkExperience m_experience;// 对象
};

void testPrototype()
{
	CResume re;
	re.setInfo("Jacky", "Male", 20);
	re.setExperience("MS", "2001.11 - 2005.11");
	re.printInfo();

	CResume* pClone = re.clone();
	pClone->setInfo("Marry", "Female", 30);
	pClone->setExperience("Google", "2006.01 - 2010.01");
	pClone->printInfo();

	delete pClone;
	pClone = NULL;
}

int main(void)
{
	testPrototype();

	return 0;
}

运行结果如下:


浅拷贝版本:

#include <iostream>
#include <string>
#include <memory>

using namespace std;

// 克隆接口
template<class T>
class ICloneable
{
public:
	virtual T* clone() = 0;
};

// 工作经历类
class CWorkExperience
{
public:
	CWorkExperience(){}

	CWorkExperience(const string& company,
		const string& workTime)
	{
		m_strCompany = company;
		m_strWorkTime = workTime;
	}

	CWorkExperience(const CWorkExperience& right)
	{
		m_strCompany = right.m_strCompany;
		m_strWorkTime = right.m_strWorkTime;
	}

	~CWorkExperience()
	{
		cout << "CWorkExperience析构" << endl;
		printInfo();
	}

	void setCompany(const string& company)
	{
		m_strCompany = company;
	}

	const string& getCompany() const
	{
		return m_strCompany;
	}

	void setWorkTime(const string& workTime)
	{
		m_strWorkTime = workTime;
	}

	const string& getWorkTime() const
	{
		return m_strWorkTime;
	}

	void printInfo()
	{
		cout << "Company: " << m_strCompany << endl;
		cout << "WorkTime: " << m_strWorkTime << endl;
	}

private:
	string m_strCompany;		// company name
	string m_strWorkTime;		// work time
};

// 简历类
class CResume : public ICloneable<CResume>
{
private:
	// 只允许调用带参数的构造函数和拷贝构造函数
	CResume(){m_experience = NULL;}
public:
	// 带参构造函数
	CResume(CWorkExperience* pWorkExperience)
	{
		// 不负责内存分配 只是共享
		m_experience = pWorkExperience;
	}

	// 带参拷贝构造函数
	CResume(const CResume& right)
	{
		m_name = right.m_name;
		m_sex = right.m_sex;
		m_age = right.m_age;
		// 注意这里是指针赋值 属于浅拷贝
		m_experience = right.m_experience;
	}

	~CResume()
	{
		cout << "CResume析构 " << m_name << endl;
	}

	void setInfo(const string& name, const string& sex
		, int age)
	{
		m_name = name;
		m_sex = sex;
		m_age = age;
	}

	void setExperience(const string& company, const string& workTime)
	{
		m_experience->setCompany(company);
		m_experience->setWorkTime(workTime);
	}

	CResume* clone()
	{
		// 调用拷贝构造函数 浅拷贝
		CResume* resume = new CResume(*this);
		return resume;
	}

	void printInfo()
	{
		cout << "Name: " << m_name << endl;
		cout << "Sex: " << m_sex << endl;
		cout << "Age: " << m_age << endl;
		cout << "Experience: " << endl;
		m_experience->printInfo();
		cout << endl;
	}
protected:
	string m_name;
	string m_sex;
	int m_age;

	CWorkExperience* m_experience;// 指针 聚合
};

void testPrototype()
{
	CWorkExperience* pWorkExperience = new CWorkExperience("MS", "2001.11 - 2005.11");
	CResume re(pWorkExperience);	// 只能这样构造 无参构造被屏蔽掉了
	re.setInfo("Jacky", "Male", 20);
	re.printInfo();

	CResume* pClone = re.clone();	// 指针赋值 浅拷贝
	//delete pWorkExperience;		// 这里如果调用delete 程序会崩溃(因为是共享)

	pClone->setInfo("Marry", "Female", 30);
	pClone->setExperience("Google", "2006.01 - 2010.01");
	pClone->printInfo();

	re.printInfo();	// 我们发现原对象的workExperience属性已经被修改

	delete pClone;
	delete pWorkExperience;
	pClone = NULL;
	pWorkExperience = NULL;
}

int main(void)
{
	testPrototype();

	return 0;
}

运行结果如下:



将146行取消注释 程序崩溃。

运行图如下:(图片请在新窗口中查看)



接下来一篇文章解决指针共享的深度拷贝版本。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值