-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem13.java
80 lines (67 loc) · 2.57 KB
/
Item13.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Arrays;
import java.util.EmptyStackException;
// Item13: Override clone judiciously
// The Clonable interface was intended as a mixin interface
// for classes to advertise that they permit cloning
// However, it lacks a `clone` method, and Object's
// clone method is protected
// Then, what does `Clonable` interface do?
// It decides how `clone` (protected) method in Object
// actually behaves
// SUMMARY
// Avoid implementing Cloneable.
// A better approach to object copying is to provide a copy constructor
// or copy factory
public class Item13 {
public static class Stack<T> implements Cloneable {
private Object[] elements;
private int size = 0;
private static final int DEFAULT_CAPACITY = 16;
public Stack() {
this.elements = new Object[DEFAULT_CAPACITY];
}
// Copy Constructor (a better approach)
public Stack(Stack<T> stack) {
this.elements = stack.elements.clone();
this.size = stack.size;
}
// Copy Factory Method (a better approach)
public static <T> Stack<T> newInstance(Stack<T> stack) {
return new Stack<T>(stack);
}
public void push(Object e) {
ensureCapacity();
elements[size++] = e;
}
public T pop() {
if (size == 0) {
throw new EmptyStackException();
}
T result = (T) elements[--size];
elements[size] = null; // remove obsolete reference (memory leak)
return result;
}
private void ensureCapacity() {
if (elements.length == size) {
elements = Arrays.copyOf(elements, 2 * elements.length + 1);
}
}
// If an object contains fields that refer to mutable
// objects, the simple clone implementation can be disastrous
// In effect, the `clone` method functions as a constructor.
// You must ensure that it does no harm to the original obejct
// and that is properly establishes invariants on the clone
// Public clone methods should omit the throws clause
// as methods that don’t throw checked exceptions are easier to use (Item 71)
@Override
public Stack<T> clone() {
try {
Stack<T> result = (Stack<T>) super.clone();
result.elements = elements.clone(); // MUST COPY INTERNALS
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
}