2

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 Answers1

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;
}
Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
  • Now, any changes that are made to the teacher object outside of the class would not reflect on the course object correct? The Course object is truly immutable? –  Feb 18 '16 at 01:24
  • You must think on these objects as pointers (references). When you clone a professor you have two different professors which happen to have the same property values. If you then change one professor the other will not change – Luis Filipe Feb 18 '16 at 01:30
  • @Sebas i agree and will remove it – Luis Filipe Feb 18 '16 at 01:52
  • The lazy in me says just serialize the `Professor` instance, save that as a memento inside the `Course`, and rehydrate it whenever a disaster occurs. Your choice of json or xml... – code4life Feb 18 '16 at 03:25
  • Instead of having an AddCourse method in course, could I call Professor.Clone from inside the Course Constructor? Why did you remove the other method of creating a defensive copy? –  Feb 18 '16 at 13:05
  • @Spaceman Not sure i completely understood your doubt. AddCourse method should be located in a separate class, namely a business class – Luis Filipe Feb 18 '16 at 15:00
  • @Spaceman I removed the other method because it is the wrong way of doing it. The classes themselves should be the ones to know how to clone themselves – Luis Filipe Feb 18 '16 at 15:01
  • Many thanks for your time/help. Bare with, I'll update my code to show what I mean. –  Feb 18 '16 at 15:15