欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

原型模式的實(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;
}

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

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

我們?cè)賮砜匆粋€(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#里面,我們可以很容易的通過Clone()方法實(shí)現(xiàn)原型模式。任何類,只要想支持克隆,必須實(shí)現(xiàn)C#中的ICloneable接口。ICloneable接口中有一Clone方法,可以在類中復(fù)寫實(shí)現(xiàn)自定義的克隆方法??寺〉膶?shí)現(xiàn)方法有兩種:淺拷貝(shallow copy)與深拷貝(deep copy)。
淺拷貝與深拷貝

下面給出淺拷貝與深拷貝的兩個(gè)例子,例子使用了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對(duì)象實(shí)現(xiàn)了一個(gè)淺拷貝,因此當(dāng)對(duì)sc1進(jìn)行克隆時(shí),其字段v并沒有克隆,這導(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)于原型模式的討論

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

上一篇:解析設(shè)計(jì)模式中的Prototype原型模式及在C++中的使用

欄    目:C語言

下一篇:沒有了

本文標(biāo)題:實(shí)例講解C++編程中對(duì)設(shè)計(jì)模式中的原型模式的使用

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2464.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有