乐在其中设计模式(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)的更多相关文章
- 二十四种设计模式:原型模式(Prototype Pattern)
原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...
- python 设计模式之原型模式 Prototype Pattern
#引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...
- 【UE4 设计模式】原型模式 Prototype Pattern
概述 描述 使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.如孙悟空猴毛分身.鸣人影之分身.剑光分化.无限剑制 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, ...
- Net设计模式实例之原型模式( Prototype Pattern)
一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...
- 设计模式系列之原型模式(Prototype Pattern)——对象的克隆
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 【设计模式】原型模式 Pototype Pattern
前面讲了创建一个对象实例的方法单例模式Singleton Pattern, 创造多个产品的工厂模式(简单工厂模式 Simple Factory Pattern, 工厂方法模式 FactoryMothe ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...
- 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...
- 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)
原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...
随机推荐
- 使用模板类导致error LNK2019: 无法解析的外部符号
原地址 1.定义模板类: template<class T> class Stack {....}; 2.定义模板成员函数: 每个函数头都要以相同的模板声明打头,并将类限定符改成:类名&l ...
- git merge,rebase和*(no branch)
上一篇:http://blog.csdn.net/xiaoputao0903/article/details/23933589,说了git的分支,相关的使用方法没说到可是仅仅要google就能搜出一大 ...
- 使用EXCEL设置“下拉菜单”选项功能
原创作品.出自 "深蓝的blog" 博客.欢迎转载.转载时请务必注明出处,否则有权追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...
- IE, FireFox, Opera 浏览器支持CSS实现Alpha透明的方法 兼容问题
一:要解决的问题时:在ie6-ie11下兼容下面透明上传文件button的效果. 实现方式通过滤镜实现. 二:效果图例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- poj1797(最短路小变形)
题目连接:http://poj.org/problem?id=1797 题意: 分析:dp[i]表示到达i点的过程中的最大承受重量,更新到i点时可能有多条路径,由优先队列堆出最大的那条即可. #inc ...
- Effective C++ -- 构造析构赋值运算
05.了解C++默默编写并调用哪些函数 编译产生的析构函数时non-virtual,除非这个类的基类析构函数为virtual 成员变量中有引用和const成员时,无法自己主动生成copy assign ...
- Django写的投票系统4(转)
原文地址:http://www.cnblogs.com/djangochina/archive/2013/06/04/3114269.html 现在已经可以在后台管理投票了,现在就差怎么在前台显示和如 ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第二步---编辑器(1)--触摸加入点
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦,他说:随便写,第一别全然照搬代码:第二能够说 ...
- POJ3050 Hopscotch 【DFS】
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2113 Accepted: 1514 Descrip ...
- iis 隐藏 banner
去微软官网下载这个补丁 http://www.microsoft.com/en-us/search/DownloadResults.aspx?q=urlscan+3.1 2. 安装urlscan_ ...