博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二十四种设计模式:原型模式(Prototype Pattern)
阅读量:6704 次
发布时间:2019-06-25

本文共 4268 字,大约阅读时间需要 14 分钟。

原型模式(Prototype Pattern)

介绍
用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。
示例
有一个Message实体类,现在要克隆它。

  MessageModel

using System;using System.Collections.Generic;using System.Text;namespace Pattern.Prototype{    ///     /// Message实体类    ///     public class MessageModel    {        ///         /// 构造函数        ///         /// Message内容        /// Message发布时间        public MessageModel(string msg, DateTime pt)        {            this._message = msg;            this._publishTime = pt;        }        private string _message;        ///         /// Message内容        ///         public string Message        {            get { return _message; }            set { _message = value; }        }        private DateTime _publishTime;        ///         /// Message发布时间        ///         public DateTime PublishTime        {            get { return _publishTime; }            set { _publishTime = value; }        }    }}

  ShallowCopy

using System;using System.Collections.Generic;using System.Text;namespace Pattern.Prototype{    ///     /// 浅拷贝    ///     public class ShallowCopy : ICloneable    {        ///         /// 构造函数        ///         public ShallowCopy()        {                    }        ///         /// 实现ICloneable的Clone()方法        ///         /// 
public Object Clone() { return this.MemberwiseClone(); } private MessageModel _mm; /// /// Message实体对象 /// public MessageModel MessageModel { get { return _mm; } set { _mm = value; } } }}

  DeepCopy

using System;using System.Collections.Generic;using System.Text;namespace Pattern.Prototype{    ///     /// 深拷贝    ///     public class DeepCopy : ICloneable    {        ///         /// 构造函数        ///         public DeepCopy()        {                    }        ///         /// 构造函数        ///         /// Message实体对象        public DeepCopy(MessageModel mm)        {            _mm = mm;        }        ///         /// 实现ICloneable的Clone()方法        ///         /// 
public Object Clone() { return new DeepCopy(new MessageModel(_mm.Message, _mm.PublishTime)); } private MessageModel _mm; /// /// Message实体对象 /// 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演示如下:
"); ShowShallowCopy(); Response.Write("DeepCopy演示如下:
"); 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("
"); Response.Write(sc2.MessageModel.Message); Response.Write("
"); sc.MessageModel.Message = "ShallowCopyShallowCopy"; Response.Write(sc.MessageModel.Message); Response.Write("
"); Response.Write(sc2.MessageModel.Message); Response.Write("
"); } 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("
"); Response.Write(sc2.MessageModel.Message); Response.Write("
"); sc.MessageModel.Message = "DeepCopyDeepCopy"; Response.Write(sc.MessageModel.Message); Response.Write("
"); Response.Write(sc2.MessageModel.Message); Response.Write("
"); }}

  运行结果

  ShallowCopy演示如下:
  ShallowCopy
  ShallowCopy
  ShallowCopyShallowCopy
  ShallowCopyShallowCopy
  DeepCopy演示如下:
  DeepCopy
  DeepCopy
  DeepCopyDeepCopy
  DeepCopy

转载于:https://www.cnblogs.com/taotaodetuer/p/6182912.html

你可能感兴趣的文章
JAVA-基础(三大特性)
查看>>
[BZOJ] 1563: [NOI2009]诗人小G
查看>>
26. Remove Duplicates from Sorted Array
查看>>
mysql5.7.16安装 初始密码获取及密码重置
查看>>
java之代理 静态代理和动态代理
查看>>
springmvc 项目完整示例05 日志 --log4j整合 配置 log4j属性设置 log4j 配置文件 log4j应用...
查看>>
黑马程序员--集合框架【3】
查看>>
Android -- 实现RecyclerView可拖拽Item
查看>>
[软考]之树与二叉树的遍历 ...
查看>>
运算符重载详解
查看>>
K-means算法应用:图片压缩
查看>>
python ----内置函数
查看>>
【洛谷4313】 文理分科(最小割)
查看>>
云服务器 linux文件系统异常an error occurren during the file system check导致服务器启动失败...
查看>>
Linux实战教学笔记07:Linux系统目录结构介绍
查看>>
创建博客的第一天
查看>>
PYTHON-进程 子进程
查看>>
iOS UIButton添加圆角,添加边框
查看>>
UIPasteboard 粘贴板
查看>>
我的winows server 2012 突然多了个piress的帐户,后来一查原来是mysql漏洞的问题,郁闷...
查看>>