.NET原型模式講解
原型模式的定義:
用原型實(shí)例指定創(chuàng)建對(duì)象的種類(lèi),并且通過(guò)拷貝這些原型創(chuàng)建新的對(duì)象。
原型模式結(jié)構(gòu)圖:

創(chuàng)建型模式中一個(gè)比較特殊的模式-原型模式,有個(gè)最大的特點(diǎn)是克隆一個(gè)現(xiàn)有的對(duì)象,這個(gè)克隆的結(jié)果有2種,一種是淺度復(fù)制,另一種是深度復(fù)制。
創(chuàng)建型模式一般是用來(lái)創(chuàng)建一個(gè)新的對(duì)象,然后我們使用這個(gè)對(duì)象完成一些對(duì)象的操作,我們通過(guò)原型模式可以快速的創(chuàng)建一個(gè)對(duì)象而不需要提供專(zhuān)門(mén)的new()操作就可以快速完成對(duì)象的創(chuàng)建,這無(wú)疑是一種非常有效的方式,快速的創(chuàng)建一個(gè)新的對(duì)象。
1.原型模式:淺度復(fù)制
定義一個(gè)接口, 用來(lái)表述所有的顏色對(duì)象接口
/// <summary>
/// 顏色接口
/// </summary>
public interface IColor
{
IColor Clone();
int Red { get; set; }
int Green { get; set; }
int Blue { get; set; }
}
給出紅色的具體實(shí)現(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();
}
}
具體的測(cè)試代碼如下:
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對(duì)象的Red屬性值,沒(méi)有對(duì)color的屬性參生影響,即對(duì)象副本的修改不會(huì)影響對(duì)象本身的狀態(tài)。
2.原型模式:深度復(fù)制
深復(fù)制考慮的情況相對(duì)來(lái)說(shuō)就會(huì)比較復(fù)雜,因?yàn)橛锌赡軐?duì)象是之間有繼承關(guān)系或者引用關(guān)系的時(shí)候,可能我們深復(fù)制的時(shí)候就需要注意.一般來(lái)說(shuō)深復(fù)制一方面可以采用種簡(jiǎn)單的深復(fù)制對(duì)象的時(shí)候的方案,還可以通過(guò)序列化的形式來(lái)進(jìn)行對(duì)象的復(fù)制。下面通過(guò)序列化的形式來(lái)實(shí)現(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);
}
}
}
序列化幫助類(lèi):
/// <summary>
/// 序列化和反序列化輔助類(lèi)
/// </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);
}
}
測(cè)試:
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é)論:通過(guò)序列化和反序列化形成新的對(duì)象。其實(shí)只要是項(xiàng)目中要使用原型模式進(jìn)行對(duì)象復(fù)制的情況下,都可以通過(guò)序列化的形式來(lái)進(jìn)行深復(fù)制。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS面向?qū)ο蠡A(chǔ)講解(工廠模式、構(gòu)造函數(shù)模式、原型模式、混合模式、動(dòng)態(tài)原型模式)
- js面向?qū)ο笾R?jiàn)創(chuàng)建對(duì)象的幾種方式(工廠模式、構(gòu)造函數(shù)模式、原型模式)
- JavaScript面向?qū)ο蟪绦蛟O(shè)計(jì)三 原型模式(上)
- javascript 原型模式實(shí)現(xiàn)OOP的再研究
- C++設(shè)計(jì)模式之原型模式
- JavaScript設(shè)計(jì)模式之原型模式(Object.create與prototype)介紹
- 構(gòu)造函數(shù)+原型模式構(gòu)造js自定義對(duì)象(最通用)
- php設(shè)計(jì)模式 Prototype (原型模式)代碼
- 淺析php原型模式
- javascript原型模式用法實(shí)例詳解
相關(guān)文章
C# FTP,GetResponse(),遠(yuǎn)程服務(wù)器返回錯(cuò)誤
C# FTP,GetResponse(),遠(yuǎn)程服務(wù)器返回錯(cuò)誤:(550) 文件不可用(例如,未找到文件,無(wú)法訪問(wèn)文件)2009-06-06
ASP.NET上傳圖片并生成可帶版權(quán)信息的縮略圖
ASP.NET上傳圖片并生成可帶版權(quán)信息的縮略圖...2006-09-09
Visual Studio for Mac版 初體驗(yàn)
這篇文章主要介紹了Visual Studio for Mac版 初體驗(yàn),本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-05-05
不可忽視的 .NET 應(yīng)用5大性能問(wèn)題
ASP.NET 或是 Windows Forms 容器中,使用 ADO 庫(kù)與運(yùn)行在 CLR 交互,而 CLR 運(yùn)行在操作系統(tǒng)中而該硬件又與其他包含不同技術(shù)堆棧的硬件通過(guò)網(wǎng)絡(luò)相連。在你的應(yīng)用與外部環(huán)境之間,。我們還有 API 管理服務(wù)以及多級(jí)緩存基礎(chǔ)構(gòu)造數(shù)量龐雜,都可能影響應(yīng)用程序的性能!2016-05-05
C#中的Equals、RefrenceEquals和==的區(qū)別與聯(lián)系
C#中判斷兩個(gè)對(duì)象是否相等有Equals、RefrenceEquals和==三種,其中==為運(yùn)算符,其它兩個(gè)為方法,而Equals又有兩種版本,一個(gè)是靜態(tài)的,一個(gè)是虛擬的,詳細(xì)了解可以參考本文2012-12-12
發(fā)布一個(gè)基于TokyoTyrant的C#客戶(hù)端開(kāi)源項(xiàng)目
目前在網(wǎng)上關(guān)于TokyoCabinet(以下簡(jiǎn)稱(chēng)TC)和TokyoTyrant(以下簡(jiǎn)稱(chēng)TT)的資料已相對(duì)豐富了,但在.NET平臺(tái)上的客戶(hù)端軟件卻相對(duì)匱乏,因?yàn)樽鯠iscuz!NT企業(yè)版的關(guān)系,兩個(gè)月前開(kāi)始接觸TC和TT,開(kāi)始寫(xiě)相關(guān)的客戶(hù)端代碼。2010-07-07
淺談ASP.NETCore統(tǒng)一處理404錯(cuò)誤都有哪些方式
本文主要介紹了ASP.NETCore統(tǒng)一處理404錯(cuò)誤都有哪些方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
SqlCommandBuilder類(lèi)批量更新excel或者CSV數(shù)據(jù)的方法
這篇文章主要介紹了SqlCommandBuilder類(lèi)批量更新excel或者CSV數(shù)據(jù)的方法,需要的朋友可以參考下2015-10-10
利用Typings為Visual Studio Code實(shí)現(xiàn)智能提示功能
最近在學(xué)習(xí)Node.js及ThinkJS這個(gè)框架,用vscode作為開(kāi)發(fā)環(huán)境。默認(rèn)情況下vscode對(duì)ThinkJS的代碼提示并不好,所以研究了一下,原來(lái)可以同通過(guò)Typings來(lái)讓vscode擁有強(qiáng)大的智能代碼提示功能。下面本文就介紹了如何利用Typings為Visual Studio Code實(shí)現(xiàn)智能提示功能。2017-02-02

