.NET原型模式講解
原型模式的定義:
用原型實例指定創(chuàng)建對象的種類,并且通過拷貝這些原型創(chuàng)建新的對象。
原型模式結(jié)構(gòu)圖:
創(chuàng)建型模式中一個比較特殊的模式-原型模式,有個最大的特點是克隆一個現(xiàn)有的對象,這個克隆的結(jié)果有2種,一種是淺度復制,另一種是深度復制。
創(chuàng)建型模式一般是用來創(chuàng)建一個新的對象,然后我們使用這個對象完成一些對象的操作,我們通過原型模式可以快速的創(chuàng)建一個對象而不需要提供專門的new()操作就可以快速完成對象的創(chuàng)建,這無疑是一種非常有效的方式,快速的創(chuàng)建一個新的對象。
1.原型模式:淺度復制
定義一個接口, 用來表述所有的顏色對象接口
/// <summary> /// 顏色接口 /// </summary> 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.原型模式:深度復制
深復制考慮的情況相對來說就會比較復雜,因為有可能對象是之間有繼承關(guān)系或者引用關(guān)系的時候,可能我們深復制的時候就需要注意.一般來說深復制一方面可以采用種簡單的深復制對象的時候的方案,還可以通過序列化的形式來進行對象的復制。下面通過序列化的形式來實現(xiàn)原型模式:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { /// <summary> /// 顏色接口 /// </summary> public interface IColor { IColorDemo Clone(); int Red { get; set; } int Green { get; set; } int Blue { get; set; } Factroy f{get;set;} } /// <summary> /// 生產(chǎn)顏色的工廠信息 /// </summary> [Serializable] public class Factroy { public string name { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication4 { /// <summary> /// 顏色 /// </summary> [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<IColor>(target); } } }
序列化幫助類:
/// <summary> /// 序列化和反序列化輔助類 /// </summary> 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<T>(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(); }
程序的運行結(jié)果如下:
結(jié)論:通過序列化和反序列化形成新的對象。其實只要是項目中要使用原型模式進行對象復制的情況下,都可以通過序列化的形式來進行深復制。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- JS面向?qū)ο蠡A(chǔ)講解(工廠模式、構(gòu)造函數(shù)模式、原型模式、混合模式、動態(tài)原型模式)
- js面向?qū)ο笾R妱?chuàng)建對象的幾種方式(工廠模式、構(gòu)造函數(shù)模式、原型模式)
- JavaScript面向?qū)ο蟪绦蛟O(shè)計三 原型模式(上)
- javascript 原型模式實現(xiàn)OOP的再研究
- C++設(shè)計模式之原型模式
- JavaScript設(shè)計模式之原型模式(Object.create與prototype)介紹
- 構(gòu)造函數(shù)+原型模式構(gòu)造js自定義對象(最通用)
- php設(shè)計模式 Prototype (原型模式)代碼
- 淺析php原型模式
- javascript原型模式用法實例詳解
相關(guān)文章
數(shù)據(jù)庫SqlParameter 的插入操作,防止sql注入的實現(xiàn)代碼
今天學習了一下SqlParameter的用法,原來這么寫是為了防止sql注入,破壞數(shù)據(jù)庫的。并自己動手連接了數(shù)據(jù)庫。2013-04-04asp.net core 集成swagger ui的原理解析
本文主要講解了如何對API進行分組,這里僅僅是舉了一個按照API功能進行分組的例子,其實在實際開發(fā)中,要按照何種方式分組,可以按照需求靈活定義,比如按照API版本進行分組2021-10-10將DataTable中的一行復制到另一個DataTable的方法
將DataTable中的一行復制到另一個DataTable的方法...2007-09-09服務(wù)器讀取EXCEL不安裝OFFICE如何實現(xiàn)
用asp.net做了一簡單的游戲管理后臺,涉及到了上傳Excel導入數(shù)據(jù)的功能,在本地開發(fā)實現(xiàn)都好好的,可已上傳的服務(wù)器上就悲劇了,下面有個不錯的解決方法,大家可以參考下2014-03-03ASP.NET?MVC遍歷驗證ModelState的錯誤信息
這篇文章介紹了ASP.NET?MVC遍歷ModelState錯誤信息的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09.net下實現(xiàn)Word動態(tài)填加數(shù)據(jù)打印
.net下實現(xiàn)Word動態(tài)填加數(shù)據(jù)打印...2007-04-04