原型模式(Prototype Pattern)

介绍
用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。

示例
有一个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

二十四种设计模式:原型模式(Prototype Pattern)的更多相关文章

  1. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...

  2. 二十四种设计模式:观察者模式(Observer Pattern)

    观察者模式(Observer Pattern) 介绍定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 示例有一个Message实体类,某些对象 ...

  3. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...

  4. 二十四种设计模式:单例模式(Singleton Pattern)

    单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...

  5. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  6. 二十四种设计模式:解释器模式(Interpreter Pattern)

    解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...

  7. 二十四种设计模式:迭代器模式(Iterator Pattern)

    迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...

  8. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

  9. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

随机推荐

  1. Construct a basic automation test framework

    Elements in an automation test framework: actor,---simulate trader, have its own name, can get contr ...

  2. "由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断"的解决方案

    先使用如下命令登录到服务器: mstsc /v:{服务器IP} /admin 然后再使用下列方法之一即可. 方法一: 1.单击“开始→运行”,输入“gpedit.msc”打开组策略编辑器窗口,依次定位 ...

  3. 电话 SMS 邮件 网页 AppStore

    //调用safar打开网页 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.cnbl ...

  4. iOS app的破解原理,就是去除已付费的账户信息的原理是什么?

    正规的应用程序(IPA 格式,官方软件店发布)在被 iTunes 同步到 iPhone 的时候会调用系统进程 INSTALLD 对应用程序进行证书校验(GPLv3 授权)而这个证书本身是由官方捆绑在应 ...

  5. HDU 1054

    http://acm.hdu.edu.cn/showproblem.php?pid=1054 二分图最少顶点覆盖,模板题,双向边最后结果/2 #include <iostream> #in ...

  6. cmake在实际复杂项目中的使用

    在实际复杂的项目之中,会有很多的源文件,以及对于库的依赖,如果直接使用makefile会比较的繁琐,而且makefile的推导规则也非常多,对多目录的支持也比较复杂. 最近看了一下cmake,发现配置 ...

  7. 《单页Web应用--温故JavaScrpt》学习笔记整理

     变量作用域,函数提升和执行环境对象 1. 变量作用域 在 JavaScript 中,变量 的 作用域 由 函数 限定,即:唯一能定义变量作用域的语块就是 函数. 变量 要么是全局的,要么是局部的. ...

  8. Day08_面向对象第三天

      1.代码块(掌握) 1.概述     由{}扩起来的代码称之为代码块,类或者方法也可认为是代码块,但是一般不这么说,我们平时所说的代码块指的是孤零零的{} 2.代码块作用     局部代码块作用  ...

  9. SSIS 组件点滴

    一 Sort组件 Sort组件是用来排序,我们在做join时也必须进行排序,排序的键值作为数据源关联的key 而在sort组件中有一个选项“Remove Rows with duplicate sor ...

  10. 腾讯优测干货精选| 安卓开发新技能Get -常用必备小工具汇总

    文/腾讯公司 陈江峰 优测小优有话说: 移动研发及测试干货哪里找?腾讯优测-优社区你值得拥有~ 开发同学们都知道,安卓开发路上会碰到很多艰难险阻,一不小心就被KO.这时候,没有新技能傍身怎么行?今天我 ...