乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

简介: 原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern)[索引页][源码下载]乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:webabcd 介绍 用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。
原文: 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

[索引页]
[源码下载]


乐在其中设计模式(C#) - 原型模式(Prototype Pattern)


作者: webabcd


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


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


参考
http://www.dofactory.com/Patterns/PatternPrototype.aspx


OK
[源码下载]
目录
相关文章
|
设计模式 JavaScript Java
【设计模式】【创建型模式】原型模式(Prototype)
一、入门 什么是原型模式? 原型模式(Prototype Pattern)是一种创建型设计模式,它通过复制现有对象来创建新对象,而不是通过实例化类。 原型模式的核心是克隆(Clone),即通过复制现有
380 15
|
设计模式 Java 数据安全/隐私保护
Java 设计模式:装饰者模式(Decorator Pattern)
装饰者模式属于结构型设计模式,允许通过动态包装对象的方式为对象添加新功能,提供比继承更灵活的扩展方式。该模式通过组合替代继承,遵循开闭原则(对扩展开放,对修改关闭)。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
这篇文章详细解释了工厂模式,包括简单工厂、工厂方法和抽象工厂三种类型。每种模式都通过代码示例展示了其应用场景和实现方法,并比较了它们之间的差异。简单工厂模式通过一个工厂类来创建各种产品;工厂方法模式通过定义一个创建对象的接口,由子类决定实例化哪个类;抽象工厂模式提供一个创建相关或依赖对象家族的接口,而不需要明确指定具体类。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
|
设计模式 安全 Java
C# 一分钟浅谈:设计模式之单例模式
【10月更文挑战第9天】单例模式是软件开发中最常用的设计模式之一,旨在确保一个类只有一个实例,并提供一个全局访问点。本文介绍了单例模式的基本概念、实现方式(包括饿汉式、懒汉式和使用 `Lazy&lt;T&gt;` 的方法)、常见问题(如多线程和序列化问题)及其解决方案,并通过代码示例详细说明了这些内容。希望本文能帮助你在实际开发中更好地应用单例模式,提高代码质量和可维护性。
828 1
|
设计模式 算法 C#
C#设计模式之策略模式
C#设计模式之策略模式
335 19
|
设计模式 Java
设计模式--适配器模式 Adapter Pattern
这篇文章介绍了适配器模式,包括其基本介绍、工作原理以及类适配器模式、对象适配器模式和接口适配器模式三种实现方式。
|
设计模式 C# 开发者
C#设计模式入门实战教程
C#设计模式入门实战教程
285 3
|
设计模式 安全 程序员
C#设计模式之单例模式
C#设计模式之单例模式
334 3
|
设计模式 Java 数据库连接
【设计模式】【创建型模式】工厂方法模式(Factory Methods)
一、入门 什么是工厂方法模式? 工厂方法模式(Factory Method Pattern)是一种创建型设计模式,它定义了一个用于创建对象的接口,但由子类决定实例化哪个类。工厂方法模式使类的实例化延迟
449 16