赞
踩
本文介绍设计模式中的原型模式。
本质上其实就是克隆。
下面以个人简历为例进行举例说明:
深拷贝版本:
- #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
- stri
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。