手机
当前位置:查字典教程网 >编程开发 >C语言 >实例讲解C++编程中对设计模式中的原型模式的使用
实例讲解C++编程中对设计模式中的原型模式的使用
摘要:原型模式的实现完整代码示例(code):原型模式的实现很简单,这里为了方便初学者的学习和参考,将给出完整的实现代码(所有代码采用C++实现,...

原型模式的实现完整代码示例(code):原型模式的实现很简单,这里为了方便初学者的学习和参考,将给出完整的实现代码(所有代码采用 C++实现,并在 VC 6.0 下测试运行)。

代码片断 1:Prototype.h

//Prototype.h #ifndef _PROTOTYPE_H_ #define _PROTOTYPE_H_ class Prototype{ public: virtual ~Prototype(); virtual Prototype* Clone() const = 0; protected: Prototype(); private: }; class ConcretePrototype:public Prototype{ public: ConcretePrototype(); ConcretePrototype(const ConcretePrototype& cp); ~ConcretePrototype(); Prototype* Clone() const; protected: private: }; #endif //~_PROTOTYPE_H_

代码片断 2:Prototype.cpp

//Prototype.cpp #include "Prototype.h" #include <iostream> using namespace std; Prototype::Prototype(){ } Prototype::~Prototype(){ } Prototype* Prototype::Clone() const{ return 0; } ConcretePrototype::ConcretePrototype(){ } ConcretePrototype::~ConcretePrototype(){ } ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp){ cout<<"ConcretePrototype copy ..."<<endl; } Prototype* ConcretePrototype::Clone() const{ return new ConcretePrototype(*this); }

代码片断 3:main.cpp

//main.cpp #include "Prototype.h" #include <iostream> using namespace std; int main(int argc,char* argv[]){ Prototype* p = new ConcretePrototype(); Prototype* p1 = p->Clone(); return 0; }

代码说明:原型模式的结构和实现都很简单,其关键就是(C++中)拷贝构造函数的实现方式,这也是 C++实现技术层面上的事情。由于在示例代码中不涉及到深层拷贝(主要指有指针、复合对象的情况),因此我们通过编译器提供的默认的拷贝构造函数(按位拷贝)的方式进行实现。说明的是这一切只是为了实现简单起见,也因为本文档的重点不在拷贝构造函数的实现技术,而在原型模式本身的思想。

另一个实例

我们再来看一个具体项目的例子:

namespace Prototype_DesignPattern { using System; // Objects which are to work as prototypes must be based on classes which // are derived from the abstract prototype class abstract class AbstractPrototype { abstract public AbstractPrototype CloneYourself(); } // This is a sample object class MyPrototype : AbstractPrototype { override public AbstractPrototype CloneYourself() { return ((AbstractPrototype)MemberwiseClone()); } // lots of other functions go here! } // This is the client piece of code which instantiate objects // based on a prototype. class Demo { private AbstractPrototype internalPrototype; public void SetPrototype(AbstractPrototype thePrototype) { internalPrototype = thePrototype; } public void SomeImportantOperation() { // During Some important operation, imagine we need // to instantiate an object - but we do not know which. We use // the predefined prototype object, and ask it to clone itself. AbstractPrototype x; x = internalPrototype.CloneYourself(); // now we have two instances of the class which as as a prototype } } /// <summary> /// Summary description for Client. /// </summary> public class Client { public static int Main(string[] args) { Demo demo = new Demo(); MyPrototype clientPrototype = new MyPrototype(); demo.SetPrototype(clientPrototype); demo.SomeImportantOperation(); return 0; } } }

C#对原型模式的支持

在C#里面,我们可以很容易的通过Clone()方法实现原型模式。任何类,只要想支持克隆,必须实现C#中的ICloneable接口。ICloneable接口中有一Clone方法,可以在类中复写实现自定义的克隆方法。克隆的实现方法有两种:浅拷贝(shallow copy)与深拷贝(deep copy)。

浅拷贝与深拷贝

下面给出浅拷贝与深拷贝的两个例子,例子使用了ICloneable接口。C#中的数组是引用型的变量,我们通过数组来进行演示:

浅拷贝:

using System; class ShallowCopy : ICloneable { public int[] v = {1,2,3}; public Object Clone() { return this.MemberwiseClone(); } public void Display() { foreach(int i in v) Console.Write( i + ", "); Console.WriteLine(); } } class Client { public static void Main() { ShallowCopy sc1 = new ShallowCopy(); ShallowCopy sc2 = (ShallowCopy)sc1.Clone(); sc1.v[0] = 9; sc1.Display(); sc2.Display(); } }

ShallowCopy对象实现了一个浅拷贝,因此当对sc1进行克隆时,其字段v并没有克隆,这导致sc1与sc2的字段v都指向了同一个v,因此,当修改了sc1的v[0]后,sc2的v[0]也发生了变化。

深拷贝:

using System; class DeepCopy : ICloneable { public int[] v = {1,2,3}; // 默认构造函数 public DeepCopy() { } // 供Clone方法调用的私有构造函数 private DeepCopy(int[] v) { this.v = (int[])v.Clone(); } public Object Clone() { // 构造一个新的DeepCopy对象,构造参数为 // 原有对象中使用的 v return new DeepCopy(this.v); } public void Display() { foreach(int i in v) Console.Write( i + ", "); Console.WriteLine(); } } class Client { public static void Main() { DeepCopy dc1 = new DeepCopy(); DeepCopy dc2 = (DeepCopy)dc1.Clone(); dc1.v[0] = 9; dc1.Display(); dc2.Display(); } }

关于原型模式的讨论

原型模式通过复制原型(原型)而获得新对象创建的功能,这里原型本身就是"对象工厂"(因为能够生产对象),实际上原型模式和 Builder 模式、AbstractFactory 模式都是通过一个类(对象实例)来专门负责对象的创建工作(工厂对象),它们之间的区别是: Builder 模式重在复杂对象的一步步创建(并不直接返回对象),AbstractFactory 模式重在产生多个相互依赖类的对象,而原型模式重在从自身复制自己创建新类。

【实例讲解C++编程中对设计模式中的原型模式的使用】相关文章:

深入理解C++中常见的关键字含义

从汇编看c++中多态的应用

C++类URL编码和解码使用技巧

基于C语言字符串函数的一些使用心得

C++中函数模板的用法详细解析

解析C++中虚析构函数的作用

基于C++类型重定义的使用详解

C++中引用的使用总结

C++中点操作符和箭头操作符的使用详解

C++中“#”号的使用技巧

精品推荐
分类导航