Prototype model is used to create a duplicate object, when you want to create a new object but the overhead is relatively large or want to preserve the current state of the object, we can use the prototype model.
Prototyping
public abstract class Base { // because of the special nature of String, so this demonstration we use the StringBuilder public the StringBuilder the Name { GET ; the SET ;} public int Age { GET ; the SET ;} public Base () { // simulate it takes to create objects overhead the Thread.Sleep ( 1000 ); } public Base (String name, int Age) { the this .Name = new new the StringBuilder (name); the this .Age = Age; //Simulation objects creates overhead expended in the Thread.Sleep ( 1000 ); } // deep copy public abstract Base Clone (); // shallow copy public abstract Base MClone (); }
Next Peron create a class that inherits Base, copy and implements two methods
// if it is to be deep copy serialization, then, play the Serializable label [Serializable] public class the Person: Base { public the Person () : Base () { } public the Person (String name, int Age) : Base (name , Age) { } /// <Summary> /// deep copy /// </ Summary> /// <returns> returns a new Person object </ returns> public the override Base Clone () { // create a memory stream MemoryStream ms =new new the MemoryStream (); // create a binary object sequence the BinaryFormatter BF = new new the BinaryFormatter (); // current object serialization in the stream memory write ms bf.Serialize (ms, the this ); // set stream read position ms.Position = 0 ; // flow deserialize objects Object return bf.Deserialize (MS) AS the Person; } /// <Summary> /// shallow copy /// </ Summary> /// < Returns> </ Returns> public the override Base MClone () => // shallow copy this.MemberwiseClone() as Person; }
Main method calls, first of all every time we create a new Person object
static void Main(string[] args) { //用于计时 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Person p = new Person("a",28); Person p2 = new Person("a",28); Person p3 = new Person("a",28); stopwatch.Stop(); Console.WriteLine("耗时:"+stopwatch.Elapsed); Console.ReadKey(); }
operation result:
If the object is visible if you create a lot of overhead, then, every time when they are created with efficiency will be very low
Next, we use the prototype model to create a duplicate object, call MClone () shallow copy
static void the Main ( String [] args) { // for clocking The Stopwatch Stopwatch = new new The Stopwatch (); stopwatch.Start (); the Person P = new new the Person ( " A " , 10 ); the Person P1 = p.MClone () AS the Person; the Person P2 = p.MClone () AS the Person; // record name value, immediate subsequent viewing window through the StringBuilder NAME2 = p2.Name; the StringBuilder NAME1 = p1.Name; stopwatch.Stop (); Console.WriteLine ("耗时:"+stopwatch.Elapsed); Console.ReadKey(); }
In Console.ReadKey (); at the set breakpoints, run programs, open the Debug window >> >> instant, real-time window in the lower right corner of the input * & name1 Enter, * & name2 enter, view name1 and name2 memory address
We can see, name1 and name2 memory addresses are the same, indicating that p2.Name and p1.Name is point to the same reference. So for the type of property is a reference to an object, to achieve a shallow copy, all attribute references will point to the same object, that is to say as long as there is an object to modify the Name attribute, the Name property of other objects will change.
Look at the operating results
Can be found only for the first time to create objects requires a lot of overhead, then copied by the prototype is very fast.
Next we look at the deep copy
static void the Main ( String [] args) { // for clocking The Stopwatch Stopwatch = new new The Stopwatch (); stopwatch.Start (); the Person P = new new the Person ( " A " , 10 ); the Person P1 = p.Clone () AS the Person; the Person P2 = p.Clone () AS the Person; // record name value, immediate subsequent viewing window through the StringBuilder NAME2 = p2.Name; the StringBuilder NAME1 = p1.Name; stopwatch.Stop (); Console.WriteLine ("耗时:"+stopwatch.Elapsed); Console.ReadKey(); }
Performs the same operation now, look at memory address
Can be found, p1 and p2 Name address in memory is not the same, is to explain all non-static properties deep copy of the object will have a copy, if you encounter will re-create a reference type, instead of copying references to objects.
Finally, we look at the operating results