博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
原型模式
阅读量:4324 次
发布时间:2019-06-06

本文共 2524 字,大约阅读时间需要 8 分钟。

【1】什么是原型模式?

原型模式即复制,或者克隆模式。

【2】原型模式代码示例:

代码示例1:

1 #include 
2 #include
3 using namespace std; 4 5 class Prototype 6 { 7 private: 8 string str; 9 public:10 Prototype(string s)11 {12 str = s;13 }14 Prototype()15 {16 str = "";17 }18 void show()19 {20 cout << str << endl;21 }22 virtual Prototype *clone() = 0;23 };24 25 class ConcretePrototype1 : public Prototype26 {27 public:28 ConcretePrototype1(string s) : Prototype(s)29 {}30 ConcretePrototype1(){}31 virtual Prototype *clone()32 {33 ConcretePrototype1 *p = new ConcretePrototype1();34 *p = *this;35 return p;36 }37 };38 39 40 class ConcretePrototype2 : public Prototype41 {42 public:43 ConcretePrototype2(string s) : Prototype(s)44 {}45 ConcretePrototype2(){}46 virtual Prototype *clone()47 {48 ConcretePrototype2 *p = new ConcretePrototype2();49 *p = *this;50 return p;51 }52 };53 54 int main()55 {56 ConcretePrototype1 *test = new ConcretePrototype1("小李");57 ConcretePrototype2 *test2 = (ConcretePrototype2 *)test->clone();58 test->show();59 test2->show();60 return 0;61 }
View Code

代码示例2:

1 #include 
2 #include
3 using namespace std; 4 5 class Resume 6 { 7 private: 8 string name, sex, age, timeArea, company; 9 public:10 Resume(string s)11 {12 name = s;13 }14 void setPersonalInfo(string s, string a)15 {16 sex = s;17 age = a;18 }19 void setWorkExperience(string t, string c)20 {21 timeArea = t;22 company = c;23 }24 void display()25 {26 cout << name << " " << sex << " " << age << endl;27 cout << "工作经历: " << timeArea << " " << company << endl;28 29 }30 Resume *clone()31 {32 Resume *b = new Resume(name);33 b->setPersonalInfo(sex, age);34 b->setWorkExperience(timeArea, company);35 return b;36 }37 };38 39 40 int main()41 {42 Resume *r = new Resume("李俊宏"); 43 r->setPersonalInfo("男","26");44 r->setWorkExperience("2007-2010","读研究生");45 r->display();46 47 48 Resume *r2 = r->clone();49 r2->setWorkExperience("2003-2007","读本科");50 51 r->display();52 r2->display();53 54 return 0;55 }
View Code

 

Good   Good   Study,  Day   Day  Up.

顺序   选择   循环   总结

转载于:https://www.cnblogs.com/Braveliu/p/3942411.html

你可能感兴趣的文章
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
64位MATLAB和C混合编程以及联合调试
查看>>
原生js大总结二
查看>>
PHP基础
查看>>
UVa 11488 超级前缀集合(Trie的应用)
查看>>
Django 翻译与 LANGUAGE_CODE
查看>>
[转]iOS教程:SQLite的创建数据库,表,插入查看数据
查看>>
【转载】OmniGraffle (一)从工具栏开始
查看>>
初识ionic
查看>>
java 中打印调用栈
查看>>
开发 笔记
查看>>