PROTOTYPE(原型)—对象创建型模式
来源:互联网 发布:炎锤网络 编辑:程序博客网 时间:2024/05/21 07:51
提供一种创建对象方式,通过复制已经存在的对象产生新的对象。该模式的一个特点是,客户端在创建一个实例时候,不需要知道实例的具体类型。实现要有一个对应类的原型对象。在c++,该模式可方便用于反射。
举例:
1、定义原形接口 VehicleProtoType
//申明原型
class VehicleProtoType
{
//声明一个接口
public:
virtual VehicleProtoType *Clone() =0;
};
2 实现一个接口
///////////////实现 接口
//轿车类
class Car :public VehicleProtoType
{
public:
int a;
public:
Car();
Car(Car& car);
Car *Clone() ;
};
Car ::Car(){}
Car::Car(Car& car){
a =200;
}
Car *Car::Clone(){
return new Car(*this);
}
/////////////////////////////////////
//公交车
class Bus :public VehicleProtoType
{
public:
int b;
public:
Bus();
Bus(Bus& bus);
Bus *Clone() ;
};
Bus ::Bus(){}
Bus::Bus(Bus& bus){
b =200;
}
Bus *Bus::Clone(){
return new Bus(*this);
}
3、客户端使用接口
int _tmain(int argc, _TCHAR* argv[])
{
Car *v1 = new Car();
v1->a=100;
VehicleProtoType *v2 = v1->Clone();
delete v1;
delete v2;
return 0;
}
说明:每个子类必须实现克隆自身的接口。动态创建一个对象,特别是组合对象为一个新对象时候,可以大大简化类的数目。
- PROTOTYPE(原型)—对象创建型模式
- 设计模式之对象创建型 — prototype 原型模式
- 设计模式之Prototype(原型模式)对象创建型
- 原型模式(Prototype Pattern对象创建型模式)
- Prototype(原型)---对象创建型模式
- 1.4 Prototype(原型) -- 对象创建型模式
- 设计模式之十:Prototype(原型)—对象创建型模式
- 创建型模式-----原型模式(Prototype)
- Prototype 原型(创建型模式)
- 创建型模式:Prototype(原型)
- Prototype 原型(创建型模式) 笔记
- java原型模式(Prototype)-创建型
- 四、原型模式Prototype(创建型)
- 创建型:原型模式(Prototype)
- Prototype原型(创建型模式)
- Prototype原型(创建型模式)
- 创建型-原型模式(prototype)
- 设计模式学习之--PROTOTYPE(原型)对象创建型模型
- GPU编程之CUDA(三)——关于联想Y470
- 利用ajaxfileupload.js实现跨域上传图片并处理返回的数据
- 使用MariaDB集群运维可能涉及的细节之binarylog
- 建有序链表,还是要打好基础的。。。
- UML时序图画法
- PROTOTYPE(原型)—对象创建型模式
- 宽度全屏SeekBar实现
- 传媒经营之交通广播电台广告的独特个性
- 使用Java实现ping的功能
- OCR 人像识别引擎
- Python 正则表达式查找相邻字符
- ZOJ 3295 War of 218
- 【ThinkingInC++】27、全局作用域解析
- AMQP学习笔记(一)