Search code examples
c#defensive-copy

Create a Defensive Copy in the Constructor


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)

}

Solution

  • 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;
    }