原文:乐在其中设计模式(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. Kaggle—Digit Recognizer竞赛

    Digit Recognizer 手写体数字识别  MNIST数据集 本赛 train 42000样例 test 28000样例,原始MNIST是 train 60000 test 10000 我分别 ...

  2. sql连接错误(Microsoft SQL Server,错误:2)

    昨天用SQL语句建表的时候写了一段代码,对于代码的逻辑和内容我不太肯定对不正确.反正是毫不犹豫的让它运行了,过程中出现好几个错误,当时没有太在意,想着大不了出错了再重写一个.结果--玩坏了,从昨天到如 ...

  3. 全国各大 oj 分类题集...

    各种题集从易到难刷到手软  你准备好了吗? 准备剁手吧

  4. poj3613(恰经过N条边的最短路)

    题目连接:http://poj.org/problem?id=3613 题意:从S 到 T 经过边得个数恰为k的最短路是多少. 分析:01邻接矩阵A的K次方C=A^K,C[i][j]表示i点到j点正好 ...

  5. ubuntu终端方向键不能用(主机名不显示)问题的解决

    sudo gedit /etc/passwd 在/etc/passwd中改动该用户相应的shell:/bin/sh改为/bin/bash就可以解决该问题

  6. coco2d-x CCScrollView实现背包翻页,仅供参考

    #include "CCCGameScrollView.h" USING_NS_CC; USING_NS_CC_EXT; CCCGameScrollView::CCCGameScr ...

  7. 逆向project第004篇:令计算器程序显示汉字(下)

    一.前言 钩子技术是一项很有有用价值的技术.在Windows下HOOK技术的方法比較多,使用比較灵活,常见的应用层的HOOK方法有Inline HOOK(详见<反病毒攻防研究第012篇:利用In ...

  8. c++自带倒置数组函数

    #include<stdio.h> #include <vector> #include <queue> #include<algorithm> usi ...

  9. NYOJ 45 棋盘覆盖 模拟+高精度

    题意就不说了,中文题... 小白上讲了棋盘覆盖,于是我就挖了这题来做. 棋盘覆盖的推导不是很难理解,就是分治的思想,具体可以去谷歌下. 公式就是f(k) = f(k - 1) * 4 + 1,再化解下 ...

  10. 三星Samsung 4.4.2该负责人制度,简化名单

    installed uninstalled AccessControl.apk AllshareControlShare.apk AirMotionTryActually.apk AllshareFi ...