设计模式---原型模式
来源:互联网 发布:windows db2 编辑:程序博客网 时间:2024/06/06 03:27
一、执行一次构造函数,以后的对象是直接 clone出来的
StudentProperty SingletonConle = (StudentProperty)_StudentProperty.MemberwiseClone();
执行的copy是浅拷贝
public class StudentProperty { private StudentProperty() { Console.WriteLine("创建对象"); } private static StudentProperty _StudentProperty = new StudentProperty() {
Course = new Course() { CourseId =6, CourseName= "kekekekeek"} };静态对象,也是由操作系统在一开始为静态变量分配好内存 public static StudentProperty CreateIntance() { StudentProperty SingletonConle = (StudentProperty)_StudentProperty.MemberwiseClone(); return SingletonConle; } public int Id { get; set; } public string Name { get; set; } public Course Course { get; set; } } public class Course { public int CourseId { get; set; } public string CourseName { get; set; } }
二、创建对象:
这样创建的两个对象拥有自己的属性和值,但是当设置了 StudentProperty 的 Course 属性后,他们两个的 Course 属性值是相同的,原因是他们指向了相同
的引用地址空间
StudentProperty StudentProperty1 = StudentProperty.CreateIntance(); StudentProperty1.Id = 1; StudentProperty1.Name = "学生1"; StudentProperty1.Course.CourseId = 7; StudentProperty1.Course.CourseName = "课程名称1"; StudentProperty StudentProperty2 = StudentProperty.CreateIntance(); StudentProperty2.Id = 2; StudentProperty2.Name = "学生2"; StudentProperty2.Course.CourseId = 8; StudentProperty2.Course.CourseName = "课程名称88888";
这样当 StudentProperty2中 Course 的属性改变 StudentProperty1的值也跟着改变
三、深拷贝:使用序列化和反序列化
StudentProperty StudentProperty3 = SerializeHelper.DeserializeWithBinary<StudentProperty>(SerializeHelper.SerializeToBinary(StudentProperty2)); StudentProperty3.Course.CourseId = 9; StudentProperty3.Course.CourseName = "课程名称9999999999";
四、SerializeHelper类
public static class SerializeHelper { /// <summary> /// 使用UTF8编码将byte数组转成字符串 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string ConvertToString(byte[] data) { return Encoding.UTF8.GetString(data, 0, data.Length); } /// <summary> /// 使用指定字符编码将byte数组转成字符串 /// </summary> /// <param name="data"></param> /// <param name="encoding"></param> /// <returns></returns> public static string ConvertToString(byte[] data, Encoding encoding) { return encoding.GetString(data, 0, data.Length); } /// <summary> /// 使用UTF8编码将字符串转成byte数组 /// </summary> /// <param name="str"></param> /// <returns></returns> public static byte[] ConvertToByte(string str) { return Encoding.UTF8.GetBytes(str); } /// <summary> /// 使用指定字符编码将字符串转成byte数组 /// </summary> /// <param name="str"></param> /// <param name="encoding"></param> /// <returns></returns> public static byte[] ConvertToByte(string str, Encoding encoding) { return encoding.GetBytes(str); } /// <summary> /// 将对象序列化为二进制数据 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] SerializeToBinary(object obj) { MemoryStream stream = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(stream, obj); byte[] data = stream.ToArray(); stream.Close(); return data; } /// <summary> /// 将对象序列化为XML数据 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] SerializeToXml(object obj) { MemoryStream stream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(obj.GetType()); xs.Serialize(stream, obj); byte[] data = stream.ToArray(); stream.Close(); return data; } /// <summary> /// 将二进制数据反序列化 /// </summary> /// <param name="data"></param> /// <returns></returns> public static object DeserializeWithBinary(byte[] data) { MemoryStream stream = new MemoryStream(); stream.Write(data, 0, data.Length); stream.Position = 0; BinaryFormatter bf = new BinaryFormatter(); object obj = bf.Deserialize(stream); stream.Close(); return obj; } /// <summary> /// 将二进制数据反序列化为指定类型对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static T DeserializeWithBinary<T>(byte[] data) { return (T)DeserializeWithBinary(data); } /// <summary> /// 将XML数据反序列化为指定类型对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static T DeserializeWithXml<T>(byte[] data) { MemoryStream stream = new MemoryStream(); stream.Write(data, 0, data.Length); stream.Position = 0; XmlSerializer xs = new XmlSerializer(typeof(T)); object obj = xs.Deserialize(stream); stream.Close(); return (T)obj; } }
阅读全文
0 0
- 设计模式:原型模式
- 设计模式------原型模式
- 设计模式 原型模式
- 设计模式-【原型模式】
- 设计模式-原型模式
- 设计模式-原型模式
- 设计模式-原型模式
- 设计模式-原型模式
- 设计模式- 原型模式
- 设计模式:原型模式
- 设计模式 - 原型模式
- 设计模式-原型模式
- 【设计模式】原型模式
- 设计模式-原型模式
- 设计模式-原型模式
- 【设计模式】原型模式
- 设计模式----原型模式
- 设计模式------原型模式
- CCF 201703-2 学生排队 Java
- 利用Volume在主机和Docker容器文件传输。
- C++单链表面试题
- Java并发面试问题
- JVM运行时数据区域及GC
- 设计模式---原型模式
- JVM学习笔记(二)——对象之生
- hibernate框架一对多cascade取值和孤儿删除(十四)
- ARM指令集 VS Thumb指令集
- 卷积神经网络CNN——使用keras识别猫咪
- 寻求高手马甲包封装APP上架问题!
- MIME---multipart类型
- Kafka的Replica分配策略之二 Replica变为0了怎么办
- c# 关于json 的引用