通过一个给定的实例创建一个对象,与通过对一个类进行实例化构造新对象不同的是,通过拷贝一个现有的对象生成新的对象。利用原型模式可以不需要工厂方法类。特别是不同对象的类仅仅在属性具有区别,而行为并没有区别时候,通过拷贝一个现有的对象生成新的对象可以减少子类的数目。它与调用构造函数创建新对象的区别在于:拷贝对象一般包括了原有对象的某些状态信息。
模型模式允许动态的增加或减少产品类,产品类不需要非得有任何事先确定的等级结构,原型模式适用于任何的等级结构。[1]
原型模式(Prototype)和生成器模式(Builder)、抽象工厂模式(AbstractFactory)都是通过一个类的对象来专门负责对象的创建工作,它们之间的区别是:原型模式通过拷贝一个现有的对象生成新的对象;生成器模式在进行对象构造之前,要逐步收集构造相关的信息,一步步创建;抽象工厂模式是创建一组相关或者相互依赖的对象提供支持.
using
System;
using System.Collections.Generic;
using System.Text;
namespace PrototypePattern
... {
public class EntityModel
...{
private string _name;
/**//// <summary>
/// 名称
/// </summary>
public string Name
...{
get
...{
return _name;
}
set
...{
_name = value;
}
}
private DateTime _currentTime;
/**//// <summary>
/// 当前时间
/// </summary>
public DateTime CurrentTime
...{
get
...{
return _currentTime;
}
set
...{
_currentTime = value;
}
}
/**//// <summary>
/// 默认构造函数
/// </summary>
public EntityModel()
...{
this._name = string.Empty;
this._currentTime = DateTime.Now;
}
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="name">名称</param>
/// <param name="currentTime">当前时间</param>
public EntityModel(string name, DateTime currentTime)
...{
this._name = name;
this._currentTime = currentTime;
}
}
public abstract class Prototype
...{
public abstract Prototype Clone();
}
/**//// <summary>
/// 浅拷贝原型类
/// </summary>
public class ConcteteShallowPrototype : Prototype
...{
private EntityModel _currentEntityModel;
/**//// <summary>
/// 实体对象
/// </summary>
public EntityModel CurrentEntityModel
...{
get
...{
return _currentEntityModel;
}
set
...{
_currentEntityModel = value;
}
}
/**//// <summary>
/// 默认构造函数
/// </summary>
public ConcteteShallowPrototype()
...{
}
/**//// <summary>
/// 浅拷贝
/// </summary>
/// <returns></returns>
public override Prototype Clone()
...{
return (Prototype)this.MemberwiseClone();
}
}
/**//// <summary>
/// 深拷贝原型类
/// </summary>
public class ConcteteDeepPrototype : Prototype
...{
private EntityModel _currentEntityModel;
/**//// <summary>
/// 实体对象
/// </summary>
public EntityModel CurrentEntityModel
...{
get
...{
return _currentEntityModel;
}
set
...{
_currentEntityModel = value;
}
}
/**//// <summary>
/// 默认构造函数
/// </summary>
public ConcteteDeepPrototype()
...{
}
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="currentEntityModel">实体</param>
public ConcteteDeepPrototype(EntityModel currentEntityModel)
...{
this._currentEntityModel = currentEntityModel;
}
/**//// <summary>
/// 浅拷贝
/// </summary>
/// <returns></returns>
public override Prototype Clone()
...{
return new ConcteteDeepPrototype(new EntityModel(_currentEntityModel.Name, _currentEntityModel.CurrentTime));
}
}
class Program
...{
static void Main(string[] args)
...{
ConcteteShallowPrototype shallowPrototype = new ConcteteShallowPrototype();
shallowPrototype.CurrentEntityModel = new EntityModel("原型", DateTime.Now);
ConcteteShallowPrototype shallowPrototypeCopy = (ConcteteShallowPrototype)shallowPrototype.Clone();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace PrototypePattern
... {
public class EntityModel
...{
private string _name;
/**//// <summary>
/// 名称
/// </summary>
public string Name
...{
get
...{
return _name;
}
set
...{
_name = value;
}
}
private DateTime _currentTime;
/**//// <summary>
/// 当前时间
/// </summary>
public DateTime CurrentTime
...{
get
...{
return _currentTime;
}
set
...{
_currentTime = value;
}
}
/**//// <summary>
/// 默认构造函数
/// </summary>
public EntityModel()
...{
this._name = string.Empty;
this._currentTime = DateTime.Now;
}
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="name">名称</param>
/// <param name="currentTime">当前时间</param>
public EntityModel(string name, DateTime currentTime)
...{
this._name = name;
this._currentTime = currentTime;
}
}
public abstract class Prototype
...{
public abstract Prototype Clone();
}
/**//// <summary>
/// 浅拷贝原型类
/// </summary>
public class ConcteteShallowPrototype : Prototype
...{
private EntityModel _currentEntityModel;
/**//// <summary>
/// 实体对象
/// </summary>
public EntityModel CurrentEntityModel
...{
get
...{
return _currentEntityModel;
}
set
...{
_currentEntityModel = value;
}
}
/**//// <summary>
/// 默认构造函数
/// </summary>
public ConcteteShallowPrototype()
...{
}
/**//// <summary>
/// 浅拷贝
/// </summary>
/// <returns></returns>
public override Prototype Clone()
...{
return (Prototype)this.MemberwiseClone();
}
}
/**//// <summary>
/// 深拷贝原型类
/// </summary>
public class ConcteteDeepPrototype : Prototype
...{
private EntityModel _currentEntityModel;
/**//// <summary>
/// 实体对象
/// </summary>
public EntityModel CurrentEntityModel
...{
get
...{
return _currentEntityModel;
}
set
...{
_currentEntityModel = value;
}
}
/**//// <summary>
/// 默认构造函数
/// </summary>
public ConcteteDeepPrototype()
...{
}
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="currentEntityModel">实体</param>
public ConcteteDeepPrototype(EntityModel currentEntityModel)
...{
this._currentEntityModel = currentEntityModel;
}
/**//// <summary>
/// 浅拷贝
/// </summary>
/// <returns></returns>
public override Prototype Clone()
...{
return new ConcteteDeepPrototype(new EntityModel(_currentEntityModel.Name, _currentEntityModel.CurrentTime));
}
}
class Program
...{
static void Main(string[] args)
...{
ConcteteShallowPrototype shallowPrototype = new ConcteteShallowPrototype();
shallowPrototype.CurrentEntityModel = new EntityModel("原型", DateTime.Now);
ConcteteShallowPrototype shallowPrototypeCopy = (ConcteteShallowPrototype)shallowPrototype.Clone();
Console.ReadLine();
}
}
}
[1]http://blog.csdn.net/zzdong168/archive/2007/05/21/1619891.aspx