這篇文章主要介紹“.NET原型模式的定義及實例用法”,在日常操作中,相信很多人在.NET原型模式的定義及實例用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”.NET原型模式的定義及實例用法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
原型模式的定義:
用原型實例指定創(chuàng)建對象的種類,并且通過拷貝這些原型創(chuàng)建新的對象。
原型模式結(jié)構(gòu)圖:
創(chuàng)建型模式中一個比較特殊的模式-原型模式,有個較大的特點是克隆一個現(xiàn)有的對象,這個克隆的結(jié)果有2種,一種是淺度復(fù)制,另一種是深度復(fù)制。
創(chuàng)建型模式一般是用來創(chuàng)建一個新的對象,然后我們使用這個對象完成一些對象的操作,我們通過原型模式可以快速的創(chuàng)建一個對象而不需要提供專門的new()操作就可以快速完成對象的創(chuàng)建,這無疑是一種非常有效的方式,快速的創(chuàng)建一個新的對象。
1.原型模式:淺度復(fù)制
定義一個接口, 用來表述所有的顏色對象接口
////// 顏色接口 /// public interface IColor { IColor Clone(); int Red { get; set; } int Green { get; set; } int Blue { get; set; } }
給出紅色的具體實現(xiàn)代碼:
public class RedColor:IColor { public int Red { get; set; } public int Green { get; set; } public int Blue { get; set; } public IColor Clone() { return (IColor)this.MemberwiseClone(); } }
具體的測試代碼如下:
static void Main(string[] args) { IColor color = new RedColor(); color.Red = 255; Console.WriteLine("color -red " + color.Red); //225 IColor color1 = color.Clone(); color1.Red = 224; Console.WriteLine("color1-red " + color1.Red);//224 Console.WriteLine("color -red " + color.Red); //225 }
可以發(fā)現(xiàn):在我們修改color1對象的Red屬性值,沒有對color的屬性參生影響,即對象副本的修改不會影響對象本身的狀態(tài)。
2.原型模式:深度復(fù)制
深復(fù)制考慮的情況相對來說就會比較復(fù)雜,因為有可能對象是之間有繼承關(guān)系或者引用關(guān)系的時候,可能我們深復(fù)制的時候就需要注意.一般來說深復(fù)制一方面可以采用種簡單的深復(fù)制對象的時候的方案,還可以通過序列化的形式來進(jìn)行對象的復(fù)制。下面通過序列化的形式來實現(xiàn)原型模式:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { ////// 顏色接口 /// public interface IColor { IColorDemo Clone(); int Red { get; set; } int Green { get; set; } int Blue { get; set; } Factroy f{get;set;} } ////// 生產(chǎn)顏色的工廠信息 /// [Serializable] public class Factroy { public string name { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { ////// 顏色 /// [Serializable] public class RedColor:IColor { public int Red { get; set; } public int Green { get; set; } public int Blue { get; set; } public Factroy f { get; set; } public IColor Clone() { SerializableHelper s = new SerializableHelper(); string target = s.Serializable(this); return s.Derializable(target); } } }
序列化幫助類:
////// 序列化和反序列化輔助類 /// public class SerializableHelper { public string Serializable(object target) { using (MemoryStream stream = new MemoryStream()) { new BinaryFormatter().Serialize(stream, target); return Convert.ToBase64String(stream.ToArray()); } } public object Derializable(string target) { byte[] targetArray = Convert.FromBase64String(target); using (MemoryStream stream = new MemoryStream(targetArray)) { return new BinaryFormatter().Deserialize(stream); } } public T Derializable(string target) { return (T)Derializable(target); } }
測試:
static void Main(string[] args) { IColor color = new RedColor(); color.Red = 255; color.f = new Factroy() { name="湖北工廠" }; Console.WriteLine("color - Factroy:" + color.f.name); //湖北工廠 IColor color1 = color.Clone(); color1.Red = 234; color1.f.name = "北京工廠"; Console.WriteLine("color1- Factroy:" + color1.f.name); //北京工廠 Console.WriteLine("color - Factroy:" + color.f.name); //湖北工廠 Console.Read(); }
程序的運(yùn)行結(jié)果如下:
結(jié)論:通過序列化和反序列化形成新的對象。其實只要是項目中要使用原型模式進(jìn)行對象復(fù)制的情況下,都可以通過序列化的形式來進(jìn)行深復(fù)制。
到此,關(guān)于“.NET原型模式的定義及實例用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站欄目:.NET原型模式的定義及實例用法-創(chuàng)新互聯(lián)
本文地址:http://weahome.cn/article/digogi.html