C#设计模式(原型模式)

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


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

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值