Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cloning.  Goal: Create an identical, independent copy of an object  Override the method (every class inherits clone() from class Object) public Object.

Similar presentations


Presentation on theme: "Cloning.  Goal: Create an identical, independent copy of an object  Override the method (every class inherits clone() from class Object) public Object."— Presentation transcript:

1 Cloning

2  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 Cloning

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  Example class to illustrate Aliasing, Shallow Copy, Deep Copy: class Robot { private int model; private Location location; // the class methods } Cloning

6 Robot r1 = new Robot(123, 5, 6); Robot r2 = r1; Aliasing (not Cloning) model: 123 location: row: 5 col: 5 r1 r2 r2 is just another name for (another way to reference) the Robot r1.

7 model: 123 location: row: 5 col: 6 r1 r2 model: 123 location: 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();

8 model: 123 location: row: 5 col: 6 r1 r2 model: 123 location: 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(); row: 5 col: 6

9 Data Structures

10  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

11 Inheritance vs Composition

12  Implementation that uses Inheritance ? class Stack extends ArrayLits { 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)

13  Implementation that uses Composition ? class Stack { private ArrayLits 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)

14  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) Data Structures Implementation (Inheritance vs Composition)

15 Iterators, Enhanced for-loop

16  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) Iterators

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 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 implements Iterable {... Stack methods and data members... // required by Iterable interface – return an Iterator object Iterator iterator() { Iterator iter = new StackIterator (); return iter; } // internal class that manages the iteration though the Stack class StackIterator implements Iterator { 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 class Command { private Canvas canvas; private Shape shape; public Command(Canvas c, Shape s) { Canvas canvas = c; Shape shape = s; }..................... } “Rookie Mistakes”

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

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

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

24 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”

25 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”

26 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”

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

28 THE END sides face roll getFace getSides


Download ppt "Cloning.  Goal: Create an identical, independent copy of an object  Override the method (every class inherits clone() from class Object) public Object."

Similar presentations


Ads by Google