原文:乐在其中设计模式(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. Android笔记二十七.Service组件入门(一).什么是Service?

    转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空) 一.Service 1.Service简单介绍     Service为Android四大组件之中 ...

  2. php 双向队列类

    (deque,全名double-ended queue)是一种具有队列和栈的性质的数据结构.双向队列中的元素能够从两端弹出,其限定插入和删除操作在表的两端进行. 在实际使用中,还能够有输出受限的双向队 ...

  3. Kafka 高性能吞吐揭秘

    Kafka 高性能吞吐揭秘   Kafka作为时下最流行的开源消息系统,被广泛地应用在数据缓冲.异步通信.汇集日志.系统解耦等方面.相比较于RocketMQ等其他常见消息系统,Kafka在保障了大部分 ...

  4. 读懂Java中的Socket编程(转)

    Socket,又称为套接字,Socket是计算机网络通信的基本的技术之一.如今大多数基于网络的软件,如浏览器,即时通讯工具甚至是P2P下载都是基于Socket实现的.本文会介绍一下基于TCP/IP的S ...

  5. Linux 单用户模式的使用

    在进入系统启动菜单时,选择kernel,输入"e"后进入,在kernel开头的启动项后输入(空格) /single或者 / 1,然后输入"b"重新启动机器,此时 ...

  6. Catch Up 朋友小聚 - 地道英语 - BBC Learning English BBC英语教学 - 爱思英语网

    Catch Up 朋友小聚 - 地道英语 - BBC Learning English BBC英语教学 - 爱思英语网 Catch Up 朋友小聚 分享到: 新浪微博 QQ空间 腾讯微博 微信 更多 ...

  7. Android开展:ADT+Eclipse使用错误:Text editor does not have a document provider

    Eclipse参加Android sdk源代码 正在使用Eclipse进行Android开发时间,我们经常需要导入sdk源代码来Eclipse中,方便api阅读和查询,详细操作为:ctrl+鼠标左键. ...

  8. Trie图和Fail树

    Trie图和AC自动机的区别 Trie图是AC自动机的确定化形式,即把每个结点不存在字符的next指针都补全了.这样做的好处是使得构造fail指针时不需要next指针为空而需要不断回溯. 比如构造ne ...

  9. Linux查看用户数、登录用户

    如果是系统中全部只要默认shell是bash的就包括那么二楼正解,就是cat /etc/passwd|grep bash|wc -l如果是正在登陆系统的账户中使用bash shell的,那么ps -e ...

  10. vmware: The file system upon which * resides is critically low on free space.

    The file system upon which ******.localized/Windows XP Professional.vmwarevm' resides is critically ...