(六)原型模式
来源:互联网 发布:java实体是什么 编辑:程序博客网 时间:2024/05/16 08:06
原型模式(prototype):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。[DP]
UML类图:
代码:
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "stdafx.h"#include <string>#include <iostream>#include <memory>//为了应用智能指针using namespace std;//教育经历class CEducation{public: CEducation(const string &strshcool, const string &strprofession) : strshcool(strshcool), strprofession(strprofession){} ~CEducation(){} string Getsch(){ return strshcool; } string Getpro(){ return strprofession; }private: string strshcool;//学校 string strprofession;//专业};//原型class ProtoType{public: virtual ~ProtoType(){ delete pedu; } ProtoType(const string &strname, const string &strsex, CEducation* pedu) : strname(strname), strsex(strsex), pedu(pedu){} string Getnm(){ return strname; } string Getsex(){ return strsex; } CEducation * Getedu(){ return pedu; } virtual ProtoType *clone() = 0;private: string strname;//姓名 string strsex;//性别 CEducation *pedu;//学历};//一个具体的原型class ConcretePrototype1 : public ProtoType{public: ConcretePrototype1(const string &strname, const string &strsex, CEducation *pedu) : ProtoType(strname, strsex, pedu){} ConcretePrototype1 *clone() { return new ConcretePrototype1(*this); } void Setwkexp(string str){ strworkexp = str; } string Getwkexp(){ return strworkexp; } void display() { cout << "姓名:" << Getnm() << endl; cout << "性别:" << Getsex() << endl; cout << "学历:" << "毕业院校:" << Getedu()->Getsch() << "专业:" << Getedu()->Getpro() << endl; cout << "工作经历:" << Getwkexp() << endl; cout << endl; }private: string strworkexp;//工作经历};int _tmain(int argc, _TCHAR* argv[]){ //张三丰的第一份简历 CEducation *edu = new CEducation("河北北方学院", "电子信息科学与技术"); ConcretePrototype1 *resum1 = new ConcretePrototype1("张三丰", "男", edu); resum1->Setwkexp("2011-2013 IBM公司担任C++程序员"); //利用张三丰简历原型克隆,变换工作经验后的新简历 ConcretePrototype1 *resum2 = resum1->clone(); resum2->Setwkexp("公元前2011-2013 微软公司担任C++程序员"); resum1->display(); resum2->display(); return 0;}
上例中,ConcretePrototype1类使我们要使用的原型类,ProtoType类符合面向接口的编程,clone方法作为抽象类的主要接口,以后如果想创建新型的原型可以从此类派生。这里只是简单举例派生出一个简单的ConcretePrototype1类。
这个例子的思路源自于大话设计模式,我在这里把简历的基本信息更加具体了,将姓名,性别,学历沉淀到了ProtoType类中,而具体的原型类ConcretePrototype1中可以自由定制工作经历,这样就是针对某个人的简历原型,因为姓名,性别,学历这些信息再实际简历编写中不会发生改变,而具体的工作经历可能要针对不同的公司做调整。
在客户端调用时,我们只需new一次ConcretePrototype1,
ConcretePrototype1 *resum1 = new ConcretePrototype1("张三丰", "男", edu);
以后就是clone操作,然后再进行定制化设置。
//利用张三丰简历原型克隆,变换工作经验后的新简历 ConcretePrototype1 *resum2 = resum1->clone(); resum2->Setwkexp("公元前2011-2013 微软公司担任C++程序员");
注意,在这里有个学历类CEducation,如果发生需要定制学历的话,由于我们没有在clone中重写CEducation的拷贝方式(有两种,深拷贝和浅拷贝),所以采用指针方式的学历是共享的,也就是当
//利用张三丰简历原型克隆,变换工作经验后的新简历 ConcretePrototype1 *resum2 = resum1->clone();
时,学历在内存中是共享的,假设我们要定制学历,改怎么办呢?(这里不考虑造假学历的道德问题,呵呵)
实际上这是C++的基础问题,我们只需要重新实现ProtoType类的拷贝构造函数即可:
ProtoType(ProtoType &pt) { //对学历进行了位拷贝,也就是“深复制” cout << "对学历进行深拷贝" << endl; CEducation *e = new CEducation(pt.Getedu()->Getsch(), pt.Getedu()->Getpro()); pedu = e; }
当然还得加上对学历信息的设置函数:
void Setsch(const string &strsch){ strshcool = strsch; } void Setpro(const string &strpro){ strprofession = strpro; }
客户端做相应的设置:
int _tmain(int argc, _TCHAR* argv[]){ //张三丰的第一份简历 CEducation *edu = new CEducation("河北北方学院", "电子信息科学与技术"); ConcretePrototype1 *resum1 = new ConcretePrototype1("张三丰", "男", edu); resum1->Setwkexp("2011-2013 IBM公司担任C++程序员"); //利用张三丰简历原型克隆,变换工作经验后的新简历 ConcretePrototype1 *resum2 = resum1->clone(); resum2->Getedu()->Setsch("清华大学"); resum2->Getedu()->Setpro("土木工程"); resum2->Setwkexp("公元前2011-2013 微软公司担任C++程序员"); resum1->display(); resum2->display(); return 0;}
执行结果:
关于这些内存如何处理的问题,等我有时间再处理吧,今天先到这里。
1 0
- (六)原型模式
- 六、原型模式(Prototype)
- 设计模式(六)---原型模式
- 设计模式(六)原型模式
- 设计模式(六)原型模式
- 设计模式—原型模式(六)
- 设计模式(六):原型模式
- 模式六(原型模式)
- 六、原型模式
- 设计模式实现(六)--- 原型模式(Prototype)
- C#设计模式(六)原型模式(Prototype Pattern)
- 设计模式学习笔记六(Prototype原型模式)
- 设计模式之python版(六)--原型模式
- Java设计模式之六(原型模式)
- 设计模式(六):原型模式——浅复制
- 设计模式(六):原型模式——深复制
- 设计模式在游戏中的应用--原型模式(六)
- (六)Java设计模式之原型模式
- 【BZOJ 3207】花神的嘲讽计划Ⅰ(Hash + 可持久化线段树)
- POJ 3273 Monthly Expense
- AC自动机
- Matlab 脚本编译成dll被c++调用
- TextView属性大全
- (六)原型模式
- 计算机网络总结笔记
- 锁
- vmware Tools 全系列版本下载及安装方法(vmware Tools 10~12)
- linux read
- #和##在宏中的妙用
- USB集腋成裘:从Linux PC角度看sys.usb.config为none时的Android设备
- 拿到一份开源项目,快速定位感兴趣的功能代码
- 3.关于removeFromSuperview和addSubview