原型模式(Prototype Pattern)

原型模式(Prototype Pattern)

1.jpg.png
介绍
用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。


示例
有一个Message实体类,现在要克隆它。


MessageModel

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Prototype
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Message实体类
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class MessageModel
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="msg">Message内容</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="pt">Message发布时间</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public MessageModel(string msg, DateTime pt)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
this._message = msg;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
this._publishTime = pt;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private string _message;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message内容
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public string Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _message; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _message = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private DateTime _publishTime;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message发布时间
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public DateTime PublishTime
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _publishTime; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _publishTime = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


ShallowCopy

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Prototype
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// 浅拷贝
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class ShallowCopy : ICloneable
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public ShallowCopy()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 实现ICloneableClone()方法
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public Object Clone()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return this.MemberwiseClone();
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private MessageModel _mm;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message实体对象
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public MessageModel MessageModel
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _mm; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _mm = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


DeepCopy

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Text;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Prototype
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// 深拷贝
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class DeepCopy : ICloneable
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public DeepCopy()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public DeepCopy(MessageModel mm)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _mm = mm;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 实现ICloneableClone()方法
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public Object Clone()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return new DeepCopy(new MessageModel(_mm.Message, _mm.PublishTime));
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private MessageModel _mm;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message实体对象
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public MessageModel MessageModel
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _mm; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _mm = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif



client

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Data;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.Security;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.WebControls;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.WebControls.WebParts;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.HtmlControls;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using Pattern.Prototype;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
public partial class Prototype : System.Web.UI.Page
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("ShallowCopy
演示如下:<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        ShowShallowCopy();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("DeepCopy
演示如下:<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        ShowDeepCopy();    
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
private void ShowShallowCopy()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        ShallowCopy sc = 
new ShallowCopy();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        sc.MessageModel = 
new MessageModel("ShallowCopy", DateTime.Now);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        ShallowCopy sc2 = (ShallowCopy)sc.Clone();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc2.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        sc.MessageModel.Message = "ShallowCopyShallowCopy";
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc2.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
private void ShowDeepCopy()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        DeepCopy sc = 
new DeepCopy();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        sc.MessageModel = 
new MessageModel("DeepCopy", DateTime.Now);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        DeepCopy sc2 = (DeepCopy)sc.Clone();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc2.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        sc.MessageModel.Message = "DeepCopyDeepCopy";
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(sc2.MessageModel.Message);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


运行结果
ShallowCopy演示如下:
ShallowCopy
ShallowCopy
ShallowCopyShallowCopy
ShallowCopyShallowCopy
DeepCopy演示如下:
DeepCopy
DeepCopy
DeepCopyDeepCopy
DeepCopy


果糖网