1、浅度拷贝:
public ObjectrPrototype Clone()
{
//实现浅拷贝
return (ObjectrPrototype ) this.MemberwiseClone();
}
ObjectrPrototype 表示返回的类型,原型模式中使用概率较高,放在对应类内部使用。
2、深度拷贝:
public ObjectrPrototype CreateDeepCopy()
{
ObjectrPrototype objectrPrototype;
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, this);
memoryStream.Position = 0;
objectrPrototype= (ObjectrPrototype )formatter.Deserialize(memoryStream);
return objectrPrototype;
}
使用深度拷贝,需要对依赖类必须标注为可序列化的[Serializable]。