原文:乐在其中设计模式(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. html5css3杂记

    最新版本号的safari.chrome.firefox以及opera支持某些html5特性.ie9将支持某些html5特性. html5提供了展现视频的标准<video>支持ogg及mpe ...

  2. B. 沙漠之旅(分组背包)

    B. 沙漠之旅 Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: % ...

  3. Wix打包系列(一)如何使用wix制作安装程序

    原文:Wix打包系列(一)如何使用wix制作安装程序 最近由于项目需要,需要给客户制作安装程序,一开始使用vs2005自带的打包工程来打包,但用了一段时间发现vs打包太死板,而且使用起来问题很多.收费 ...

  4. 10招让你成为杰出的Java程序员(转)

    如果你是一个热衷于技术的 Java 程序员, 那么下面的 10 个要点可以让你在众多 Java 开发人员中脱颖而出. 1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 Ob ...

  5. hdu3496(二维背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3496 题意:题意是 DuoDuo 想看n部电影,但是被要求最长能看的总时间数为 L,每部电影有他的时长 ...

  6. hdu5124(树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:有n条线段,求被覆盖到次数最多的点的次数 分析: 1.可以转化成求前缀和最大的问题:将区间 ...

  7. C++ Primer中文版(第5版)

    <C++ Primer中文版(第5版)> 基本信息 作者: (美)Stanley B. Lippman(斯坦利 李普曼)    Josee Lajoie(约瑟 拉乔伊)    Barbar ...

  8. Window8.1 64位无法使用Debug命令的解决方法[附牛人代码]

    偶然看到网上一篇文章,讲的是世界黑客编程大赛第一名的一个很酷的程序,大小仅有4KB,使用debug命令执行. 悲催的是win8.1的debug命令不能使用. 错误例如以下: 解决方法例如以下: 1. ...

  9. web开发性能优化---项目架构篇

    项目技术架构层级规划和介绍 简称四横两纵 四横即四大层次.分别为: 1.用户渠道层:用户渠道层是直接面向终于用户.通过站点的形式向用户提供产品展示.企业市场宣传.对产品的订购.互动分享.客户关怀以及用 ...

  10. IL来理解属性

    IL来理解属性   阅读目录 概述: C#中如何定义一个属性 Student类 属性Name Main方法 实现get,set方法 性能 访问权限 回到最开始提出的问题 参考资料 .Net底层剖析目录 ...