原型模式(ProtoType)
来源:互联网 发布:excel macro编程 编辑:程序博客网 时间:2024/06/05 08:52
1.目的
当需要从一个对象copy出另外一个对象时,可以使用原型模式,本质上是调用的copy构造函数
与copy构造函数的区别,可能就是不需要显示的生成对象,而是通过函数调用(?)
2.代码
prototype.h
#ifndef PROTOTYPE_H_#define PROTOTYPE_H_#include <string>#include <iostream>using namespace std;class Resume{public:Resume(string name, string sex, string age):name(name), sex(sex),age(age){}Resume(const Resume& arg){this->name = arg.name;this->age = arg.age;this->sex = arg.sex;this->workExperience = arg.workExperience;}void setWorkExperience(string arg){workExperience = arg;}Resume& clone() const{return *(new Resume(*this));}void show(){cout<<name<<" "<<sex<<" "<<age<<endl;cout<<workExperience<<endl;}private:string name;string sex;string age;string workExperience;};#endif /* PROTOTYPE_H_ */
prototype.cpp
#include "ProtoType.h"void protoType(){Resume a("Tom", "male", "30");a.setWorkExperience("1998-2000, Company A");Resume b = a.clone();b.setWorkExperience("2000-2002, Company B");Resume c = a.clone();c.setWorkExperience("2002-2004, Company C");a.show();b.show();c.show();}
执行效果:
Tom male 30
1998-2000, Company A
Tom male 30
2000-2002, Company B
Tom male 30
2002-2004, Company C
0 0
- 原型模式(Prototype)
- 原型模式(prototype)
- 原型(Prototype)模式
- 原型(ProtoType)模式
- 原型模式(Prototype)
- 原型模式(Prototype)
- Prototype(原型模式)
- 原型(Prototype)模式
- Prototype(原型模式)
- PROTOTYPE(原型)模式
- 原型模式(ProtoType)
- 原型(prototype)模式
- 原型模式(Prototype)
- 原型模式(Prototype)
- 原型模式(Prototype)
- 原型模式(prototype)
- 原型模式(prototype)
- Prototype(原型模式)
- Axis2 创建 web service (3) : 示例解读
- 安装mysql和memcached
- Android——使用SQLite数据库访问
- Tchart的旋转动画 (评估板源码)
- C++ 构造/析构函数中的异常处理
- 原型模式(ProtoType)
- 【LeetCode】Word Break
- RMQ and LCA
- java中的构造函数有什么用?用它有什么好处?在什么地方用?
- 单元测试 Google Test 使用
- 【iOS】KVC 和 KVO 的使用场景
- Robotium创建一个Note的实例
- mysql 字段类型对查询效率的影响实验及小范围取值字段不宜添加索引
- Unity & Metal