原文:乐在其中设计模式(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. hdu5176(并查集)

    传送门:The Experience of Love 题意:一个叫Gorwin的女孩和一个叫Vivin的男孩是一对情侣.他们来到一个叫爱情的国家,这个国家由N个城市组成而且只有N−1条小道(像一棵树) ...

  2. android之listView定位到指定行同一时候隐藏输入键盘

    帮别人该bug遇到的一个问题,记录下来. listView.setSelection(a); 这种方法能够让让你的listview定位到指定行 可是假设紧接着运行隐藏输入键盘的代码.则会有bug.这个 ...

  3. WPF案例(二)模拟Apple OS 界面前后180度反转

    原文:WPF案例(二)模拟Apple OS 界面前后180度反转 我们在设计应用程序界面的时候,为了充分利用界面空间,住住需要灵活的界面布局方式,比如可以在界面正面空间上定义一个Chart,背面空间上 ...

  4. hdu2606(递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2606 题意:  用1*1,2*2,3*3,4*4的正方形填充4*n的矩形, 问有多少种不同填法. 分析 ...

  5. zoj2059(经典dp)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1059 分析:dp[i][j]表示前i个石头组成两座塔高度差为j的较低 ...

  6. Android利用反射获取状态栏(StatusBar)高度

    MainActivity如下: package cc.teststatusbarheight; import java.lang.reflect.Field; import android.os.Bu ...

  7. cocos2d_x_05_Box2D物理引擎

    一.认识Box2D 帮助文档,共69页 二.创建一个物理世界 先导入主头文件 #include <Box2D/Box2D.h> 三.物理世界一览 像素转成米 的比例因子 就是32 三.运动 ...

  8. C++ Preprosessor import

    #import Attributes Provides links to attributes used with the #import directive. Microsoft Specific ...

  9. 恩布企业 IM 安卓端 1.3,服务端 1.12 公布

    恩布企业IM的 Android 安卓开源手机client EntboostIM 公布 1.3 版本号.同一时候恩布IM服务端更新至 1.12 版本号; 安卓端主要更新内容: 添加收发手机文件功能: 登 ...

  10. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)

    如无书面授权,请勿转载 第四章,大型项目中Ansible的使用 Roles If your playbooks start expanding beyond what includes can hel ...