二十四种设计模式:原型模式(Prototype Pattern)
原型模式(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)的更多相关文章
- 二十四种设计模式:适配器模式(Adapter Pattern)
适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...
- 二十四种设计模式:观察者模式(Observer Pattern)
观察者模式(Observer Pattern) 介绍定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 示例有一个Message实体类,某些对象 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- 二十四种设计模式:单例模式(Singleton Pattern)
单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- 二十四种设计模式:解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...
- 二十四种设计模式:迭代器模式(Iterator Pattern)
迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...
- 二十四种设计模式:策略模式(Strategy Pattern)
策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
随机推荐
- Ubuntu安装Fcitx(小企鹅五笔输入法)
安装配置如下: 1. 安装 fcitx sudo apt-get install fcitx 2. 配置默认输入法为 fcitx im-switch -s fcitx // 注意无须加 sudo 3. ...
- zf2-tutorial调通,坑爹的init_autoloader.php
zf2-tutorial的作者把init_autoloader.php内容写错了,新建个工程,把其中的这个文件的内容替一下,然后建库/建表,把local.ini打开(不是必须的),用户名/口令等配好, ...
- OC中修饰符:宏define 常量:const extern
const const最好理解,修饰的东西不能被修改 指针类型根据位置的不同可以理解成3种情况: I 常量指针 // 初始化之后不能赋值,指向的对象可以是任意对象,对象可变. NSString * c ...
- HDU5437 Alisha’s Party 优先队列
点击打开链接 可能出现的问题: 1.当门外人数不足p人时没有判断队列非空,导致RE. 2.在m次开门之后最后进来到一批人没有入队. 3.给定的开门时间可能是打乱的,需要进行排序. #include&l ...
- html释疑
解析<button>和<input type="button"> 的区别(转) 一.定义和用法 <button> 标签定义的是一个按钮. 在 b ...
- 【大胃王】2013暴食女王巅峰战(安吉拉x三宅x正司x木下)熟肉+高能
一边说着“不可以还没试就先放弃”,一边认真吃饭的女生真的让人抵抗不了啊. http://www.bilibili.com/video/av2395980/
- [转Go-简洁的并发 ]
http://www.yankay.com/go-clear-concurreny/ Posted on 2012-11-28by yankay 多核处理器越来越普及.有没有一种简单的办法,能够让我们 ...
- msp430 问题及解决记录
----------------------------- 2015.4.28 问题:开发板串口显示的内容为乱码 解决:使用的是原先产品主板的15200的波特率设置,但看来或者是开发板不支持11520 ...
- (实用篇)PHP实现队列及队列原理
队列是一种线性表,按照先进先出的原则进行的: PHP实现队列:第一个元素作为队头,最后一个元素作为队尾 <?php /** * 队列就是这么简单 * * @link */ $array = ar ...
- UI学习笔记---第六天
UIControl及其子类 UISegmentedControl的用法 UISegmentedControl是iOS中得分段控件,每个segment都能被点击,相当于集成了若干个button.通常我们 ...