国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

實(shí)例講解C++編程中對(duì)設(shè)計(jì)模式中的原型模式的使用

2020-01-26 14:42:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

原型模式的實(shí)現(xiàn)完整代碼示例(code):原型模式的實(shí)現(xiàn)很簡(jiǎn)單,這里為了方便初學(xué)者的學(xué)習(xí)和參考,將給出完整的實(shí)現(xiàn)代碼(所有代碼采用 C++實(shí)現(xiàn),并在 VC 6.0 下測(cè)試運(yùn)行)。

代碼片斷 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;}

代碼說(shuō)明:原型模式的結(jié)構(gòu)和實(shí)現(xiàn)都很簡(jiǎn)單,其關(guān)鍵就是(C++中)拷貝構(gòu)造函數(shù)的實(shí)現(xiàn)方式,這也是 C++實(shí)現(xiàn)技術(shù)層面上的事情。由于在示例代碼中不涉及到深層拷貝(主要指有指針、復(fù)合對(duì)象的情況),因此我們通過(guò)編譯器提供的默認(rèn)的拷貝構(gòu)造函數(shù)(按位拷貝)的方式進(jìn)行實(shí)現(xiàn)。說(shuō)明的是這一切只是為了實(shí)現(xiàn)簡(jiǎn)單起見(jiàn),也因?yàn)楸疚臋n的重點(diǎn)不在拷貝構(gòu)造函數(shù)的實(shí)現(xiàn)技術(shù),而在原型模式本身的思想。

另一個(gè)實(shí)例

我們?cè)賮?lái)看一個(gè)具體項(xiàng)目的例子:

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#對(duì)原型模式的支持

在C#里面,我們可以很容易的通過(guò)Clone()方法實(shí)現(xiàn)原型模式。任何類,只要想支持克隆,必須實(shí)現(xiàn)C#中的ICloneable接口。ICloneable接口中有一Clone方法,可以在類中復(fù)寫(xiě)實(shí)現(xiàn)自定義的克隆方法。克隆的實(shí)現(xiàn)方法有兩種:淺拷貝(shallow copy)與深拷貝(deep copy)。
淺拷貝與深拷貝

下面給出淺拷貝與深拷貝的兩個(gè)例子,例子使用了ICloneable接口。C#中的數(shù)組是引用型的變量,我們通過(guò)數(shù)組來(lái)進(jìn)行演示:

淺拷貝:

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對(duì)象實(shí)現(xiàn)了一個(gè)淺拷貝,因此當(dāng)對(duì)sc1進(jìn)行克隆時(shí),其字段v并沒(méi)有克隆,這導(dǎo)致sc1與sc2的字段v都指向了同一個(gè)v,因此,當(dāng)修改了sc1的v[0]后,sc2的v[0]也發(fā)生了變化。

深拷貝:

using System;class DeepCopy : ICloneable{ public int[] v = {1,2,3}; // 默認(rèn)構(gòu)造函數(shù) public DeepCopy() { } // 供Clone方法調(diào)用的私有構(gòu)造函數(shù) private DeepCopy(int[] v) { this.v = (int[])v.Clone(); } public Object Clone() { // 構(gòu)造一個(gè)新的DeepCopy對(duì)象,構(gòu)造參數(shù)為 // 原有對(duì)象中使用的 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(); }}

關(guān)于原型模式的討論

原型模式通過(guò)復(fù)制原型(原型)而獲得新對(duì)象創(chuàng)建的功能,這里原型本身就是"對(duì)象工廠"(因?yàn)槟軌蛏a(chǎn)對(duì)象),實(shí)際上原型模式和 Builder 模式、AbstractFactory 模式都是通過(guò)一個(gè)類(對(duì)象實(shí)例)來(lái)專門負(fù)責(zé)對(duì)象的創(chuàng)建工作(工廠對(duì)象),它們之間的區(qū)別是: Builder 模式重在復(fù)雜對(duì)象的一步步創(chuàng)建(并不直接返回對(duì)象),AbstractFactory 模式重在產(chǎn)生多個(gè)相互依賴類的對(duì)象,而原型模式重在從自身復(fù)制自己創(chuàng)建新類。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐至县| 邯郸市| 张家界市| 二连浩特市| 嫩江县| 乐陵市| 吴江市| 荃湾区| 荆州市| 遵义县| 闽清县| 正阳县| 竹北市| 嘉善县| 吉安市| 平原县| 商都县| 沁水县| 阿图什市| 屏东市| 定远县| 阳泉市| 炉霍县| 博爱县| 中卫市| 嘉善县| 云梦县| 临武县| 沅江市| 贵溪市| 神农架林区| 台江县| 梁平县| 普兰县| 靖西县| 工布江达县| 沂水县| 隆昌县| 张北县| 宜城市| 渭南市|