On some occasions, we need shallow copy to solve the problem, because we copy the instance, still refer to the original initial object. But sometimes, this is not enough, because we copy the instance, but also to the reference type to make local value modification adjustment, and guarantee can not affect the initial object!
This requires a deep copy!
The requirement is this:
First look at why shallow copy doesn't meet our requirements: We want to copy the CV, and the copied version is only inconsistent with the company name in the job intent of the template resume.
Our first version of the code is this:
CV Model 1.0 Version
public class Resumeinfo1:icloneable {public ResumeInfo1 (string name, String telephone, Educationinfo Ed Ucationinfo,wantedjob1 job) {this._name = name; This._telephone = telephone; This._educationinfo = Educationinfo; This._wantedjob = job; } private string _name; public string Name {get {return this._name;} } private string _telephone; public string Telephone {get {return _telephone;} } private Educationinfo _educationinfo; Public Educationinfo Educationinfo {get {return _educationinfo;} } private WantedJob1 _wantedjob; Public WantedJob1 Wantedjob {get {return _wantedjob;} } public Object Clone () {return this. MemberwiseClone (); } }
Inside nested sub-category educational background object Educationinfo
public class Educationinfo {public string Schoolname {get; set;} Public DateTime enrolltime {get; set;} Public DateTime leavetime {get; set;} }
There are also nested object WantedJob1:
public class WantedJob1 {public string CompanyName {get; set;} Public double eanrings {get; set;} }
We use it under the client:
Educationinfo educationinfo = new Educationinfo (); WantedJob1 wantedjob = new WantedJob1 (); ResumeInfo1 templateresume = new ResumeInfo1 ("Qaz", "18810002000", Educationinfo, wantedjob); Educationinfo.enrolltime = new DateTime (7, 1); Educationinfo.leavetime = new DateTime (1, 1); Educationinfo.schoolname = "WSX"; Wantedjob1.companyname = "EDC"; Wantedjob1.eanrings = +; It is assumed that each CV version has only CompanyName attribute values. var res1 = Templateresume.clone (); (Res1 as ResumeInfo1). Wantedjob.companyname = "Baidu"; var res2 = Templateresume.clone (); (Res1 as ResumeInfo1). Wantedjob.companyname = "Ali";
But we got the company name "Ali."
This is a typical shallow copy!
Can not meet the company name to differentiate the requirements, continue to modify, into a deep copy:
public class Resumeinfo2:icloneable {public ResumeInfo2 (string name, String telephone, Educationinfo Ed Ucationinfo,wantedjob2 job) {this._name = name; This._telephone = telephone; This._educationinfo = Educationinfo; This._wantedjob = job; }//private void Clonejob (WantedJob2 job) {this._wantedjob = job. Clone () as WANTEDJOB2; } private string _name; public string Name {get {return this._name;} } private string _telephone; public string Telephone {get {return _telephone;} } private Educationinfo _educationinfo; Public Educationinfo Educationinfo {get {return _educationinfo;} } private WantedJob2 _wantedjob; Public WantedJob2 Wantedjob {get {return _wantedjob;} public Object Clone () {clonejob (this._wantedjob); return new ResumeInfo2 (_name,_telephone,_educationinfo,_wantedjob); } }
Job Seekers Target 2.0:
WANTEDJOB2 Implement Interface Public class wantedjob2:icloneable {public string CompanyName {get; set;} Public double eanrings {get; set;} public Object Clone () { return this. MemberwiseClone (); } }
Client calls:
Here we need to do deep copy processing for wantedjob. educationinfo educationinfo = new Educationinfo (); WantedJob2 wantedjob = new WantedJob2 (); ResumeInfo2 templateresume = new ResumeInfo2 ("Qaz", "18810002000", Educationinfo, wantedjob); Educationinfo.enrolltime = new DateTime (7, 1); Educationinfo.leavetime = new DateTime (1, 1); Educationinfo.schoolname = "WSX"; Wantedjob.companyname = "EDC"; Wantedjob.eanrings = +; It is assumed that each CV version has only CompanyName attribute values. var res1 = Templateresume.clone (); (Res1 as ResumeInfo2). Wantedjob.companyname = "Baidu"; var res2 = Templateresume.clone (); (Res2 as ResumeInfo2). Wantedjob.companyname = "Ali";
Get a different company name! Deep copy success!