您的位置:首页 > 其它

6.原型模式

2015-06-07 22:46 330 查看
1.说明

请参阅本文第一章

2.原型模式说明

原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象

通俗说话:创建一个和原来一模一样的对象,减少代码中使用动态来new的方式,并且不用知道任何的创建细节

3.UML



4.代码

//个人信息类,通过不同的数据类型来测试原型模式中的复制状况
//当原型中的为指针类型时,为浅复制,其余均为深复制,且会调用复制构造函数

//BaseClone.h 原型基类接口
#ifndef __BASECLONE_H
#define __BASECLONE_H

#include <iostream>
#include <string>
class CBaseClone
{
public:
virtual CBaseClone* Clone(){}
};
#endif

//个人信息类
#ifndef __WORKEXPRIENCE_H
#define __WORKEXPRIENCE_H

#include <iostream>

class CWorkExprience
{
public:
void SetInfo(std::string time, std::string company)
{
m_time = time;
m_company = company;
}
std::string GetTime()
{
return m_time;
}
std::string GetCompany()
{
return m_company;
}
private:
std::string m_time;
std::string m_company;
};

#endif

//原型模式构造类

#ifndef __RESUME_H
#define __RESUME_H

#include <vector>
#include "BaseClone.h"
#include "WorkExprience.h"

//在原型模式中当 为值类型,类类型,复合类型(vector)均为 深复制
//指针类型为浅复制

class CResume:public CBaseClone
{
public:
CResume()
{

}
/*CResume(CResume& par)
{
std::cout<<"this is nothing."<<std::endl;
}*/
void SetResumeName(std::string resuname)
{
m_resumename = resuname;
}
void SetInfo(std::string name, std::string sex)
{
m_name = name;
m_sex = sex;
}
//深浅复制测试区
void AddWorkExprience(std::string time, std::string company)
{
m_exprience.SetInfo(time, company);
}
void Add_set(std::string time, std::string company)
{
CWorkExprience exper;
exper.SetInfo(time, company);
m_exset.push_back(exper);
}
void Set_mon(int* p)
{
if (p != NULL)
m_mon = p;
}
//测试结束
void Display()
{
std::cout<<"resuname:"<<m_resumename.c_str()<<"  "<<m_name.c_str()<<"  "<<m_sex.c_str()<<std::endl;
std::cout<<m_exprience.GetTime()<<"  "<<m_exprience.GetCompany()<<std::endl;

std::cout<<"setInfo:"<<std::endl;
for (std::vector<CWorkExprience>::iterator it=m_exset.begin(); it!=m_exset.end(); it++)
{
std::cout<<it->GetTime()<<"  "<<it->GetCompany()<<std::endl;
}
std::cout<<"set endl"<<std::endl;
std::cout<<"point test "<<*m_mon<< std::endl;
}
CResume* Clone()
{
return new CResume(*this);              //此处会调用构造函数
}
private:
std::string m_resumename;
std::string m_name;
std::string m_sex;
//std::vector<>
CWorkExprience m_exprience;
std::vector<CWorkExprience> m_exset;        //集合 用于测试clone时 是深复制还是浅复制
int* m_mon;
};

#endif

//客户端调用
#include <iostream>
#include <string>
#include "Resume.h"
#include "BaseClone.h"

int main(void)
{
CResume* resume = new CResume();
resume->SetResumeName("zp resume");
resume->SetInfo("ZP", "BOY");
resume->AddWorkExprience("2014-2015", "GHCA");
resume->Add_set("1990-1995", "CHANGHONG");
resume->Add_set("1996-1999", "GUANZI");
resume->Add_set("2000-2003", "YILONG");
int* mon = new int(20);
resume->Set_mon(mon);

std::cout<<"Init info"<<std::endl;
resume->Display();

std::cout<<std::endl<<std::endl;
std::cout<<"light info"<<std::endl;
CResume* light = (CResume* )resume->Clone();
light->SetResumeName("Light Resume");
light->AddWorkExprience("2013-2014", "SIFANG");
light->Add_set("2004-2009", "LANGZHONG");
light->Add_set("2009-2012", "CHENGDU");
*mon = 30;
light->Set_mon(mon);
light->Display();

std::cout<<std::endl<<std::endl<<std::endl;
std::cout<<"zp info"<<std::endl;
resume->Display();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: