Create a Defensive Copy in the Constructor

拟墨画扇 提交于 2019-12-24 01:53:35

问题


The following is just example code to explain the problem I have trouble understanding:

Lets say I have the following Professor class, note the public getters and setters:

    public class Professor
    {
        public string id {get; set; }
        public string firstName{get; set;}
        public string lastName {get; set;}

        public Professor(string ID, string firstName, string lastname)
        {
            this.id = ID;
            this.firstName = firstName;
            this.lastName = lastname;
        }


    }

and Course:

public class Course
{
    string courseCode {get; private set;}
    string courseTitle {get; private set;}
    Professor teacher {get; private set;}

    public Course(string courseCode, string courseTitle, Professor teacher)
    {
        this.courseCode = courseCode;
        this.courseTitle = courseTitle;

    }
}

How would I make a defensive copy of the Professor object in the Course class? The example provided here does it like this with the date object.

fDateOfDiscovery = new Date(aDateOfDiscovery.getTime());

Can the same be done with the professor object in the Course class?

Update:

Taking the answer that was provided this is what I've come up with, is it correct?

public class Professor
{
 public string id {get; set; }
 public string firstName{get; set;}
 public string lastName {get; set;}

 Professor(string ID, string firstName, string lastname)
  {
       this.id = ID;
       this.firstName = firstName;
       this.lastName = lastname;
  }

 //This method can be either static or not
 //Please note that i do not implement the ICloneable interface. There is discussion in the community it should be marked as obsolete because one can't tell if it's doing a shallow-copy or deep-copy
 public static Professor Clone(Professor original)
 {
   var clone = new Professor(original.id, original.firstName, original.lastName);
   return clone;
 }
}

//not a method, but a constructor for Course
public Course (string courseCode, string courseTitle, Professor teacher)
{
    this.courseCode = courseCode;
    this.courseTitle = courseTitle;
    this.teacher = Professor.Clone(teacher)

}

回答1:


You can Clone your professor instance.

Clone logic should be within Professor class.

You can then receive an already cloned professor instance in the Course constructor

public class Professor
{
 public string id {get; set; }
 public string firstName{get; set;}
 public string lastName {get; set;}

 Professor(string ID, string firstName, string lastname)
  {
       this.id = ID;
       this.firstName = firstName;
       this.lastName = lastname;
  }

 //This method can be either static or not
 //Please note that i do not implement the ICloneable interface. There is discussion in the community it should be marked as obsolete because one can't tell if it's doing a shallow-copy or deep-copy
 public static Professor Clone(Professor original)
 {
   var clone = new Professor(original.id, original.firstName, original.lastName);
   return clone;
 }
}

Then, when you invoke a new Course you'll do this

public Course AddCourse(string courseCode, string courseTitle, Professor original)
{
  var clonedProfessor = Professor.Clone(original);
  var course = new Course(courseCode, courseTitle, clonedProfessor);
  return course;
}


来源:https://stackoverflow.com/questions/35470863/create-a-defensive-copy-in-the-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!