CSC 216 F09/cloning1: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 11: Line 11:
===Participants and props===
===Participants and props===


The full exercise requires 4 students with cell phones, 4 sheets of paper or index cards, and a writing utensil.
The full exercise requires 4 students with cell phones, 4 sheets of paper or index cards, and a writing utensil. Also a projector or white board to display the code to the class.
 
{| border="1" cellspacing="0" cellpadding="5" align="center"
!
PacmanGhost (Original)
Color:   
Location:       
!
PacmanGhost (Clone)
Color:   
Location:       
|-
!
Point
Name:         
x:       
y:       
!
Point
Name:         
x:       
y:       
|}
 


===The script===
===The script===


Describe how to do your exercise.
  public class PacmanGhost {
      private Point location = new Point(0, 0);
      private String color = "Blue"
 
  public PacmanGhost clone() {
      Student newStudent = new Student();
      newStudent.color = this.color;
      newStudent.location = this.location;
  }
 
The instructor begins by "creating" a PacmanGhost, by giving one of the students the paper
 
  public PacmanGhost deepClone() {
      Student newStudent = new Student();
      newStudent.color = this.color;
      newStudent.location.x = this.location.x;
  }

Revision as of 15:02, 12 November 2009

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 4 students with cell phones, 4 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)
Color:     
Location:        
PacmanGhost (Clone)
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"
  public PacmanGhost clone() {
     Student newStudent = new Student();
     newStudent.color = this.color;
     newStudent.location = this.location;
  }
  

The instructor begins by "creating" a PacmanGhost, by giving one of the students the paper

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