程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 設計模式(C#) - 原型模式(Prototype Pattern)

設計模式(C#) - 原型模式(Prototype Pattern)

編輯:關於C#

返回“設計模式(C#)系列文章索引”

介紹

用原型實例指定創建對象的種類,並且通過拷貝這個原型來創建新的對象。

示例

有一個Message實體類,現在要克隆它。

MessageModel

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Prototype
{
  /**//// <summary>
  /// Message實體類
  /// </summary>
  public class MessageModel
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="msg">Message內容</param>
    /// <param name="pt">Message發布時間</param>
    public MessageModel(string msg, DateTime pt)
    {
      this._message = msg;
      this._publishTime = pt;
    }

    private string _message;
    /**//// <summary>
    /// Message內容
    /// </summary>
    public string Message
    {
      get { return _message; }
      set { _message = value; }
    }

    private DateTime _publishTime;
    /**//// <summary>
    /// Message發布時間
    /// </summary>
    public DateTime PublishTime
    {
      get { return _publishTime; }
      set { _publishTime = value; }
    }
  }
}

ShallowCopy

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Prototype
{
  /**//// <summary>
  /// 淺拷貝
  /// </summary>
  public class ShallowCopy : ICloneable
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    public ShallowCopy()
    {

    }

    /**//// <summary>
    /// 實現ICloneable的Clone()方法
    /// </summary>
    /// <returns></returns>
    public Object Clone()
    {
      return this.MemberwiseClone();
    }

    private MessageModel _mm;
    /**//// <summary>
    /// Message實體對象
    /// </summary>
    public MessageModel MessageModel
    {
      get { return _mm; }
      set { _mm = value; }
    }
  }
}

DeepCopy

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.Prototype
{
  /**//// <summary>
  /// 深拷貝
  /// </summary>
  public class DeepCopy : ICloneable
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    public DeepCopy()
    {

    }

    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    public DeepCopy(MessageModel mm)
    {
      _mm = mm;
    }

    /**//// <summary>
    /// 實現ICloneable的Clone()方法
    /// </summary>
    /// <returns></returns>
    public Object Clone()
    {
      return new DeepCopy(new MessageModel(_mm.Message, _mm.PublishTime));
    }

    private MessageModel _mm;
    /**//// <summary>
    /// Message實體對象
    /// </summary>
    public MessageModel MessageModel
    {
      get { return _mm; }
      set { _mm = value; }
    }
  }
}

client

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Pattern.Prototype;

public partial class Prototype : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    Response.Write("ShallowCopy演示如下:<br />");
    ShowShallowCopy();

    Response.Write("DeepCopy演示如下:<br />");
    ShowDeepCopy();  
  }

  private void ShowShallowCopy()
  {
    ShallowCopy sc = new ShallowCopy();
    sc.MessageModel = new MessageModel("ShallowCopy", DateTime.Now);

    ShallowCopy sc2 = (ShallowCopy)sc.Clone();

    Response.Write(sc.MessageModel.Message);
    Response.Write("<br />");
    Response.Write(sc2.MessageModel.Message);
    Response.Write("<br />");

    sc.MessageModel.Message = "ShallowCopyShallowCopy";

    Response.Write(sc.MessageModel.Message);
    Response.Write("<br />");
    Response.Write(sc2.MessageModel.Message);
    Response.Write("<br />");
  }

  private void ShowDeepCopy()
  {
    DeepCopy sc = new DeepCopy();
    sc.MessageModel = new MessageModel("DeepCopy", DateTime.Now);

    DeepCopy sc2 = (DeepCopy)sc.Clone();

    Response.Write(sc.MessageModel.Message);
    Response.Write("<br />");
    Response.Write(sc2.MessageModel.Message);
    Response.Write("<br />");

    sc.MessageModel.Message = "DeepCopyDeepCopy";

    Response.Write(sc.MessageModel.Message);
    Response.Write("<br />");
    Response.Write(sc2.MessageModel.Message);
    Response.Write("<br />");
  }
}

運行結果

ShallowCopy演示如下:

ShallowCopy
ShallowCopy
ShallowCopyShallowCopy
ShallowCopyShallowCopy

DeepCopy演示如下:

DeepCopy
DeepCopy
DeepCopyDeepCopy
DeepCopy

參考

http://www.dofactory.com/Patterns/PatternPrototype.aspx

OK

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved