一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - .NET原型模式講解

.NET原型模式講解

2020-04-05 14:31Yangyi.He ASP.NET教程

這篇文章主要為大家詳細(xì)介紹了ASP.NET原型模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

原型模式的定義:

用原型實(shí)例指定創(chuàng)建對象的種類,并且通過拷貝這些原型創(chuàng)建新的對象。

原型模式結(jié)構(gòu)圖:

.NET原型模式講解

創(chuàng)建型模式中一個(gè)比較特殊的模式-原型模式,有個(gè)最大的特點(diǎn)是克隆一個(gè)現(xiàn)有的對象,這個(gè)克隆的結(jié)果有2種,一種是淺度復(fù)制,另一種是深度復(fù)制。

創(chuàng)建型模式一般是用來創(chuàng)建一個(gè)新的對象,然后我們使用這個(gè)對象完成一些對象的操作,我們通過原型模式可以快速的創(chuàng)建一個(gè)對象而不需要提供專門的new()操作就可以快速完成對象的創(chuàng)建,這無疑是一種非常有效的方式,快速的創(chuàng)建一個(gè)新的對象。

1.原型模式:淺度復(fù)制

定義一個(gè)接口, 用來表述所有的顏色對象接口

?
1
2
3
4
5
6
7
8
9
10
/// <summary>
/// 顏色接口
/// </summary>
public interface IColor
{
  IColor Clone();
  int Red { get; set; }
  int Green { get; set; }
  int Blue { get; set; }
}

給出紅色的具體實(shí)現(xiàn)代碼:

?
1
2
3
4
5
6
7
8
9
10
11
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();
  }
}

具體的測試代碼如下:

?
1
2
3
4
5
6
7
8
9
10
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
}

.NET原型模式講解

可以發(fā)現(xiàn):在我們修改color1對象的Red屬性值,沒有對color的屬性參生影響,即對象副本的修改不會影響對象本身的狀態(tài)。

2.原型模式:深度復(fù)制

深復(fù)制考慮的情況相對來說就會比較復(fù)雜,因?yàn)橛锌赡軐ο笫侵g有繼承關(guān)系或者引用關(guān)系的時(shí)候,可能我們深復(fù)制的時(shí)候就需要注意.一般來說深復(fù)制一方面可以采用種簡單的深復(fù)制對象的時(shí)候的方案,還可以通過序列化的形式來進(jìn)行對象的復(fù)制。下面通過序列化的形式來實(shí)現(xiàn)原型模式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
    }
  }
}

序列化幫助類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <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);
    }
  }

測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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é)果如下:

.NET原型模式講解

結(jié)論:通過序列化和反序列化形成新的對象。其實(shí)只要是項(xiàng)目中要使用原型模式進(jìn)行對象復(fù)制的情況下,都可以通過序列化的形式來進(jìn)行深復(fù)制。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 秋霞在线一级 | 日本xxxx在线视频免费 | 福利视频免费 | 天天曰天天干 | 亚洲免费视频一区二区三区 | 99视频都是精品热在线播放 | 免费一级片在线观看 | 九九精品成人免费国产片 | 亚洲精品一区二区三区在线看 | 亚洲国产成人久久综合一区 | 欧美激情 亚洲 | 亚洲国产成人在人网站天堂 | 欧美日韩视频在线第一区二区三区 | 日韩专区在线观看 | 国产成人福利美女观看视频 | 污小说免费| 国产一级毛片外aaaa | 亚洲品质自拍网站 | 深夜视频在线播放 | 婷婷麻豆| 亚洲一区二区三区福利在线 | 羞羞视频免费观 | 美女福利视频午夜在线 | 精品国产免费第一区二区三区日韩 | 日韩乱淫 | 99自拍网| 四虎永久网址影院 | 亚洲va久久久噜噜噜久久狠狠 | 福利视频久久 | 日韩亚洲人成在线综合 | 欧美性色黄大片四虎影视 | 99热这里有免费国产精品 | 日本大乳护士的引诱图片 | 99热久热这里只精品 | 色先锋影音资源 | 给我免费观看的视频在线播放 | 特大黑人娇小亚洲女mp4 | 日韩久久综合 | 午夜伦午夜伦锂电影 | 国产大秀视频一区二区三区 | 性欧美金发洋妞xxxxbbbb |