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

[索引页][源码下载]

乐在其中设计模式(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

[源码下载]

乐在其中设计模式(C#) - 原型模式(Prototype Pattern)的更多相关文章

  1. 二十四种设计模式:原型模式(Prototype Pattern)

    原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...

  2. python 设计模式之原型模式 Prototype Pattern

    #引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...

  3. 【UE4 设计模式】原型模式 Prototype Pattern

    概述 描述 使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.如孙悟空猴毛分身.鸣人影之分身.剑光分化.无限剑制 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, ...

  4. Net设计模式实例之原型模式( Prototype Pattern)

    一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...

  5. 设计模式系列之原型模式(Prototype Pattern)——对象的克隆

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  6. 【设计模式】原型模式 Pototype Pattern

    前面讲了创建一个对象实例的方法单例模式Singleton Pattern, 创造多个产品的工厂模式(简单工厂模式 Simple Factory Pattern, 工厂方法模式 FactoryMothe ...

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

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

  8. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  9. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

随机推荐

  1. [Cocos2d-x]节点之间的相互通讯

    在做.NET开发时,对象之间的相互通讯一般使用事件(event)实现,事件概念是.NET对Delegate的封装. 在Cocos2d-x开发过程中,对象之间的通讯刚开始时不知道如何实现,于是想到c++ ...

  2. Python基础 - 迭代

    前言 在pythone中经常可以看到iterable这样的描述. 直译为迭代. 这是在C中没有的概念. iterable(可迭代) 支持每次返回自己所包含的一个成员的对象就是可迭代对象. iterab ...

  3. 方案猿身高project联赛,艺术家,相反,养殖场!-------三笔

    已经看到了程序猿在电影中都是非常厉害的人物,硬道理键盘噼里啪啦后,奇妙的事情会发生. 当我报了这个专业,開始认真的写程序,在这个领域学习的时候,却发现非常多干这一行 的都自称"码农" ...

  4. CSDN博文大赛赛况简报

    CSDN博文大赛已经開始两周啦.如今赛况怎样呢,接下来,小编为大家揭晓. 大赛自2014年6月10日正式开赛以来.博友们踊跃发表文章,提交文章.到眼下为止,博主们提交博文1045余篇.且以上这些数据还 ...

  5. 调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法

    1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置- ...

  6. notepad扩展搜索,正则搜索

    Dos和windows采用回车+换行CR/LF表示下一行, 0d 0a 两个字节表示而UNIX/Linux采用’\n’换行符LF表示下一行(ASCII代码是10),0a一个字节表示MAC OS系统则采 ...

  7. 阅读zepto.js的core中的Core methods

    学习zepto.js,參考资料:http://www.zeptojs.cn/ 跟jQuery一样.其选择符号也是$; 首先接触的是 $.()  选择 $(selector, [context]) ⇒ ...

  8. ECshop lib_base.php on line 1241 错误解决方法

    ECSHOP做的一个网站,突然报这个错误,整个网站打不开,后来找了很久,终于找到这个方法,亲测可用 Notice: Undefinedvariable: data in D:\wwwroot\KISS ...

  9. 屏蔽DataGridView控件DataError 事件提示的异常信息

    DataGridView.DataError 事件简单介绍: 出现故障.则外部数据分析或验证操作引发异常,或者.当尝试提交数据写入数据源失败. 具体信息:參见MSDN this.dgvState.Da ...

  10. OUI-67076 : OracleHomeInventory was not able to create a lock file&quot; in Unix

    Symptoms The command "opatch lsinventory" reports the error: OUI-67076:OracleHomeInventory ...