Prototype Mode prototype

Source: Internet
Author: User

Prototype mode

Http://www.cnblogs.com/zhili/p/PrototypePattern.html

ICloneable interface

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 a existing object.

The ICloneable interface contains one member, the Clone method, which is intended to provide cloning support beyond th At 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 OB Ject 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, values of the original instance to being 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 ICl Oneable not being 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 T He 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 was copied but the referred object was not; Therefore, the original object and its clone refer to the same object.

For example, consider an object called X is 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 is 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 is 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 being copied to create a second object with property values taken from the first OB Ject. This assumes, the values of an object is entirely defined by its class constructor.

    • Call the MemberwiseClone method to create a shallow copy of a object, and then assign new objects whose values is th E same as the original object to any properties or fields whose values is reference types. The Deepcopy method in the example illustrates this approach.

    • Serialize the object to is deep copied, and then restore the serialized data to a different object variable.

    • Use reflection and recursion to perform, the deep copy operation.

Deep copy and shallow copy

 Public classidinfo{ Public intIdNumber;  PublicIdinfo (intIdNumber) {         This. IdNumber =IdNumber; }} Public classPerson { Public intAge ;  Public stringName;  PublicIdinfo Idinfo;  PublicPerson shallowcopy () {return(person) This.    MemberwiseClone (); }     PublicPerson deepcopy () { person other= (person) This.       MemberwiseClone (); Other. Idinfo=NewIdinfo (Idinfo.idnumber); Other. Name=string.copy (Name); returnOther ; }}

Prototype Mode prototype

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.