原型模式Prototype
原型模式
http://www.cnblogs.com/zhili/p/PrototypePattern.html
ICloneable接口
https://msdn.microsoft.com/en-us/library/system.icloneable(v=vs.110).aspx
Supports cloning, which creates a new instance of a class with the same value as an existing instance.
Remarks
The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object.
The ICloneable interface contains one member, the Clone method, which is intended to provide cloning support beyond that supplied by Object.MemberwiseClone.
For more information about cloning, deep versus shallow copies, and examples, see the Object.MemberwiseClone method.
Notes to Implementers
The ICloneable interface simply requires that your implementation of the Clone method return a copy of the current object instance.
It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between.
Nor does it require all property values of the original instance to be copied to the new instance.
For example, the NumberFormatInfo.Clone method performs a shallow copy of all properties except the NumberFormatInfo.IsReadOnly property;it always sets this property value to false in the cloned object.
Because callers of Clone cannot depend on the method performing a predictable cloning operation, we recommend that ICloneable not be implemented in public APIs.
Object.MemberwiseClone
Creates a shallow copy of the current Object.
Remarks
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.
For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy of C. The example illustrates the difference between a shallow and a deep copy operation.
There are numerous ways to implement a deep copy operation if the shallow copy operation performed by the MemberwiseClone method does not meet your needs. These include the following:
-
Call a class constructor of the object to be copied to create a second object with property values taken from the first object. This assumes that the values of an object are entirely defined by its class constructor.
-
Call the MemberwiseClone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types. The DeepCopy method in the example illustrates this approach.
-
Serialize the object to be deep copied, and then restore the serialized data to a different object variable.
-
Use reflection with recursion to perform the deep copy operation.
深拷贝和浅拷贝
public class IdInfo { public int IdNumber; public IdInfo(int IdNumber) { this.IdNumber = IdNumber; } } public class Person { public int Age; public string Name; public IdInfo IdInfo; public Person ShallowCopy() { return (Person) this.MemberwiseClone(); } public Person DeepCopy() { Person other = (Person) this.MemberwiseClone(); other.IdInfo = new IdInfo(IdInfo.IdNumber); other.Name = String.Copy(Name); return other; } }
ICloneable<T>
http://blog.csdn.net/yapingxin/article/details/12754015
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2016-01-12 Walkthrough: Creating and Running Unit Tests for Managed Code
2016-01-12 Create a unit test project
2016-01-12 How to: Run Tests from Microsoft Visual Studio
2015-01-12 C#中将一个引用赋值null的作用
2015-01-12 关于C#中的垃圾回收
2015-01-12 触发事件,检查是否注册过事件的方法