大话设计模式--原型模式 Prototype -- C++实现
来源:互联网 发布:现货交易app软件排名 编辑:程序博客网 时间:2024/06/03 15:57
1. 原型模式: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。。。
注意: 拷贝的时候是浅拷贝 还是 深拷贝, 来考虑是否需要重写拷贝构造函数。
关键在于: virtual Prototype* clone(); 克隆函数。。。返回一个克隆的对象。
实例: 以深拷贝为例
prototype.h prototype.cpp
#ifndef PROTOTYPE_H#define PROTOTYPE_Hclass Prototype{public: int a; char *str; Prototype(int b, char* cstr); Prototype(const Prototype &cp); ~Prototype(); void show(); virtual Prototype* clone();};#endif // PROTOTYPE_H
#include "prototype.h"#include <string.h>#include <stdio.h>Prototype::Prototype(int b, char* cstr){ a = b; str = new char[b]; strcpy(str, cstr);}Prototype::~Prototype(){ delete str;}Prototype::Prototype(const Prototype &cp){ a = cp.a; str = new char[a]; if(str!=0) strcpy(str, cp.str);}Prototype* Prototype::clone(){ return new Prototype(a, str);}void Prototype::show(){ printf("a: %d, str: %s\n", a, str);}
main.cpp
#include <iostream>#include "prototype.h"#include <string.h>#include <stdio.h>using namespace std;int main(){ cout << "Prototype test !" << endl; Prototype *p = new Prototype(6, "hello"); Prototype *p1 = p->clone(); p1->show(); return 0;}
- 大话设计模式--原型模式 Prototype -- C++实现
- 读《大话设计模式》---原型模式(Prototype)
- 大话设计模式九:原型模式(Prototype)
- 《大话设计模式》--简历复印--原型模式<Prototype>(9)
- 大话设计模式c++实现--原型模式
- 设计模式 原型Prototype
- prototype(原型设计模式)
- 设计模式--Prototype(原型)
- Prototype(原型)设计模式
- 设计模式-prototype原型
- 大话设计模式--原型模式
- 大话设计模式-原型模式
- 大话设计模式-原型模式
- 大话设计模式-原型模式
- 大话设计模式----原型模式
- 设计模式--原型模式(ProtoType)
- [设计模式]Prototype原型模式
- 设计模式--Prototype(原型模式)
- 使用 Alchemy 技术编译 C 语言程序为 Flex 可调用的 SWC
- Linux系统下,CPU信息详解(cpuinfo,多核,多线程)
- 进程与线程的一个简单解释
- TortoiseSVN中分支和合并实践
- ASP.NET大文件上传设置maxRequestLength无效的情况
- 大话设计模式--原型模式 Prototype -- C++实现
- 为什么要用面向对象编程
- 订阅发布模式(Subscribe/Publish)
- 堆和栈的区别
- compass学习笔记(绝对经典)
- 读书笔记——数据库的ADO开发总结
- SQLite学习手册(内存数据库)
- Logistic regression (逻辑回归) 概述
- 专注网格剖分 - TetGen