1. Prototype mode
Use the prototype instance to specify an instance of the created object, and create a new object by copying the prototypes.
* Prototype mode hides the details of creating objects and improves performance.
* Shallow copy: All variables of the copied object contain the same value as the original object, and all references to other objects point to the original object.
* Deep copy: The reference to the copied object points to the new object, not the original referenced object.
*. NET provides the only method under the Iconeable interface clone can easily complete the prototype mode implementation.
2. Example:
namespaceprototype Mode _ shallow copy {classProgram {Static voidMain (string[] args) {Resume a=NewResume ("Big Bird"); A.setpersonalinfo ("male"," at"); A.setworkexperience (" -","XX Company"); Resume b=(Resume) A.clone (); B.setworkexperience ("male","yy company"); A.display (); B.display ();//a shallow copy makes the referenced copy point to the same space, and A's work experience is covered by BConsole.read (); } } classWorkexperience {Private stringworkdate; Public stringWorkdata {Get{returnworkdate;} Set{workdate =value;} } Private stringCompany ; Public stringCompany {Get{returnCompany ;} Set{company =value;} } } classresume:icloneable {Private stringname; Private stringsex; Private stringAge ; Privateworkexperience work; PublicResume (stringname) { This. Name =name; work=Newworkexperience (); } Public voidSetpersonalinfo (stringSexstringAge ) { This. Sex =sex; This. Age =Age ; } Public voidSetworkexperience (stringWorkdata,stringCompany ) {work. Workdata=Workdata; Work.company=Company ; } Public voidDisplay () {Console.WriteLine ("{0} {1} {2}", name, sex, age); Console.WriteLine ("Shallow copy: {0} {1}", work. Workdata, Work.company); } Public ObjectClone () {return(Object) This. MemberwiseClone (); } }}
At this point the output, a, and all details are identical.
namespacePrototype Mode _ deep copy {classProgram {Static voidMain (string[] args) {Resume a=NewResume ("Big Bird"); A.setpersonalinfo ("male"," at"); A.setworkexperience (" -","XX Company"); Resume b=(Resume) A.clone (); B.setworkexperience ("male","yy company"); A.display (); B.display ();//a shallow copy makes the referenced copy point to the same space, and A's work experience is covered by BConsole.read (); } } classworkexperience:icloneable {Private stringworkdate; Public stringWorkdata {Get{returnworkdate;} Set{workdate =value;} } Private stringCompany ; Public stringCompany {Get{returnCompany ;} Set{company =value;} } PublicObject Clone () {return(Object) This. MemberwiseClone (); } } classresume:icloneable {Private stringname; Private stringsex; Private stringAge ; Privateworkexperience work; PublicResume (stringname) { This. Name =name; work=Newworkexperience (); } PublicResume (workexperience work) { This. Work =(workexperience) work. Clone (); } Public voidSetpersonalinfo (stringSexstringAge ) { This. Sex =sex; This. Age =Age ; } Public voidSetworkexperience (stringWorkdata,stringCompany ) {work. Workdata=Workdata; Work.company=Company ; } Public voidDisplay () {Console.WriteLine ("{0} {1} {2}", name, sex, age); Console.WriteLine ("Deepreplication: {0} {1}", work. Workdata, Work.company); } Public ObjectClone () {Resume obj=NewResume ( This. Work); Obj.name= This. Name; Obj.sex= This. Sex; Obj.age= This. Age; returnobj; } }}
b in modifying the attributes in work experience is not affected in a.
(C #) prototype mode-deep copy and shallow copy