DOTNET framework source code mode (6) -- prototype (prototype)

Source: Internet
Author: User
Tags dotnet

Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

To. NET Framework 2.0 system. CollectionsFor example

System. Collections. icollection

Public   Interface Icollection: ienumerable
{
//
}

 

System. Collections. icloneable

Public   Interface Icloneable
{
Object Clone ();
}

System. Collections. Stack

Code
Public   Class STACK: icollection, icloneable
{
//
Public   Virtual Object clone ()
{
Stack s =   New Stack (_ size );
S. _ size = _ Size;
Array. Copy (_ array, 0 , S. _ array, 0 , _ Size );
S. _ version = _ Version;
Return S;
}
//
}

System. Collections. Queue

Code
Public   Class Queue: icollection, icloneable
{
//
Public   Virtual Object clone ()
{
Queue Q =   New Queue (_ size );
Q. _ size = _ Size;

Int Numtocopy = _ Size;
Int Firstpart = (_ Array. Length - _ Head < Numtocopy) ? _ Array. Length - _ Head: numtocopy;
Array. Copy (_ array, _ head, Q. _ array, 0 , Firstpart );
Numtocopy -= Firstpart;
If (Numtocopy >   0 )
Array. Copy (_ array, 0 , Q. _ array, _ array. Length - _ Head, numtocopy );

Q. _ version=_ Version;
ReturnQ;
}
//
}

CallCode

Code
Public   Class Client
{
Public   Static   Void Main ()
{
Stack mystack =   New Stack ();
Mystack. Push ( " Hello " );
Mystack. Push ( " World " );
Mystack. Push ( " ! " );

Stack mystackcopy=(Stack) mystack. Clone ();
Foreach(StringSInMystackcopy)
{
Console. Write (s );
}
Console. writeline ();
Console. Readline ();
}
}

In. NET Framework.IcloneableInterface to implement the prototype mode,IcloneableThere is only one interfaceCloneMethod. There are two clone implementation methods (Shallow copy) And deep copy (Deep copy).

 When the field value of an object is copied, the object referenced by the field is not copied. For example, if an object has a field pointing to a string and we make a shortest copy of the object, the two objects will reference the same string. Deep copy is also a method for copying objects referenced by fields in the object instance. Therefore, if an object has a field pointing to a string, if we make a deep copy of the object, we will create a new object and a new string.--The new object references the new string. Note that after deep copy, the original object and the newly created object will not share anything; changing an object will not affect the other object.

For the value type, the shortest copy is directly implemented by assigning values and other operations. The field of the value type in the object is copied to the new object. The deep copy and shortest copy are the same, by assigning values and other operations, you can directly copy the field of value type in the object to the new object. For the reference typeMemberwisecloneMethod To create a shallow copy. The method is to create a new object. If the field is of the value type, perform a one-by-one copy of the field. If the field is of the reference type, then, copy and reference the original object to reference the same object as the original object. Copy the object application in depth and copy the actual content of the object, that is, create a new object, changing the new object does not affect the content of the original object.

 

UsePrototypeMode:

1. When a system should be independent of its product creation, composition, and representation;

2. When the class to be instantiated is specified at the runtime, for example, through dynamic loading;

3. To avoid creating a factory class level parallel to the product class level;

4. When an instance of a class can only have one of several combinations of different States. Creating prototype and cloning them may be more convenient than manually instantiating this type with the appropriate state each time.

 

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.