Design mode - prototype mode Reference "C # Technology" "Design Mode - Foundation for Object-Oriented Software" "Java and Mode" http://blog.9cbs.net/beautyispower/, a very good netizen Blog a lot There are menus in the restaurant, which lists which foods they offer, which kind of pasta, etc., the general Lanzhou Ramen Museum is such a beef railing (large bowl) beef ramen (small bowl) beef knife scrap lame lame lame lascat knife, You say that I want to eat my hand, I have to eat, I want to eat pearl jade abalone, he is afraid that there is no, these faces need to be customized, the beef rough, beef, beef knife, etc. can be seen as a prototype, the hood can look The prototype manager. The prototype mode is not directly created in the creation object, that is, not by the constructor of the external call class, but through the existing object instance cloned out, this cloned object and his source object have the same properties. And the state, that is, the state of the beef knife faces in the noodle is the same. The cloned object is divided into shallow copy and deep copy shallow copy is the object of cloning objects and its source object sharing references. For example, it may be inappropriate, assuming that beef knife is referred to an object: Money, indicating how much it is worth it, here The object is that it is system.object inherited (C #), which means that different shallow copy objects, their price is the same, after the price of the clone object has changed, the price of the object it is collectively passed It has changed, such as the price of the beef knife, and transferred 4 yuan by 5 dollars, 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 value type has its own copy, and 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 {///
NoodleManager ["Mutton Targe"] = New Cutnoodle ("Mutton Targe"); // Cloning a bowl of lamb knife noodle muttoncutnoodle = (Noodle) NoodleManager ["Mutton knife face"] .clone (); console.writeline (NoodleManager Knife surface "] .noodlename " / n "); console.writeline (MuttonCutnoodle.NoodleName " / n "); console.readline ();}} // Abstract product-noodle // serialization attribute, using a deep copy, Each derived class is plus this attribute to implement a deep copy [Serializable] public abstract class noodle {// Define a DataTable object, mainly to verify the deep copy and shallow copy of the comparable object, // you can also use any other referenced objects protected DataTable dataTable = new DataTable (); public string tbName {get {return dataTable.TableName;} set {dataTable.TableName = value;}} // fields protected string noodleName; / / properties public string noodleName {get {return noodleName;} set {noodleName = value;}} public abstract Noodle Make (string name); // shallow clone interface public abstract Noodle clone (); // a deep clone public abstract interface Noodle D eepClone ();} // specific products, noodles [Serializable] public class PullNoodle: Noodle {public PullNoodle (string name) {this.NoodleName = name; this.TbName = name "table"; Console.WriteLine ( "PullNoodle is made / N ");} // Implement a shallow copy public Override Noodle Clone () {Return (noide) this.memberwiseclone ();} // Realize the process of deep copy," flooded beef ", first sequence the object to the memory stream , Retrore sequencing, you can get DBPLIC Override Noodle DeepClone () {// Define Memorystream Ms = New MemoryStream ();
/ / Define binary flow iformatter bf = new binaryformatter (); // Serialization BF.Serialize (ms, this); // Reset pointer to the starting position for reverse sequence of MS.Position = 0; // Return Reverse sequenceful deep cloned object return (noodle);} public override noodle make (back name) {return new pullnoodle (name);}} // specific product-knife noodles [Serializable] public class cutnoodle: PUBLIC CUTNOODLE: Noodle {public cutnoodle (string name) {this.noodlename = name; this.tbname = Name "Table"; "CUTNODLE IS MADE / N");} // Implement 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 (NOODLE) BF.DSERIALIZE (MS);}} // Define prototype manager for storage prototypes, here is Hashtable Class NoodleManager {// Define HashTable Protected Hashtable Noodleht = New HashTable (); protected noodle noide; Public noodleManager () {// Add three basic prototype noodle = new pullnoodle ("Beef ramen"); Noodleht.Add ("Beef Ramen", Noodle; Noodle = New PullNoodle; Noodleht. Add ("Mutton Ramen", Noodle; Noodle = New Cutnoodle; Noodleht.Add ("Beef Targe", Noodle;} // Inderator for addition,