原型模式不是通過new生成新的對象,而使通過複製進行生成; 原型模式適用於相同類型的多個對象的生成; 原型模式分為兩種:淺克隆/淺表副本(Shallow Clone)和深克隆/深表副本(Deep Clone); 淺克隆:Shallow Clone,只複製值類型變數,不複製引用類型變數的克隆;(只複製 ...
原型模式不是通過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>
public 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();
}
}
運行結果