C++设计模式-Prototype原型模式
来源:互联网 发布:战舰世界克利夫兰数据 编辑:程序博客网 时间:2024/06/05 03:17
作用:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
Prototype模式提供了一个通过已存在对象进行新对象创建的接口(Clone), Clone()实现和具体的语言相关,在C++中通过拷贝构造函数实现。
UML图如下:
代码如下:
Prototype.h
1 #ifndef _PROTOTYPE_H_ 2 #define _PROTOTYPE_H_ 3 4 /*Prototype模式提供了一个通过已存在对象进行新对象创建的接口(Clone) 5 Clone()实现和具体的语言相关,在C++中通过拷贝构造函数实现 6 7 作用: 8 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 9 10 */11 12 /*Prototype原型基类,定义Clone接口函数13 */14 class Prototype15 {16 protected:17 Prototype();18 public:19 virtual Prototype* Clone() const=0;//定义Clone接口,根据不同的派生类来实例化对象20 virtual ~Prototype();21 };22 23 //派生自Prototype,实现其接口函数24 class ConcretePrototype1:public Prototype25 {26 public:27 ConcretePrototype1();//构造函数28 ~ConcretePrototype1();//析构函数29 ConcretePrototype1(const ConcretePrototype1&);//拷贝构造函数30 virtual Prototype* Clone() const;//实现基类定义的Clone接口,内部调用拷贝构造函数实现复制功能31 };32 33 //派生自Prototype,实现其接口函数34 class ConcretePrototype2:public Prototype35 {36 public:37 ConcretePrototype2();//构造函数38 ~ConcretePrototype2();//析构函数39 ConcretePrototype2(const ConcretePrototype2&);//拷贝构造函数40 virtual Prototype* Clone() const;//实现基类定义的Clone接口,内部调用拷贝构造函数实现复制功能41 };42 43 #endif
Prototype.cpp
1 #include "Prototype.h" 2 #include "iostream" 3 4 using namespace std; 5 6 ////Prototype 7 Prototype::Prototype() 8 { 9 cout<<"Prototype"<<endl;10 }11 12 Prototype::~Prototype()13 {14 cout<<"~Prototype"<<endl;15 }16 17 //ConcretePrototype118 ConcretePrototype1::ConcretePrototype1()19 {20 cout<<"ConcretePrototype1"<<endl;21 }22 23 ConcretePrototype1::~ConcretePrototype1()24 {25 cout<<"~ConcretePrototype1"<<endl;26 }27 28 ConcretePrototype1::ConcretePrototype1(const ConcretePrototype1& cp)29 {30 cout<<"ConcretePrototype1 copy"<<endl;31 }32 33 Prototype* ConcretePrototype1::Clone() const34 {35 return new ConcretePrototype1(*this);36 }37 38 //ConcretePrototype239 ConcretePrototype2::ConcretePrototype2()40 {41 cout<<"ConcretePrototype2"<<endl;42 }43 44 ConcretePrototype2::~ConcretePrototype2()45 {46 cout<<"~ConcretePrototype2"<<endl;47 }48 49 ConcretePrototype2::ConcretePrototype2(const ConcretePrototype2& cp)50 {51 cout<<"ConcretePrototype2 copy"<<endl;52 }53 54 Prototype* ConcretePrototype2::Clone() const55 {56 return new ConcretePrototype2(*this);57 }
main.cpp
1 #include "Prototype.h" 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 /*原型模式作用:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 8 Prototype模式重在从自身复制自己创建新类,隐藏(不需知道)对象创建的细节 9 */10 11 /*1、用原型实例p1指定创建对象的种类ConcretePrototype1 */12 Prototype* p1 = new ConcretePrototype1();13 14 /*2、通过拷贝这些原型创建新的对象 */15 Prototype* p2 = p1->Clone();16 17 cout<< "------------------------" << endl;18 19 Prototype* p3 = new ConcretePrototype2();20 Prototype* p4 = p3->Clone();21 22 cout<< "------------------------" << endl;23 24 delete p1;25 delete p2;26 cout<< "------------------------" << endl;27 28 delete p3;29 delete p4;30 31 return 0;32 }
Prototype模式和Builder模式、AbstractFactory模式都是通过一个类(对象实例)来专门负责对象的创建工作(工厂对象),它们之间的区别是:Builder模式重在复杂对象的一步步创建(并不直接返回对象),AbstractFactory模式重在产生多个相互依赖类的对象,而Prototype模式重在从自身复制自己创建新类。
0 0
- 设计模式 原型Prototype
- prototype(原型设计模式)
- 设计模式--Prototype(原型)
- Prototype(原型)设计模式
- 设计模式-prototype原型
- 设计模式--原型模式(ProtoType)
- [设计模式]Prototype原型模式
- 设计模式--Prototype(原型模式)
- 设计模式 -- 原型模式Prototype
- 设计模式-原型模式(Prototype)
- [设计模式] 原型模式(Prototype)
- 设计模式-原型模式(Prototype)
- 【设计模式】-原型模式-Prototype
- 设计模式 原型模式 Prototype
- 设计模式:原型模式-prototype
- 设计模式---原型模式(Prototype)
- 设计模式 原型设计 Prototype
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
- C++设计模式-Builder建造者模式
- 在 javascript 中,为什么 [1,2] + [3,4] 不等于 [1,2,3,4]?
- java中的抽象类
- 前端工程师的修真秘籍(css、javascript和其它)
- 修改WordPress中上传附件2M大小限制的方法
- C++设计模式-Prototype原型模式
- Codeforces Div. 2 #259-C. Little Pony and Expected Maximum
- 写给新入IT的新人们
- 如何调试libc++abi.dylib handler threw exception错误
- java基础之内部类
- C程序设计语言(K&R)第五章学习笔记
- 程序员的这一天:也许永远不会到来
- MyEclipse中的SVN操作手册
- C++设计模式-Bridge桥接模式