實(shí)例講解C++編程中對(duì)設(shè)計(jì)模式中的原型模式的使用
原型模式的實(shí)現(xiàn)完整代碼示例(code):原型模式的實(shí)現(xià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)都很簡單,其關(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)簡單起見,也因?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://m.jygsgssxh.com/a1/Cyuyan/2464.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解
- 01-10貪心算法 WOODEN STICKS 實(shí)例代碼


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery


