设计模式之:原型模式PrototypePattern的实现(浅克隆和深克隆)(浅表副本和深表副本)

原型模式不是通过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();        }    }

 运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值