乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
[源码下载]
作者:webabcd
介绍
用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。
示例
有一个Message实体类,现在要克隆它。
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; }
}
}
}
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; }
}
}
}
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; }
}
}
}
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)相关推荐
- C++设计模式——原型模式(Prototype Pattern)
C++设计模式--原型模式(Prototype Pattern) 微信公众号:幼儿园的学霸 目录 文章目录 C++设计模式--原型模式(Prototype Pattern) 目录 定义 代码示例 普通 ...
- 原型模式(Prototype Pattern)
原型模式(Prototype Pattern) 原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:web ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 设计模式---原型模式(Prototype Pattern)
在编程中有时候我们会发现,当我们需要一个实例,可是这个实例的创建过程十分复杂,在执行过程中 会消耗大量的时间,同时创建第一个实例和创建第二个时间的初始化信息并未改变.在此种情况下,直接New 一个实例 ...
- 极速理解设计模式系列:4.原型模式(Prototype Pattern)
四个角色:抽象原型角色(Prototype).具体原型角色(ConcretePrototype).原型管理器角色(PrototypeManager).客户端角色(Client) 抽象原型角色(Prot ...
- 设计模式之原型模式(Prototype)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式包括:1.FactoryMethod(工厂方法模式):2.Abstract Factory(抽象工厂模式):3.Sin ...
- 设计模式之原型模式prototype
1.原型模式的使用和本质.以及优势: a.通过 new 产生一个对象需要非常繁琐的数据准备或者访问权限,则可以使用原型模式. b.原型模式的使用就是 java 中的克隆技术,以某个对象为原型,复制出新 ...
- 【设计模式】—— 原型模式Prototype
前言:[模式总览]----------by xingoo 模式意图 由于有些时候,需要在运行时指定对象时哪个类的实例,此时用工厂模式就有些力不从心了.通过原型模式就可以通过拷贝函数clone一个原有的 ...
最新文章
- 第十九章 9标签类Label
- java三大集合_java中三大集合框架
- JAVA使用正则表达式给字符串添加分隔符
- 环路的产生及RIP防环机制
- activemq消息持久化_ActiveMQ 5.x中的消息持久性
- ssh放行端口_安全组中已经添加规则放行SSH端口的访问之后如何使用f1 RTL
- Android 系统(165)---在apns-conf文件中配置一个read_only字段,使APN不可被编辑
- 值得收藏的130个神器网站
- setitime和相关函数
- mysql5.7如何打开,mysql57怎么打开
- 项目管理相关的考试认证及证书价值介绍
- 魔兽支持宽屏--怎样让宽屏支持更多游戏?
- 吴恩达机器学习和深度学习视频和笔记
- Tuxera2022Mac系统读写NTFS磁盘工具装机必备
- Verilog实现数字时钟
- 计算机硬盘有坏道,电脑硬盘有坏道怎么办
- bluefish中文乱码问题
- FusionStorage原理及组件,Java面试回忆录
- 漫画人脸检测 | 全局和局部信息融合的深度神经网络(文末源码)
- 轻量级自动化测试框架 UFT 初学者 学习编写