亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2016年03月09日 11:59:03   作者:夢在天涯  
這篇文章主要介紹了C++編程中對設(shè)計模式中的原型模式的使用實例,包括原型模式中對C++的深拷貝和淺拷貝的處理,需要的朋友可以參考下

原型模式的實現(xiàn)完整代碼示例(code):原型模式的實現(xiàn)很簡單,這里為了方便初學(xué)者的學(xué)習(xí)和參考,將給出完整的實現(xiàn)代碼(所有代碼采用 C++實現(xiàn),并在 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;
}

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

另一個實例

我們再來看一個具體項目的例子:

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()方法實現(xiàn)原型模式。任何類,只要想支持克隆,必須實現(xiàn)C#中的ICloneable接口。ICloneable接口中有一Clone方法,可以在類中復(fù)寫實現(xiàn)自定義的克隆方法??寺〉膶崿F(xiàn)方法有兩種:淺拷貝(shallow copy)與深拷貝(deep copy)。
淺拷貝與深拷貝

下面給出淺拷貝與深拷貝的兩個例子,例子使用了ICloneable接口。C#中的數(shù)組是引用型的變量,我們通過數(shù)組來進(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對象實現(xiàn)了一個淺拷貝,因此當(dāng)對sc1進(jìn)行克隆時,其字段v并沒有克隆,這導(dǎo)致sc1與sc2的字段v都指向了同一個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)造一個新的DeepCopy對象,構(gòu)造參數(shù)為
 // 原有對象中使用的 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)于原型模式的討論

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

相關(guān)文章

  • C語言中.與->的區(qū)別詳細(xì)解析

    C語言中.與->的區(qū)別詳細(xì)解析

    這篇文章主要給大家介紹了關(guān)于C語言中.與->區(qū)別的相關(guān)資料,這雖然是個小問題,但有時候很容易讓人迷惑,因為有的時候用混淆了,程序編譯不通過,需要的朋友可以參考下
    2023-06-06
  • C語言關(guān)于自定義數(shù)據(jù)類型之枚舉和聯(lián)合體詳解

    C語言關(guān)于自定義數(shù)據(jù)類型之枚舉和聯(lián)合體詳解

    枚舉顧名思義就是把所有的可能性列舉出來,像一個星期分為七天我們就可以使用枚舉,聯(lián)合體是由關(guān)鍵字union和標(biāo)簽定義的,和枚舉是一樣的定義方式,不一樣的是,一個聯(lián)合體只有一塊內(nèi)存空間,什么意思呢,就相當(dāng)于只開辟最大的變量的內(nèi)存,其他的變量都在那個變量占據(jù)空間
    2021-11-11
  • C++中的引用與高級函數(shù)詳解

    C++中的引用與高級函數(shù)詳解

    這篇文章主要介紹了C++中的引用與高級函數(shù)詳解,概念:引用是為已存在的變量取了一個別名,引用和引用的變量共用同一塊內(nèi)存空間,需要的朋友可以參考下
    2023-07-07
  • C語言之字符串模糊查詢方法的實現(xiàn)

    C語言之字符串模糊查詢方法的實現(xiàn)

    本篇文章主要為大家介紹字符串模糊查詢的C語言程序編寫方法,有需要的朋友可以參考下
    2015-07-07
  • tinyxml 常用的C++ XML解析器非常優(yōu)秀

    tinyxml 常用的C++ XML解析器非常優(yōu)秀

    讀取和設(shè)置xml配置文件是最常用的操作,試用了幾個C++的XML解析器,個人感覺TinyXML是使用起來最舒服的,因為它的API接口和Java的十分類似,面向?qū)ο笮院芎?/div> 2012-11-11
  • C++字符數(shù)組的輸入輸出和字符串結(jié)束標(biāo)志使用講解

    C++字符數(shù)組的輸入輸出和字符串結(jié)束標(biāo)志使用講解

    這篇文章主要介紹了C++字符數(shù)組的輸入輸出和符串結(jié)束標(biāo)志使用講解,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • c++實現(xiàn)高精度加法

    c++實現(xiàn)高精度加法

    高精度運算是指參與運算的數(shù)(加數(shù),減數(shù),因子……)范圍大大超出了標(biāo)準(zhǔn)數(shù)據(jù)類型(整型,實型)能表示的范圍的運算。例如,求兩個200位的數(shù)的和。這時,就要用到高精度算法了。
    2017-05-05
  • 指向類成員函數(shù)的指針其實并非指針

    指向類成員函數(shù)的指針其實并非指針

    對于指向類成員的指針,必須緊記,指向類成員(非static)的指針并非指針
    2013-08-08
  • 學(xué)好C++必須做到的50條 絕對經(jīng)典!

    學(xué)好C++必須做到的50條 絕對經(jīng)典!

    學(xué)好C++必須做到的50條,絕對經(jīng)典!想要學(xué)好C++的朋友一定要認(rèn)真閱讀本文,更要做到以下50條
    2016-09-09
  • C語言詳解實現(xiàn)猜數(shù)字游戲步驟

    C語言詳解實現(xiàn)猜數(shù)字游戲步驟

    猜數(shù)字是興起于英國的益智類小游戲,起源于20世紀(jì)中期,一般由兩個人或多人玩,也可以由一個人和電腦玩。游戲規(guī)則為一方出數(shù)字,一方猜,今天我們來實現(xiàn)這個游戲案例
    2022-07-07

最新評論