乐在其中设计模式(C#) - 原型模式(Prototype Pattern)-阿里云开发者社区

开发者社区> 开发与运维> 正文

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

简介:
[索引页]
[源码下载]


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


作者:webabcd


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


示例
有一个Message实体类,现在要克隆它。
MessageModel
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Prototype 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// Message实体类 
InBlock.gif        /// </summary> 
InBlock.gif        public class MessageModel 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="msg">Message内容</param> 
InBlock.gif                /// <param name="pt">Message发布时间</param> 
InBlock.gif                public MessageModel(string msg, DateTime pt) 
InBlock.gif                { 
InBlock.gif                        this._message = msg; 
InBlock.gif                        this._publishTime = pt; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private string _message; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message内容 
InBlock.gif                /// </summary> 
InBlock.gif                public string Message 
InBlock.gif                { 
InBlock.gif                        get { return _message; } 
InBlock.gif                        set { _message = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private DateTime _publishTime; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message发布时间 
InBlock.gif                /// </summary> 
InBlock.gif                public DateTime PublishTime 
InBlock.gif                { 
InBlock.gif                        get { return _publishTime; } 
InBlock.gif                        set { _publishTime = value; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
ShallowCopy
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Prototype 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 浅拷贝 
InBlock.gif        /// </summary> 
InBlock.gif        public class ShallowCopy : ICloneable 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                public ShallowCopy() 
InBlock.gif                { 
InBlock.gif                         
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 实现ICloneable的Clone()方法 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public Object Clone() 
InBlock.gif                { 
InBlock.gif                        return this.MemberwiseClone(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private MessageModel _mm; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message实体对象 
InBlock.gif                /// </summary> 
InBlock.gif                public MessageModel MessageModel 
InBlock.gif                { 
InBlock.gif                        get { return _mm; } 
InBlock.gif                        set { _mm = value; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
DeepCopy
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifnamespace Pattern.Prototype 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 深拷贝 
InBlock.gif        /// </summary> 
InBlock.gif        public class DeepCopy : ICloneable 
InBlock.gif        { 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                public DeepCopy() 
InBlock.gif                { 
InBlock.gif                         
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 构造函数 
InBlock.gif                /// </summary> 
InBlock.gif                /// <param name="mm">Message实体对象</param> 
InBlock.gif                public DeepCopy(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                        _mm = mm; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                /// <summary> 
InBlock.gif                /// 实现ICloneable的Clone()方法 
InBlock.gif                /// </summary> 
InBlock.gif                /// <returns></returns> 
InBlock.gif                public Object Clone() 
InBlock.gif                { 
InBlock.gif                        return new DeepCopy(new MessageModel(_mm.Message, _mm.PublishTime)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private MessageModel _mm; 
InBlock.gif                /// <summary> 
InBlock.gif                /// Message实体对象 
InBlock.gif                /// </summary> 
InBlock.gif                public MessageModel MessageModel 
InBlock.gif                { 
InBlock.gif                        get { return _mm; } 
InBlock.gif                        set { _mm = value; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
client
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifusing Pattern.Prototype; 
InBlock.gif 
InBlock.gifpublic partial class Prototype : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Response.Write("ShallowCopy演示如下:<br />"); 
InBlock.gif                ShowShallowCopy(); 
InBlock.gif 
InBlock.gif                Response.Write("DeepCopy演示如下:<br />"); 
InBlock.gif                ShowDeepCopy();         
InBlock.gif        } 
InBlock.gif 
InBlock.gif        private void ShowShallowCopy() 
InBlock.gif        { 
InBlock.gif                ShallowCopy sc = new ShallowCopy(); 
InBlock.gif                sc.MessageModel = new MessageModel("ShallowCopy", DateTime.Now); 
InBlock.gif 
InBlock.gif                ShallowCopy sc2 = (ShallowCopy)sc.Clone(); 
InBlock.gif 
InBlock.gif                Response.Write(sc.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(sc2.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif 
InBlock.gif                sc.MessageModel.Message = "ShallowCopyShallowCopy"
InBlock.gif 
InBlock.gif                Response.Write(sc.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(sc2.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        private void ShowDeepCopy() 
InBlock.gif        { 
InBlock.gif                DeepCopy sc = new DeepCopy(); 
InBlock.gif                sc.MessageModel = new MessageModel("DeepCopy", DateTime.Now); 
InBlock.gif 
InBlock.gif                DeepCopy sc2 = (DeepCopy)sc.Clone(); 
InBlock.gif 
InBlock.gif                Response.Write(sc.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(sc2.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif 
InBlock.gif                sc.MessageModel.Message = "DeepCopyDeepCopy"
InBlock.gif 
InBlock.gif                Response.Write(sc.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif                Response.Write(sc2.MessageModel.Message); 
InBlock.gif                Response.Write("<br />"); 
InBlock.gif        } 
InBlock.gif}
 
 
运行结果
ShallowCopy演示如下:
ShallowCopy
ShallowCopy
ShallowCopyShallowCopy
ShallowCopyShallowCopy
DeepCopy演示如下:
DeepCopy
DeepCopy
DeepCopyDeepCopy
DeepCopy


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


OK
[源码下载]




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344497,如需转载请自行联系原作者

版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章