Prototype原型模式
来源:互联网 发布:匿名四轴飞控源码 编辑:程序博客网 时间:2024/06/05 23:39
Prototype原型模式。用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式就是用一个对象来创建另一个相同的对象而无需知道创建的具体细节。而且大大提高了创建的效率。优点主要是这两个:
- 屏蔽创建的具体细节,如参数等。
- 创建的效率高。因为不必调用构造函数等。
原型模式也是一种创建型模式,跟建造者模式,工厂模式系类一样,不同的是,建造者模式侧重于一步一步建造对象,工厂模式侧重于多个类的依赖。相同的是都是通过一个类(对象实例)来专门负责对象的创建工作。
使用原型模式要先了解下拷贝构造函数的深拷贝和浅拷贝。
代码:
//Prototype.h
#include "stdafx.h"#include<iostream>#include<cstring>using namespace std;class Prototype{public:virtual ~Prototype(){cout << "~Prototype" << endl;}virtual Prototype* clone()const = 0;virtual void show()const = 0;};class ConcreteProtype1 :public Prototype{public:ConcreteProtype1(int len,char*str){_length = len;_str = new char[_length];strcpy_s(_str, _length, str);}~ConcreteProtype1(){delete(_str);cout << "~ConcreteProtype" << endl;}ConcreteProtype1(const ConcreteProtype1& rhs){//实现深拷贝_length = rhs._length;_str = new char[_length];if (_str != NULL)strcpy_s(_str, _length, rhs._str);cout << "copy construct ConcreteProtype1" << endl;}virtual Prototype* clone()const{return new ConcreteProtype1(*this);}virtual void show()const{cout <<"value:"<< _str << endl;cout << "Address:" << &_str << endl;}private:int _length;char* _str;};
// PrototypePattern.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "Prototype.h"int _tmain(int argc, _TCHAR* argv[]){Prototype* p1 = new ConcreteProtype1(6,"hello");Prototype *p2 = p1->clone();p1->show();p2->show();getchar();return 0;}
0 0
- 原型模式 Prototype模式
- 原型模式 Prototype模式
- 原型模式:prototype模式
- 原型模式 Prototype模式
- Prototype模式(原型模式)
- 原型模式:prototype模式
- 原型模式(Prototype)
- 原型模式(prototype)
- 原型(Prototype)模式
- 原型模式(Prototype Pattern)
- 原型(ProtoType)模式
- 原型模式(Prototype Pattern)
- 原型模式(Prototype Pattern)
- 原型模式 Prototype Pattern
- Prototype 原型模式
- 原型模式(Prototype)
- 原型模式 Prototype Pattern
- Prototype 原型模式
- JavaScriptSerializer序列化成Json时DateTime类型数据的处理
- 测试题第二版
- ORA-03113: 通信通道的文件结束
- php免费接口API 分享 各大功能
- SVN分支与合并
- Prototype原型模式
- sql server 带有OUTPUT的INSERT,DELETE,UPDATE
- parted 显示单位
- (二)glassFish提供的rest接口的post方法
- MySQL问题解决:-bash:mysql:command not found
- 华为面试题java
- javaScript 动态生成密码强度
- UDP数据包大小问题
- 最长公共子序列,最大递增子序列,最长公共递增子序列