首页 > 其他 > 详细

乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

时间:2014-03-09 21:47:22      阅读:649      评论:0      收藏:0      [点我收藏+]
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

[索引页]
[源码下载]


乐在其中设计模式(C#) - 原型模式(Prototype Pattern)


作者:webabcd


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


示例
有一个Message实体类,现在要克隆它。
bubuko.com,布布扣


MessageModel
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Prototype
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Message实体类
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class MessageModel
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="msg">Message内容</param>
bubuko.com,布布扣        
/// <param name="pt">Message发布时间</param>

bubuko.com,布布扣        public MessageModel(string msg, DateTime pt)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
this._message = msg;
bubuko.com,布布扣            
this._publishTime = pt;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private string _message;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message内容
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public string Message
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _message; }
bubuko.com,布布扣            
set { _message = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private DateTime _publishTime;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message发布时间
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public DateTime PublishTime
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _publishTime; }
bubuko.com,布布扣            
set { _publishTime = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

ShallowCopy
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Prototype
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 浅拷贝
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class ShallowCopy : ICloneable
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public ShallowCopy()
bubuko.com,布布扣        
{
bubuko.com,布布扣            
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 实现ICloneable的Clone()方法
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public Object Clone()
bubuko.com,布布扣        
{
bubuko.com,布布扣            
return this.MemberwiseClone();
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private MessageModel _mm;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message实体对象
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public MessageModel MessageModel
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _mm; }
bubuko.com,布布扣            
set { _mm = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

DeepCopy
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Prototype
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 深拷贝
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class DeepCopy : ICloneable
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public DeepCopy()
bubuko.com,布布扣        
{
bubuko.com,布布扣            
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="mm">Message实体对象</param>

bubuko.com,布布扣        public DeepCopy(MessageModel mm)
bubuko.com,布布扣        
{
bubuko.com,布布扣            _mm 
= mm;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 实现ICloneable的Clone()方法
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public Object Clone()
bubuko.com,布布扣        
{
bubuko.com,布布扣            
return new DeepCopy(new MessageModel(_mm.Message, _mm.PublishTime));
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private MessageModel _mm;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message实体对象
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public MessageModel MessageModel
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _mm; }
bubuko.com,布布扣            
set { _mm = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣


client
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Data;
bubuko.com,布布扣
using System.Configuration;
bubuko.com,布布扣
using System.Collections;
bubuko.com,布布扣
using System.Web;
bubuko.com,布布扣
using System.Web.Security;
bubuko.com,布布扣
using System.Web.UI;
bubuko.com,布布扣
using System.Web.UI.WebControls;
bubuko.com,布布扣
using System.Web.UI.WebControls.WebParts;
bubuko.com,布布扣
using System.Web.UI.HtmlControls;
bubuko.com,布布扣
bubuko.com,布布扣
using Pattern.Prototype;
bubuko.com,布布扣
bubuko.com,布布扣
public partial class Prototype : System.Web.UI.Page
bubuko.com,布布扣
{
bubuko.com,布布扣    
protected void Page_Load(object sender, EventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        Response.Write(
"ShallowCopy演示如下:<br />");
bubuko.com,布布扣        ShowShallowCopy();
bubuko.com,布布扣
bubuko.com,布布扣        Response.Write(
"DeepCopy演示如下:<br />");
bubuko.com,布布扣        ShowDeepCopy();    
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
private void ShowShallowCopy()
bubuko.com,布布扣    
{
bubuko.com,布布扣        ShallowCopy sc 
= new ShallowCopy();
bubuko.com,布布扣        sc.MessageModel 
= new MessageModel("ShallowCopy", DateTime.Now);
bubuko.com,布布扣
bubuko.com,布布扣        ShallowCopy sc2 
= (ShallowCopy)sc.Clone();
bubuko.com,布布扣
bubuko.com,布布扣        Response.Write(sc.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣        Response.Write(sc2.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣
bubuko.com,布布扣        sc.MessageModel.Message 
= "ShallowCopyShallowCopy";
bubuko.com,布布扣
bubuko.com,布布扣        Response.Write(sc.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣        Response.Write(sc2.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣    
private void ShowDeepCopy()
bubuko.com,布布扣    
{
bubuko.com,布布扣        DeepCopy sc 
= new DeepCopy();
bubuko.com,布布扣        sc.MessageModel 
= new MessageModel("DeepCopy", DateTime.Now);
bubuko.com,布布扣
bubuko.com,布布扣        DeepCopy sc2 
= (DeepCopy)sc.Clone();
bubuko.com,布布扣
bubuko.com,布布扣        Response.Write(sc.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣        Response.Write(sc2.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣
bubuko.com,布布扣        sc.MessageModel.Message 
= "DeepCopyDeepCopy";
bubuko.com,布布扣
bubuko.com,布布扣        Response.Write(sc.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣        Response.Write(sc2.MessageModel.Message);
bubuko.com,布布扣        Response.Write(
"<br />");
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

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


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


OK
[源码下载]

乐在其中设计模式(C#) - 原型模式(Prototype Pattern),布布扣,bubuko.com

乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

原文:http://www.cnblogs.com/lonelyxmas/p/3590329.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!