280 likes | 308 Views
Cloning. Cloning. Goal: Create an identical, independent copy of an object Override the method (e very class inherits clone() from class Object ) public Object clone() General Setup: 1. State the the class implements Cloneable 2. Implement the method public Object clone()
E N D
Cloning • Goal: Create an identical, independent copy of an object • Override the method (every class inherits clone()from class Object) public Object clone() • General Setup: 1. State the the class implements Cloneable 2. Implement the method public Object clone() a) create a copy by calling the parent’s clone method b) clone the data members c) return the copy
class MyClass implements Cloneable { ... data members of MyClass ... public Object clone() { try { MyClass copy = (MyClass) super.clone(); ... copy/clone the data members ... return copy } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } }
Clone and Constructor Similarity • class Explorer extends Robot • { • private int points; • private Location target; • public Explorer(int m, int r, int c) • { • super(m, r, c); • points = 0; • target = new Location(random(), random()); • } • public Object clone() • { • Explorer copy = (Explorer) super.clone(); copy.points = this.points; • copy.target = (Location) this.target.clone(); • return copy; • } • }
Cloning • Example class to illustrate Aliasing, Shallow Copy, Deep Copy: class Robot { private int model; private Location location; // the class methods }
Aliasing (not Cloning) Robot r1 = new Robot(123, 5, 6); Robot r2 = r1; r1 model: 123 r2 location: row: 5 col: 5 r2 is just another name for (another way to reference) the Robot r1.
Shallow Copy(did not clone the data members) • public Object clone() • { • Robot copy = (Robot) super.clone(); • copy.model = this.model; • copy.location = this.location; • return copy; • } • Robot r1 = new Robot(123, 5, 6); • Robot r2 = (Robot) r1.clone(); r1 r2 model: 123 model: 123 location: location: row: 5 col: 6
Deep Copy(cloned the data members) • public Object clone() • { • Robot copy = (Robot) super.clone(); • copy.model = this.model; • copy.location = (Location) this.location.clone(); • return copy; • } • Robot r1 = new Robot(123, 5, 6); • Robot r2 = (Robot) r1.clone(); r1 r2 row: 5 col: 6 model: 123 model: 123 location: location: row: 5 col: 6
Data Structures • Special purpose structures used for organizing and managing data • Provide controlled access to the stored data • Tradeoffs in efficiency in storing and accessing the data (CS III) • Examples: ArrayList-- one-dimensional “array” MyGrid -- two-dimensional “array” (hw 5) Stack -- last in, first out (LIFO) Queue -- first in, first out (FIFO)
Data Structures Implementation(Inheritance vs Composition) • Implementation that usesInheritance ? class Stack<E> extends ArrayLits<E> { public E pop() { return remove(size()-1); } public E peek() { return get(size()-1); } public void push(E elmnt) { add(elmnt); } } • The methods clear(), empty(), size() come for free (inherited) • Unfortunately, Stack acquires other methods that affect Stack integrity
Data Structures Implementation(Inheritance vs Composition) • Implementation that usesComposition ? class Stack<E> { private ArrayLits<E> items; public E pop() { return items.remove(size()-1); } public E peek() { return items.get(size()-1); } public void push(E elmnt) { items.add(elmnt); } public int size() { return items.size(); } public void clear() { items.clear(); } public boolean empty() { return items.isEmpty();} }
Data Structures Implementation(Inheritance vs Composition) • Composition should be used ! • A bit more work – have to implement clear(), empty(), size() • However, Stack integrity is preserved, i.e. only the methods required by Stack are available to the user (with Inheritance user got extra methods that can “damage” the Stack)
Iterators • Iterators generalize the concept of traversing a collection • Uni-direction Iterator has the following interface boolean hasNext() -- are there more elements to traverse E next() -- return next element in collection void remove() -- remove last returned item (optional)
Using Iterators • See reference files Stack.java and IteratorTester.java for Assignment 12 • // option 1: standard CS I traversal • for (int i = 0; i < numbers.size(); i++) { • Double value = numbers.get(i); • System.out.println("1.processing: " + value); • } • // option 2: extract an iterator object • Iterator<Double> iter = numbers.iterator(); • while( iter.hasNext() ) { • Double value = iter.next(); • System.out.println("2.processing: " + value); • } • // option 3: a convenient form for option 2 • for (Double value : numbers) { • System.out.println("3.processing: " + value); • }
Implementing Iterators class Stack<E> implements Iterable<E> { ... Stack methods and data members ... // required by Iterable interface – return an Iterator object Iterator<E> iterator() { Iterator<E> iter = new StackIterator<E>(); return iter; } // internal class that manages the iteration though the Stack class StackIterator<E> implements Iterator<E> { private int index; // point to current element public StackIterator() { initialize the iterator } public boolean hasNext() { check if index in range } public E next() { return current element; update index } public void remove() { throw exception if no action on remove} }
“Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { Canvas canvas = c; Shape shape = s; } ... ... ... ... ... ... ... }
“Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { Canvas canvas = c; Shape shape = s; } ... ... ... ... ... ... ... }
“Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { c = canvas; s = shape; } ... ... ... ... ... ... ... }
“Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { c = canvas; canvas = c; s = shape; shape = s; } ... ... ... ... ... ... ... }
“Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; ... ... ... ... ... ... ... } class MoveCommand extends Command { private Canvas canvas; private Shape shape; private int dx; private int dy; ... ... ... ... ... ... ... }
“Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; ... ... ... ... ... ... ... } class MoveCommand extends Command { private Canvas canvas; private Shape shape; private int dx; private int dy; ... ... ... ... ... ... ... }
“Rookie Mistakes” class MoveCommand extends Command { private int dx; private int dy; public MoveCommand(Canvas c, Shape s, ind dx, int dy) { super(c, s); dx = dx; dy = dy; } ... ... ... ... ... ... ... }
“Rookie Mistakes” class MoveCommand extends Command { private int dx; private int dy; public MoveCommand(Canvas c, Shape s, ind dx, int dy) { super(c, s); this.dx = dx; this.dy = dy; } ... ... ... ... ... ... ... }
roll sides face getSides getFace THE END