[索引页]
[源码下载]

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

作者:webabcd

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

示例
有一个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
[源码下载]

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

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

  1. C++设计模式——原型模式(Prototype Pattern)

    C++设计模式--原型模式(Prototype Pattern) 微信公众号:幼儿园的学霸 目录 文章目录 C++设计模式--原型模式(Prototype Pattern) 目录 定义 代码示例 普通 ...

  2. 原型模式(Prototype Pattern)

    原型模式(Prototype Pattern) 原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. ...

  3. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:web ...

  4. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  5. 设计模式---原型模式(Prototype Pattern)

    在编程中有时候我们会发现,当我们需要一个实例,可是这个实例的创建过程十分复杂,在执行过程中 会消耗大量的时间,同时创建第一个实例和创建第二个时间的初始化信息并未改变.在此种情况下,直接New 一个实例 ...

  6. 极速理解设计模式系列:4.原型模式(Prototype Pattern)

    四个角色:抽象原型角色(Prototype).具体原型角色(ConcretePrototype).原型管理器角色(PrototypeManager).客户端角色(Client) 抽象原型角色(Prot ...

  7. 设计模式之原型模式(Prototype)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式包括:1.FactoryMethod(工厂方法模式):2.Abstract Factory(抽象工厂模式):3.Sin ...

  8. 设计模式之原型模式prototype

    1.原型模式的使用和本质.以及优势: a.通过 new 产生一个对象需要非常繁琐的数据准备或者访问权限,则可以使用原型模式. b.原型模式的使用就是 java 中的克隆技术,以某个对象为原型,复制出新 ...

  9. 【设计模式】—— 原型模式Prototype

    前言:[模式总览]----------by xingoo 模式意图 由于有些时候,需要在运行时指定对象时哪个类的实例,此时用工厂模式就有些力不从心了.通过原型模式就可以通过拷贝函数clone一个原有的 ...

最新文章

  1. 第十九章 9标签类Label
  2. java三大集合_java中三大集合框架
  3. JAVA使用正则表达式给字符串添加分隔符
  4. 环路的产生及RIP防环机制
  5. activemq消息持久化_ActiveMQ 5.x中的消息持久性
  6. ssh放行端口_安全组中已经添加规则放行SSH端口的访问之后如何使用f1 RTL
  7. Android 系统(165)---在apns-conf文件中配置一个read_only字段,使APN不可被编辑
  8. 值得收藏的130个神器网站
  9. setitime和相关函数
  10. mysql5.7如何打开,mysql57怎么打开
  11. 项目管理相关的考试认证及证书价值介绍
  12. 魔兽支持宽屏--怎样让宽屏支持更多游戏?
  13. 吴恩达机器学习和深度学习视频和笔记
  14. Tuxera2022Mac系统读写NTFS磁盘工具装机必备
  15. Verilog实现数字时钟
  16. 计算机硬盘有坏道,电脑硬盘有坏道怎么办
  17. bluefish中文乱码问题
  18. FusionStorage原理及组件,Java面试回忆录
  19. 漫画人脸检测 | 全局和局部信息融合的深度神经网络(文末源码)
  20. 轻量级自动化测试框架 UFT 初学者 学习编写

热门文章

  1. Java 代码性能优化
  2. 大数据学习(4)--分布式数据库HBase
  3. 《Python Cookbook 3rd》笔记(4.6):带有外部状态的生成器函数
  4. frame中src怎么设置成一个变量_Go 语言设计哲学之七:变量声明须一致
  5. python赋值01_python学习笔记1-赋值与字符串 | 学步园
  6. 互联网商业模式:增值还是减值?
  7. 推辞掉得不是你的工作,而是你的未来
  8. 改变您一生的90/10原理
  9. Astyle 一键格式化项目代码
  10. coco creator编辑动画坑之拖图片