1 / 28

Cloning

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()

halej
Download Presentation

Cloning

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cloning

  2. 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

  3. 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; } } }

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

  5. Cloning • Example class to illustrate Aliasing, Shallow Copy, Deep Copy: class Robot { private int model; private Location location; // the class methods }

  6. 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.

  7. 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

  8. 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

  9. Data Structures

  10. 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)

  11. Inheritance vs Composition

  12. 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

  13. 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();} }

  14. 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)

  15. Iterators, Enhancedfor-loop

  16. 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)

  17. 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); • }

  18. 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} }

  19. The “Rookie Mistakes”

  20. “Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { Canvas canvas = c; Shape shape = s; } ... ... ... ... ... ... ... }

  21. “Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { Canvas canvas = c; Shape shape = s; } ... ... ... ... ... ... ... }

  22. “Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { c = canvas; s = shape; } ... ... ... ... ... ... ... }

  23. “Rookie Mistakes” class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { c = canvas; canvas = c; s = shape; shape = s; } ... ... ... ... ... ... ... }

  24. “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; ... ... ... ... ... ... ... }

  25. “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; ... ... ... ... ... ... ... }

  26. “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; } ... ... ... ... ... ... ... }

  27. “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; } ... ... ... ... ... ... ... }

  28. roll sides face getSides getFace THE END

More Related