原型模式不是通过new生成新的对象,而使通过复制进行生成;
原型模式适用于相同类型的多个对象的生成;
原型模式分为两种:浅克隆/浅表副本(Shallow Clone)和深克隆/深表副本(Deep Clone);
浅克隆:Shallow Clone,只复制值类型变量,不复制引用类型变量的克隆;(只复制引用类型变量的地址,其实还是指向同一个引用类型)
深克隆:Deep Clone,同时复制值类型和引用类型变量的克隆;
浅克隆用到的技术:
- Object.MemberwiseClone 方法(命名空间:System),使用该方法可以进行浅克隆;
深克隆用到的技术:
- BinaryFormatter 类(命名空间:System.Runtime.Serialization.Formatters.Binary),用来将需要克隆的类进行序列化和反序列化;
- FileStream 类 (命名空间:System.IO),用来将序列化的数据存储到临时文件中,克隆的时候从文件中读取数据;
- SerializationException 类(命名空间:System.Runtime.Serialization),用来捕获序列化/反序列化过程中的异常;
- SerializableAttribute 类(命名空间:System),用来对需要克隆的类进行标记,表示改类可以序列化;
类图 with StarUML
浅克隆Shallow Clone
internal class WeeklyLog { private Attachment attachment; public Attachment Attachment { get => attachment; set => attachment = value; } /// <summary> /// 浅克隆 /// </summary> /// <returns></returns> pub 河北干部培训学校 www.gsganxun.cn lic WeeklyLog ShallowClone() { return this.MemberwiseClone() as WeeklyLog; } }
internal class Attachment { private string name; public string Name { get => name; set => name = value; } public void DownLoad() { Console.WriteLine($"下载附件,文件名为:{name}"); } }
internal class Program { static void Main(string[] args) { WeeklyLog previousLog, newLog; previousLog = new WeeklyLog(); Attachment attachment = new Attachment(); previousLog.Attachment = attachment; newLog = previousLog.ShallowClone(); Console.WriteLine("--浅表副本/浅克隆(Shallow Clone)的测试--"); Console.WriteLine("周报是否相同?{0}", previousLog == newLog ? "是" : "否"); Console.WriteLine("附件是否相同?{0}", (previousLog.Attachment == newLog.Attachment) ? "是" : "否"); Console.Read(); } }
运行结果
深克隆Deep Clone
[Serializable] internal class WeeklyLog { private Attachment attachment; public Attachment Attachment { get => attachment; set => attachment = value; } /// <summary> /// 深克隆DeepClone /// </summary> /// <returns></returns> public WeeklyLog DeepClone() { WeeklyLog clone = null; FileStream stream1 = new FileStream("Temp.dat", FileMode.Create); BinaryFormatter formatter1 = new BinaryFormatter(); //序列化 try { formatter1.Serialize(stream1, this); } catch (SerializationException ex) { Console.WriteLine($"序列化失败:{ex.Message}"); } finally { stream1.Close(); } FileStream stream2 = new FileStream("Temp.dat", FileMode.Open); BinaryFormatter formatter2 = new BinaryFormatter(); //反序列化 try { clone = formatter2.Deserialize(stream2) as WeeklyLog; } catch (SerializationException ex) { Console.WriteLine($"反序列化失败:{ex.Message}"); } finally { stream2.Close(); } return clone; } }
[Serializable] internal class Attachment { private string name; public string Name { get => name; set => name = value; } public void DownLoad() { Console.WriteLine($"下载附件,文件名为:{name}"); } }
internal class Program { static void Main(string[] args) { WeeklyLog previousLog, newLog; previousLog = new WeeklyLog(); Attachment attachment = new Attachment(); previousLog.Attachment = attachment; newLog = previousLog.DeepClone(); Console.WriteLine("--深表副本/深克隆(Deep Clone)的测试--"); Console.WriteLine("周报是否相同?{0}", previousLog == newLog ? "是" : "否"); Console.WriteLine("附件是否相同?{0}", (previousLog.Attachment == newLog.Attachment) ? "是" : "否"); Console.Read(); } }
运行结果