乐在其中设计模式(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 ...
随机推荐
- Java程序猿面试题集(181- 199)
Java面试题集(181-199) 摘要:这部分是包括了Java高级玩法的一些专题,对面试者和新入职的Java程序猿相信都会有帮助的. 181. 182. 183. 184. 185. 186. 1 ...
- leetcode第一刷_Unique Paths
从左上到右下,仅仅能向右或向下,问一共同拥有多少种走法. 这个问题当然能够用递归和dp来做,递归的问题是非常可能会超时,dp的问题是须要额外空间. 事实上没有其它限制条件的话,这个问题有个非常easy ...
- 使用notepad运行python
Notepad++ 是一个开源的文本编辑器,功能强大而且使用方便,一般情况下,Notepad++作为代码查看器,很方便,但是每次要运行的时候,总是需要用右键打开其他的IDE来编译和运行,总有些不方便. ...
- 鸟哥Linux私房菜知识点总结6到7章
近期翻看了一本<鸟哥的Linux私房菜>.这是一本基础的书,万丈高楼平地起.会的不多但能够学.这是我整理的一些知识点.尽管非常基础.希望和大家共同交流. 第6章主机规划与磁盘分区 1.在进 ...
- java 中间 final修饰符
修饰符final:它是一个常数,我不同意改变 ,可以修改 变数,办法 ,分类 final修改变量:是final成常量,一旦赋值不能改变 常量能够在初始化时直接赋值.也能够在构造方法里赋值.仅仅能在这两 ...
- Java程序猿笔试面试之String1
1.怎样实现字符串的反转比如:"how are you"--->"you are how" public class InverseString { pu ...
- 从SAE又回到BAE,感觉好轻松
[前言] 我这个人总喜欢对同一类东西比較过来比較过去,用过来用过去. 比如曾经选择浏览器,从開始ie,到遨游,世界之窗.qq等等,用了有10款左右的浏览器,每款都用了不短时间, 终于固定在火狐+chr ...
- Oracle连接池
原由:许多用户可能在查询相同的数据库以获取相同的数据.在这些情况下,可以通过使应用程序共享到数据源的连接来提高应用程序的性能.否则,让每个用户打开和关闭单独的连接的开销会对应用程序性能产生不利影响.这 ...
- Class ThreadPoolExecutor
Class ThreadPoolExecutor java.lang.Object java.util.concurrent.AbstractExecutorService java.util.con ...
- Android在Context详细解释 ---- 你不知道Context
...