导航

(二)透过.Net框架找设计模式系列:原型模式

Posted on 2011-07-01 14:34  chicwoo  阅读(222)  评论(0)    收藏  举报

1 DataSet 提供了 Clone()和 Copy()方法,

Clone()方法呢只是复制 DataSet 的结构,而并不会复制数据,这就是原型模式的浅复制的一个应用了。

Copy()方法的话,是既复制 DataSet 中的结构,而且还会把全部的数据复制过去,很明显,这是原型模式的深复制的应用。

 ICloneable 

该接口 . Net 中的 System 命名空间下,其扮演的角色就是类图中的抽象类Prototype,其继承类中,可以通过MemberwiseClone实现浅复制,深复制则采用系列化的方式。

附录

通过系列化方式实现深度拷贝

要实现深拷贝,可以通过序列化的方式。抽象类及具体类都必须标注为可序列化的[Serializable],上面的例子加上深拷贝之后的完整程序如下:

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
 
[Serializable]
abstract class ColorPrototype
{
    public abstract ColorPrototype Clone(bool Deep);
}
 
[Serializable]
class ConcteteColorPrototype : ColorPrototype
{
 
    private int _red, _green, _blue;
 
 
    public ConcteteColorPrototype(int red, int green, int blue)
    {
        this._red = red;
        this._green = green;
        this._blue = blue;
    }
     public override ColorPrototype Clone(bool Deep)
    {
        if(Deep)
            return CreateDeepCopy();
        else
            return (ColorPrototype) this.MemberwiseClone();
    }
 
    //实现深拷贝
    public ColorPrototype CreateDeepCopy()
    {
        ColorPrototype colorPrototype;
 
        MemoryStream memoryStream = new MemoryStream();
        BinaryFormatter formatter = new BinaryFormatter();
 
        formatter.Serialize(memoryStream, this);
        memoryStream.Position = 0;
 
        colorPrototype = (ColorPrototype)formatter.Deserialize(memoryStream);
        return colorPrototype;
    }
 
    public ConcteteColorPrototype Create(int red,int green,int blue)
    {
        return new ConcteteColorPrototype(red,green,blue); 
    }
 
    public void Display(string _colorname)
    {
        Console.WriteLine("{0}'s RGB Values are: {1},{2},{3}",
            _colorname,_red, _green, _blue );
    }
}
 
class ColorManager

    Hashtable colors = new Hashtable();
     public ColorPrototype this[string name]
    {
        get
        {
            return (ColorPrototype)colors[name];
        }
        set
        {
            colors.Add(name,value);
        }
    }
}
 
 
class App
{
    public static void Main(string[] args)
    {
        ColorManager colormanager = new ColorManager();
 
        //初始化颜色
        colormanager["red"] = new ConcteteColorPrototype(255, 0, 0);
        colormanager["green"] = new ConcteteColorPrototype(0, 255, 0);
        colormanager["blue"] = new ConcteteColorPrototype(0, 0, 255);
        colormanager["angry"] = new ConcteteColorPrototype(255, 54, 0);
        colormanager["peace"] = new ConcteteColorPrototype(128, 211, 128);
        colormanager["flame"] = new ConcteteColorPrototype(211, 34, 20);
 
        //使用颜色
        string colorName = "red";
        ConcteteColorPrototype c1 = (ConcteteColorPrototype)colormanager[colorName].Clone(false);
        c1.Display(colorName);
 
        colorName = "peace";
        ConcteteColorPrototype c2 = (ConcteteColorPrototype)colormanager[colorName].Clone(true);
        c2.Display(colorName);
 
        colorName = "flame";
        ConcteteColorPrototype c3 = (ConcteteColorPrototype)colormanager[colorName].Clone(true);
        c3.Display(colorName);
 
        Console.ReadLine();
    }
}

 

原型模型类图

原型模式(Prototype)是创建模型之一,典型特点就是通过克隆方式建立新对象,新建一个对象通常通过new方式获取,但是有时候这个构造过程却是非常复杂的,消耗大量资源。