原型模式(Prototype Pattern)
介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。示例有一个Message实体类,现在要克隆它。MessageModel
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Prototype{ ////// Message实体类 /// public class MessageModel { ////// 构造函数 /// /// Message内容 /// Message发布时间 public MessageModel(string msg, DateTime pt) { this._message = msg; this._publishTime = pt; } private string _message; ////// Message内容 /// public string Message { get { return _message; } set { _message = value; } } private DateTime _publishTime; ////// Message发布时间 /// public DateTime PublishTime { get { return _publishTime; } set { _publishTime = value; } } }}
ShallowCopy
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Prototype{ ////// 浅拷贝 /// public class ShallowCopy : ICloneable { ////// 构造函数 /// public ShallowCopy() { } ////// 实现ICloneable的Clone()方法 /// ///public Object Clone() { return this.MemberwiseClone(); } private MessageModel _mm; /// /// Message实体对象 /// public MessageModel MessageModel { get { return _mm; } set { _mm = value; } } }}
DeepCopy
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Prototype{ ////// 深拷贝 /// public class DeepCopy : ICloneable { ////// 构造函数 /// public DeepCopy() { } ////// 构造函数 /// /// Message实体对象 public DeepCopy(MessageModel mm) { _mm = mm; } ////// 实现ICloneable的Clone()方法 /// ///public Object Clone() { return new DeepCopy(new MessageModel(_mm.Message, _mm.PublishTime)); } private MessageModel _mm; /// /// Message实体对象 /// 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演示如下:"); ShowShallowCopy(); Response.Write("DeepCopy演示如下:"); 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(""); Response.Write(sc2.MessageModel.Message); Response.Write(""); sc.MessageModel.Message = "ShallowCopyShallowCopy"; Response.Write(sc.MessageModel.Message); Response.Write(""); Response.Write(sc2.MessageModel.Message); Response.Write(""); } 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(""); Response.Write(sc2.MessageModel.Message); Response.Write(""); sc.MessageModel.Message = "DeepCopyDeepCopy"; Response.Write(sc.MessageModel.Message); Response.Write(""); Response.Write(sc2.MessageModel.Message); Response.Write(""); }}
运行结果
ShallowCopy演示如下: ShallowCopy ShallowCopy ShallowCopyShallowCopy ShallowCopyShallowCopy DeepCopy演示如下: DeepCopy DeepCopy DeepCopyDeepCopy DeepCopy