Design Pattern-prototype Pattern

Source: Internet
Author: User

The prototype mode creates a prototype mode similar to creating another custom object from an object without having to know the details of the Creation. Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object. Structure chart
Implementation Code of the basic prototype mode <span style = "font-size: 18px;"> namespace prototype mode {class Program {static void Main (string [] args) {ConcretePrototype1 p1 = new ConcretePrototype1 ("I"); // you can get the new instance c1 ConcretePrototype1 c1 = (ConcretePrototype1) p1.Clone (); Console. writeLine ("Cloned: {0}", c1.Id); Console. read () ;}// Prototype class abstract class Prototype {private string id; public Prototype (string id) {th Is. id = id;} public string Id {get {return id;} // declare a Clone method in the abstract class public abstract Prototype Clone ();} // specific Prototype class ConcretePrototype1: Prototype {public ConcretePrototype1 (string id): base (id) {} public override Prototype Clone () {// create the child table of the current object return (Prototype) this. the MemberwiseClone () ;}}</span> MemberwiseClone () method is to create a new object, and then copy the non-static fields of the current object to the new object. If the field is of the value type, perform a step-by-step copy on the field. If the field is of reference type, the referenced object is copied but not referenced. Therefore, the original object and its copy reference the same object. For. net, Prototype is unnecessary for the Prototype abstract class, because cloning is too common, so. net provides the ICloneable interface in the System namespace, which is the only method to Clone (). In this way, you only need to implement this interface to complete the prototype mode. For example, if you copy a Resume and the Resume prototype is [csharp] <span style = "font-size: 18px;"> // Resume class Resume: ICloneable {private string name; private string sex; private string age; private string timeArea; private string company; public Resume (string name) {this. name = name;} // set the personal information public void SetPersonalInfo (string sex, string age) {this. sex = sex; this. age = age;} // set the work experience public void SetWorkExperience (string timeAr Ea, string company) {this. timeArea = timeArea; this. company = company;} // Display public void Display () {Console. writeLine ("{0} {1} {2}", name, sex, age); // Method for interface implementation, used to clone the object Console. writeLine ("working experience: {0} {1}", timeArea, company);} public object Clone () {return (Object) this. memberwiseClone () ;}}</span> In general, cloning is the best solution when the initialization information does not change, which hides the details of object creation, this greatly improves the performance. Instead of re-initializing the object, the system dynamically obtains the running state of the object. All variables of the replicated object contain the same value as the original object, and all references to other objects still point to the original object. That is to say, the shortest copy only copies the objects to be considered, rather than the objects referenced by it. Let's take a look at an example: copy a resume. Structure chart code implementation work experience class [csharp] <span style = "font-size: 18px;"> // work experience class WorkExperience {private string workDate; public string WorkDate {get {return workDate;} set {workDate = value ;}} private string company; public string Company {get {return company ;} set {company = value ;}}</span> Resume class [csharp] <span style = "font-size: 18px;"> // Resume class Resume: ICloneable {private string name; private st Ring sex; private string age; // reference the "work experience" Object private WorkExperience work; public Resume (string name) {this. name = name; // instantiate "work experience" work = new WorkExperience () ;}// set personal information public void SetPersonalInfo (string sex, string age) {this. sex = sex; this. age = age;} // set the work experience public void SetWorkExperience (string workDate, string company) {work. workDate = workDate; // when this method is called, the two attributes of the object are assigned work. comp Any = company;} // Display public void Display () {Console. writeLine ("{0} {1} {2}", name, sex, age); Console. writeLine ("work experience: {0} {1}", work. workDate, work. company);} public object Clone () {return (Object) this. memberwiseClone () ;}</span> client call code [csharp] <span style = "font-size: 18px; "> class Program {static void Main (string [] args) {Resume a = new Resume (" laruence ");. setPersonalInfo ("male", "29");. setWorkE Xperience ("1998-2000", "XX Company"); Resume B = (Resume). clone (); B. setWorkExperience ("1998-2006", "YY enterprise"); Resume c = (Resume). clone (); c. setPersonalInfo ("male", "24"); c. setWorkExperience ("1998-2003", "ZZ enterprise"); // both B and c are cloned from a, but when they both set "work experience, the expected result is three different displays.. display (); B. display (); c. display (); Console. read () ;}</span>: the three versions reference the last setting. This is because only the reference is copied, And the referenced object is not copied, no object was created during the cloning process. Now all three references point to the same work experience object. This is light replication. All the variables of the Copied object contain the same value as the original object, but all references to other objects still point to the original object. All variables of the replicated object in deep replication contain the same value as the original object, except those variables that reference other objects. Variables that reference other objects will point to the new objects that have been copied, instead of the original referenced objects. That is to say, the objects referenced by the objects to be copied are all copied. Take resume copy as an example. Structure Code Implementation Resume class [csharp] <span style = "font-size: 18px;"> // Resume class Resume: ICloneable {private string name; private string sex; private string age; private WorkExperience work; public Resume (string name) {this. name = name; work = new WorkExperience () ;}// provides a private constructor called by the Clone method, in order to clone the data of "work experience" // the difference between the private Resume (WorkExperience work) {this. work = (WorkExperience) work. clone ();} // Set the personal information public void Set PersonalInfo (string sex, string age) {this. sex = sex; this. age = age;} // set the work experience public void SetWorkExperience (string workDate, string company) {work. workDate = workDate; work. company = company;} // Display public void Display () {Console. writeLine ("{0} {1} {2}", name, sex, age); Console. writeLine ("work experience: {0} {1}", work. workDate, work. company);} // Clone public object Clone () {// call the private constructor to Clone the "work experience", // then Assign a value to the relevant fields of the Resume object, and finally return a deeply copied Resume object Resume obj = new Resume (this. work); obj. name = this. name; obj. sex = this. sex; obj. age = this. age; return obj ;}</span> working experience class [csharp] <span style = "font-size: 18px; "> // work experience class implementation Interface class WorkExperience: ICloneable {private string workDate; public string WorkDate {get {return workDate;} set {workDate = value ;}} private string company; public string Company {g Et {return company;} set {company = value;} public Object Clone () {// work experience class implements the Clone method return (Object) this. memberwiseClone () ;}</span> the running result is different. The objects referenced by a, B, and c are different. during replication, the result is changed to two and three. Deep replication points the variables of the referenced object to the Copied object instead of the original referenced object. Because deep replication or shallow replication is often involved in specific scenarios, for example, the DataSet object DataSet has the Clone () method, Copy () method, and Clone () method () the method is used to copy the DataSet structure, but does not copy the data of the DataSet. It implements the shortest copy of the prototype mode. The Copy () method not only copies the structure, but also copies the data. In fact, it achieves deep replication in the prototype mode.

Related Article

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.