原型模式
代码
using System;
using System.Collections.Generic;
/// <summary>
/// 实体类
/// </summary>
public class MessageModel
{
public string Message
{
get;
set;
}
public DateTime PublishTime
{
get;
set;
}
public MessageModel()
{
}
public MessageModel(string msg,DateTime pt)
{
this.Message=msg;
this.PublishTime=pt;
}
}
/// <summary>
/// 浅拷贝
/// </summary>
public class ShallowCopy:ICloneable
{
public ShallowCopy()
{
}
public MessageModel MessageModel
{
get;
set;
}
public object Clone()
{
return this.MemberwiseClone();
}
}
/// <summary>
/// 深拷贝
/// </summary>
public class DeepCopy:ICloneable
{
public DeepCopy()
{
}
public DeepCopy(MessageModel mm)
{
this.MessageModel=mm;
}
public MessageModel MessageModel
{
get;
set;
}
public object Clone()
{
return new DeepCopy(new MessageModel(this.MessageModel.Message,this.MessageModel.PublishTime));
}
}
/// <summary>
/// 测试类
/// </summary>
public class MyClass
{
public static void Main()
{
Console.WriteLine("浅拷贝测试");
ShallowCopy sc=new ShallowCopy();
sc.MessageModel=new MessageModel("浅拷贝",DateTime.Now);
Console.WriteLine(sc.MessageModel.Message);
Console.WriteLine(sc.MessageModel.PublishTime);
Console.WriteLine("****************************");
ShallowCopy sc2=(ShallowCopy)sc.Clone();
MessageModel mymodel=new MessageModel();
mymodel.Message=sc2.MessageModel.Message;
mymodel.PublishTime=sc2.MessageModel.PublishTime;
Console.WriteLine(mymodel.Message);
Console.WriteLine(mymodel.PublishTime);
Console.WriteLine("******************************");
Console.WriteLine("深拷贝测试");
DeepCopy dc=new DeepCopy();
dc.MessageModel=new MessageModel("深拷贝",DateTime.Now);
Console.WriteLine(dc.MessageModel.Message);
Console.WriteLine(dc.MessageModel.PublishTime);
Console.WriteLine("******************************");
DeepCopy dc2=(DeepCopy)dc.Clone();
MessageModel myDeepM=new MessageModel();
myDeepM.Message=dc2.MessageModel.Message;
Console.WriteLine(myDeepM.Message);
myDeepM.PublishTime=dc2.MessageModel.PublishTime;
Console.WriteLine(myDeepM.PublishTime);
Console.ReadLine();
}
}
using System.Collections.Generic;
/// <summary>
/// 实体类
/// </summary>
public class MessageModel
{
public string Message
{
get;
set;
}
public DateTime PublishTime
{
get;
set;
}
public MessageModel()
{
}
public MessageModel(string msg,DateTime pt)
{
this.Message=msg;
this.PublishTime=pt;
}
}
/// <summary>
/// 浅拷贝
/// </summary>
public class ShallowCopy:ICloneable
{
public ShallowCopy()
{
}
public MessageModel MessageModel
{
get;
set;
}
public object Clone()
{
return this.MemberwiseClone();
}
}
/// <summary>
/// 深拷贝
/// </summary>
public class DeepCopy:ICloneable
{
public DeepCopy()
{
}
public DeepCopy(MessageModel mm)
{
this.MessageModel=mm;
}
public MessageModel MessageModel
{
get;
set;
}
public object Clone()
{
return new DeepCopy(new MessageModel(this.MessageModel.Message,this.MessageModel.PublishTime));
}
}
/// <summary>
/// 测试类
/// </summary>
public class MyClass
{
public static void Main()
{
Console.WriteLine("浅拷贝测试");
ShallowCopy sc=new ShallowCopy();
sc.MessageModel=new MessageModel("浅拷贝",DateTime.Now);
Console.WriteLine(sc.MessageModel.Message);
Console.WriteLine(sc.MessageModel.PublishTime);
Console.WriteLine("****************************");
ShallowCopy sc2=(ShallowCopy)sc.Clone();
MessageModel mymodel=new MessageModel();
mymodel.Message=sc2.MessageModel.Message;
mymodel.PublishTime=sc2.MessageModel.PublishTime;
Console.WriteLine(mymodel.Message);
Console.WriteLine(mymodel.PublishTime);
Console.WriteLine("******************************");
Console.WriteLine("深拷贝测试");
DeepCopy dc=new DeepCopy();
dc.MessageModel=new MessageModel("深拷贝",DateTime.Now);
Console.WriteLine(dc.MessageModel.Message);
Console.WriteLine(dc.MessageModel.PublishTime);
Console.WriteLine("******************************");
DeepCopy dc2=(DeepCopy)dc.Clone();
MessageModel myDeepM=new MessageModel();
myDeepM.Message=dc2.MessageModel.Message;
Console.WriteLine(myDeepM.Message);
myDeepM.PublishTime=dc2.MessageModel.PublishTime;
Console.WriteLine(myDeepM.PublishTime);
Console.ReadLine();
}
}
原型模式属于创建型模式,其意图是用原型实例指定创建对 象的种类,并且通过拷贝这些原型创建新的对象。这就要求可以通过复制的方法复制一个对象。定制一个通用的图形编辑器框架,编辑器框架可能有一个工具选择板 用以将这些图形对象添加到画布上,这个选择板还可能包括选择,移动,和其他操纵音乐对象的工具。用户可以点击选择板上的按钮来画一个图形,或者用户可以选 择一个图形移动。这里的关键是定义一个抽象的类,每个选择板上的图形都实现此类的抽象方法,如:选择板上有一个画矩形图形的按钮,当选择这个按钮在画布上点击时,便画出一个图 形。解决办法是通过克隆一个矩形的实例来创建新的图形,被克隆的矩形就是原型模式的原型。画出来的矩形通过调整大小参数,移动位置最终达到用户的需求。
适用性:
当一个系统应该独立于他的产品创建,构成和表示时,要使用Prototype模式,以及
当要实例化的类是在运行时刻指定的,或者为了避免创 建一个与产品类层次平行的工厂类层次时,或者当一个类的实例只能有几个不同状态组合的一种时。建立相应数目的原型并克隆他们可能比每次用合适的状态手工实 例化该类更方便一些。