首页 > 其他 > 详细

原型模式Prototype

时间:2017-01-12 18:25:46      阅读:22      评论:0      收藏:0      [点我收藏+]

标签:col   模式   tab   form   some   mar   style   rem   rst   

原型模式

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;
    }
}

 

原型模式Prototype

标签:col   模式   tab   form   some   mar   style   rem   rst   

原文:http://www.cnblogs.com/chucklu/p/6279168.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号