Design mode - prototype mode
Reference
"C # technology reveals"
"Design mode - can be used for object-oriented software
"Java and Mode"
Http://blog.9cbs.net/beautyispower/, very good netizen blog
There are menu in many restaurants, which are listed in which foods are there, which pasta, etc., the general Lanzhou Rana Museum is like this.
Beef ramen (large bowl)
Beef ramen (small bowl)
Beef knife face
Mutton ramen
Mutton knife
You have arrive here, you say that I have to eat my hand, I have to eat, I want to eat pearl jade abalone, he is afraid, there is no, these faces need to be fixed
System, the beef rainders, beef knife faces, etc. can be seen as prototypes, and the hood can be seen as prototype managers.
The prototype mode is not directly created in the creation object, that is, not called by the constructor of the external call class, but passed
Existing object instance cloned an object, this cloned object has the same properties and status, which is in the hoodemark.
Beef knife faces every bowl of state.
Clone object is divided into shallow copy and deep copy
Shallow copy is the object of cloning object and its source object sharing reference, for an example, may not properly, assume the beef knife cut into an object: Money, indicating how much it is worth it, here is SYSTEM.Object Inherited (C #), that is, different shallow copy objects, their price is the same, after the price of the cloned object has changed, the price of the object it will change, such as the noodle house
The price of the beef knife is lowered, and the 5 dollars have adjusted 4 yuan, then the price per bowl has changed to four dollars. However, for value types, each shallow copy object has its own copy, that is, when changing the value type of the cloned object, its source object does not change.
Deep copy is a complete copy
Not only the value type has its own copy, and even the reference object also has its own copy, modify any properties of the cloned object, nor does any impact on the source object.
Prototype Manager is to provide prototype registration, when creating objects, can be cloned using objects in the object, can also add them to the prototype manager when there is a new instance object.
It is a bit chaotic, programmer, or with code communication, the following procedures realize shallow copy and deep copy of the prototype clone.
/// Understand Deep Copy and Shallow Copy /// / Shallow Diagram Create a new instance of the same type of original object, and then copy the non-static field of the original object. // If the field is a value type, the field is docked in line. If the field is a reference type, copy this // reference but do not copy the referenced objects; thus, reference to the reference and reputation in the original object points to the same object. / / In contrast, the entire contents of the object's deep copy copy objects are directly or indirectly referenced. ////, for example, if x is an Object with reference to the object A and object B, and the object A also // has a reference to the object M, the X's shallow copy is the object Y, and Y is equally Reference to object a and object b //. In contrast, the deep copy of X is the object y, and the object Y has direct lead // of the object C and object D, and the indirect reference to the object N, where C is a copy of A, D is a copy of B, and N is M'S copy.
using System; using System.Collections; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; using System.Data; namespace Prototype {///
// Define prototype Manager NoodleManager NoodleManager = new noodleManager (); // Customer Requirements The following three bowl nookle beefNoodle = (Noodle) NoodleManager ["Beef Ramen"] .clone ();
// Noodle beefNoodle = (Noodle) noodleManager [ "beef noodles"] .DeepClone (); Noodle muttonNoodle = (Noodle) noodleManager [ "mutton noodles"] .Clone (); Noodle beefCutNoodle = (Noodle) noodleManager [ "beef noodle" ] .Clone ();
/ / Modify the properties of the reference object in the clone object, verify that it is a shallow copy or a deep copy beefnoodle.tbname = ", hahaha!
// Show the NoodelName and TBName Console.Writeline of the original object (NoodleManager "] .noodlename NoodleManager [" Beef Ramen "] .tbname " / N "); // Show the NoodleName and TBName Console.Writeline of the Clone Object (Beefnoodle.NoodleName Beefnoodle.tbname "/ N");
// Add a new product to the prototype manager to use after cloning, below is defined a new noodle - mutton knife,
/ / And add it to the noodle manager, if there is another customer point, it can be cloned directly. NoodleManager ["Mutton Targe"] = New Cutnoodle ("Mutton Targe");
// Cloning a bowl of mutton knife noodle muttoncutnoodle = (Noodle) NoodleManager ["Mutton Targe"] .clone ();
Console.Writeline (NoodleManager ["Mutton Targe"] .NoodleName "/ N"); console.writeline (MuttonCutnoodle.NoodleName "/ N");
Console.readline ();}} // Abstract product-noodle // serialization attribute, use this attribute when each derived class can be used to achieve a deep copy [Serializable] public abstract class noodle {// Define a DataTable object, mainly to verify the deep copy and shallow copy when the comparative object is included in the comparative object,
/ / You can also use other references object protected datatable data = new data (); public string tbname {get {return datatable.tablename;} set {dataable.tablename = value;}}
// field protected string noodlename; // Property public string noicelename {get {noodlename;} set {noodlename = value;}}
Public Abstract Noodle Make (String Name); // Shallow Interface PUBLIC Abstract Noodle Clone (); // Deep Interface PUBLIC Abstract Noodle DeepClone ();
// Specific product, ramen [serializable] public class pullnoodle: noide {
Public pullnoodle (string name) {this.noodlename = name; this.tbname = name "Table"; "punlnoodle is name / n");}
// Implement shallow copy public override noodle clone () {return (noice) this.memberwiseclone ();
// Realize the process of deep copy, "flooded beef", first sequence the object to the memory stream, re-serialize, to obtain a deep clone public override noodle deepclone () {// Define memory MemoryStream MS = New MemoryStream ); // Define binary flow iformatter bf = new binaryformatter (); // Serialization BF.Serialize (MS, this); // Reset pointer to the starting position for reverse sequenced ms.Position = 0; / / Return to the anti-sequence of deep cloned object returnis (Noodle) BF.DSERIALIZE (MS);
}
Public Override Noodle Make (String Name) {Return New PullNoodle (Name);
}
Specific products // - noodle [Serializable] public class CutNoodle: Noodle {public CutNoodle (string name) {this.NoodleName = name; this.TbName = name "table"; Console.WriteLine ( "CutNoodle is made / n"); } // Implement the shallow public override noodle clone () {Return (Noodle) this.memberwiseclone ();}
public override Noodle Make (string name) {return new CutNoodle (name);} // achieve deep clone public override Noodle DeepClone () {MemoryStream ms = new MemoryStream (); IFormatter bf = new BinaryFormatter (); bf.Serialize (ms , this); ms.position = 0; return (noice) bf.deserialize (ms);
}
// Define prototypes to store prototype collections, here is HashTable Class NoodleManager {// Define HashTable Protected Hashtable NoodTable NoodTABLEHT = New HashTable (); protected noodle noide;
Public noodleManager () {
Add three basic prototype noodle = new pullnoodle ("Beef Rosk", noodle; noodle = new pullnoodle; noodle = new pullnoodle; noodleht.add ("Mutton ramen"; Noodleht.Add ", Noodle); Noodle = New Cutnoodle (" Beef Targe "); Noodleht.Add (" Beef Targe ", Noodle;
} // Indexer, used to add, access noodle object public noodle this [string key] {get {return (noicele) noiceleht [key];} set {noodleht.add (key, value);}}
}
}