CSC 216 F09/cloning1

From Expertiza_Wiki
Revision as of 15:37, 12 November 2009 by Twpensyl (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Shallow vs. Deep Cloning

Give the title of your exercise, which may include the name of the topic you are covering, or some other catchy title.

The problem

In Java, cloning an object creates a new object of the same class, copying each instance variable from the old object to the the new object. This sounds fairly straightforward, until you consider instance variables whose "values" are actually references to other, mutable objects. This creates the problem where modifying the clone can inadvertently modify the original object, or vice versa.

The solution to this problem is a recursive cloning function called a 'deep clone'. Instead of just copying values, this function calls itself on each value, excluding primitive data types.

Participants and props

The full exercise requires 5 students, 5 sheets of paper or index cards, and a writing utensil. Also a projector or white board to display the code to the class.

PacmanGhost (Original)
Name:         
Color:         
Location:            
PacmanGhost (Clone)
Name:         
Color:         
Location:            
PacmanGhost (Deep Clone)
Name:         
Color:         
Location:   
Point
Name:           
x:        
y:        
Point
Name:           
x:        
y:        


The script

  public class PacmanGhost {
     private Point location = new Point(0, 0);
     private String color = "Blue"
  

The instructor should begin by distributing the 5 sheets to the 5 students. At this point the students should write nothing but their first names on the sheets.

The instructor begins by "creating" a PacmanGhost. The Student with the PacmanGhost (Original) paper should step forward (Student A), and be instructed to write "Blue" for his color. For the location, a 'Point' student should step forward (Student B). Student A should tell Student B to use zero and zero as his 'x' and 'y' values. Because 'location' is a reference to the point object, Student A should then ask for Student B's first name, which Student A should write down as the value for 'location'.

  public PacmanGhost clone() {
     Student newStudent = new Student();
     newStudent.color = this.color;
     newStudent.location = this.location;
  }

The instructor should then announce he's creating a clone() of Student A, and call Student C (PacmanGhost (clone) ) forward. He should ask Student C to copy the values from Student A's card for location and color.

  public PacmanGhost move() {
     location.x += 100;
  }

The instructor should then call the move() method on Student C. Student C should use the name he has written down for 'location:' and ask that person (Student B) to increase their x value by 100. Student B should then change his value and walk a few steps towards the other side of the room, to indicate that his position has changed.

The instructor should then ask the ghosts (A and C) to move to their 'location' (which should both by at Student B). Point out that although the move() method was called on Student C, Student A's location also moved.

  public PacmanGhost deepClone() {
     Student newStudent = new Student();
     newStudent.color = this.color;
     newStudent.location.x = this.location.x;
     newStudent.location.y = this.location.y;
  }

The instructor should now announce he is creating a DeepClone using the above method, and Students D and E should step forward. This time, when the clone (Student D) is created, he should first 'create' his own Point (student E), giving the values zero and zero, and writing down Student E's first name for his location. He should then copy student A's color as before. But instead of copying the name Student A has written down for location, he should ask the point by that name (Student B) what their 'x' and 'y' values are, and tell Student E to change his values to those.

At this point, the instructor can call the move() method on each of the three 'ghosts', having the points move each time, and the ghosts follow the point written on their card. It should be observed that original ghost and the first clone are coupled together because they reference the same point, while the deep clone moves independently because it has it's own point.