C # Design Pattern: Prototype mode (Prototype pattern)

Source: Internet
Author: User

One, prototype mode: By passing a prototype object to the object to be created, the object to be created will be created by requesting the prototype object to copy themselves.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Diagnostics;namespace_5. prototype Mode {//prototype mode: By passing a prototype object to the object to be created, the object to be created will be created by requesting the prototype object to copy themselves. //The main problem is the creation of "some complex objects", which often face drastic changes due to the change of requirements, but they have more stable and consistent interfaces.     classProgram { Public Static intMax_count =100000; Static voidMain (string[] args) {            //using the Clone program execution timeStopwatch st =NewStopwatch (); St.            Start (); Mail m=NewMail ("XXX company Send mail");  for(inti =0; i < Max_count; i++) {Mail m2=M.clone (); M2. Content= i +"Mr. (MS.)";            SendMail (m2); } St.            Stop (); Console.WriteLine ("To use the Clone program execution time:"+St.            Elapsed); //do not use the Clone program execution timeStopwatch St2 =NewStopwatch (); St2.            Start ();  for(inti =0; i < Max_count; i++) {Mail M3=NewMail ("XXX company Send mail"); M3. Content= i +"Mr. (MS.)";            SendMail (m3); } st2.            Stop (); Console.WriteLine ("do not use the Clone program execution time:"+St2.            Elapsed);        Console.readkey (); }         Public Static voidSendMail (Mail m) {//Console.WriteLine (M.title + m.content);        }    }     Public classMail {Private string_title;  PublicMail (stringtitle) {             This. _title =title;        ADD (); }         Public voidAdd () { for(inti =0; I < -; i++)            {                varA =i; }        }         Public stringTitle {Get{return_title;} }         Public stringContent {Get;Set; }  PublicMail Clone () {return(Mail) This.        MemberwiseClone (); }    }}

II. Results of implementation

Three, prototype mode: "Some complex objects" to create work; Because of the change of requirements, these objects often face drastic changes, but they have a relatively stable and consistent interface.

1 "complex object refers to excessive resource consumption when creating the object

2 "face drastic changes, such as e-mail, we need to send N, but the object of the n mail each person sends different information, so the strength of the object is not exactly the same

3 Stable interface It is worthwhile to send the object through the same method, not only that the calling method does not change, but that the object changes

Four, under what circumstances should the prototype mode be selected?

1 is the class initialization needs to digest a lot of resources, this resource includes data, hardware resources, and so on, as shown in the results, we are initializing to consume too much resources, which is to reflect the advantages of prototype mode

2 "is to create an object through new requires very tedious data preparation or access rights, you can use the prototype mode

Five, what does the prototype model of shallow cloning and deep cloning mean?

1 (Shallow copy): Copy all the fields from the original object to a new object, and if the field is a value type, simply copy a copy to the new object, and changing the value Type field of the new object does not affect the original object; If the field is a reference type, the reference is copied. Changing the value of the reference type field in the target object will affect the original object.

2 deep copy: Unlike shallow copy, where reference types are handled, deep copy points the reference type field in the new object to the new copied object, altering any object referenced in the new object, without affecting the contents of the corresponding field in the original object.

C # Design Pattern: Prototype mode (Prototype pattern)

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.