设计模式-创建型-原型

#pragma once

#include <iostream>
#include <string>
#include <list>
using namespace std;

class cloneable
{
public:
	virtual cloneable* clone() = 0;
};

class work_experience:public cloneable
{
public:
	cloneable* clone()
	{
		work_experience *obj = new work_experience;
		obj->work_date = this->work_date;
		obj->company = this->company;
		return obj;
	}
private:
	string work_date;
	string company;
};

class resume:public cloneable
{
public:
	void set_personal_info(string name, string age, string sex,string phone)
	{
		name_ = name;
		age_ = age;
		sex_ = sex;
		phone_ = phone;
	}
	void set_work_experience(string date,string company)
	{
		work_exp_list_.push_back(make_pair(date,company));
	}
	cloneable* clone()
	{
		resume *obj = new resume;
		obj->name_ = this->name_;
		obj->age_ = this->age_;
		obj->sex_ = this->sex_;
		obj->phone_ = this->phone_;
		obj->work_exp_list_ = this->work_exp_list_;
		return obj;
	}
	void print()
	{
		cout<<"info"<<endl;
		cout<<"name:"<<name_<<" age:"<<age_<<" sex:"
			<<sex_<<" phone:"<<phone_<<endl;
		cout<<"exper"<<endl;
		for (list< pair<string,string> >::iterator itr = work_exp_list_.begin();
			itr!=work_exp_list_.end();++itr)
		{
			cout<<"date:"<<itr->first<<" company:"<<itr->second<<endl;
		}
		cout<<endl;
	}
private:
	string name_;
	string age_;
	string sex_;
	string phone_;
	list< pair<string,string> > work_exp_list_;
};

#include "StdAfx.h"
#include "prototype_impl.h"


// Prototype.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "prototype_impl.h"

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

int _tmain(int argc, _TCHAR* argv[])
{
	//创建原型对象
	resume *first_work = new resume;
	first_work->set_personal_info("jack","25","name","875964");
	first_work->set_work_experience("2012-2015","HP");
	first_work->print();
	//resume2 *first_work2 = new resume2;

	//创建新对象并拷贝原型对象的内容
	resume *second_work = static_cast<resume *>(first_work->clone());
	second_work->set_work_experience("2015-2017","google");
	second_work->print();
	//resume2 *second_work2 = first_work2->clone();

	delete first_work;
	delete second_work;

	system("pause");

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值