Vector類:
爲了更好的從宏觀上理解Vector,我們可以首先看一下Vector的父類以及實現的一些Interface:
Vector作爲一種容器,實現了Collection這個接口,並繼承AbstractList這個“直接父類”。同時,我們還能看到,Vector和他的父類AbstractList都實現了List這個接口,表明Vector是一種List類型的容器(其他的有比如Map型)。
綜上,用集合的思想來看Vector的邏輯地位:Collection > List > Vector ,Vector是所有容器的一條分支。
由UML圖,可以看到Vector的這個類在設計時的邏輯構架,作爲一個菜鳥,除了五花八門的算法,這種高級的面向對象的設計思想,是初學者更應該去把握的。
下面來Trace Code:Interface Collection:
Collection是所有容器的根,定義了了Collection應該去實現的方法Method,見下圖:
public abstract class AbstractCollection implements Collection {
protected AbstractCollection() {
super();
}
public boolean add(E object) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection extends E> collection) {
boolean result = false;
Iterator extends E> it = collection.iterator();
while (it.hasNext()) {
if (add(it.next())) {
result = true;
}
}
return result;
}
public void clear() {
Iterator it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
public boolean contains(Object object) {
Iterator it = iterator();
if (object != null) {
while (it.hasNext()) {
if (object.equals(it.next())) {
return true;
}
}
} else {
while (it.hasNext()) {
if (it.next() == null) {
return true;
}
}
}
return false;
}
public boolean containsAll(Collection> collection) {
Iterator> it = collection.iterator();
while (it.hasNext()) {
if (!contains(it.next())) {
return false;
}
}
return true;
}
public boolean isEmpty() {
return size() == 0;
}
public abstract Iterator iterator();
public boolean remove(Object object) {
Iterator> it = iterator();
if (object != null) {
while (it.hasNext()) {
if (object.equals(it.next())) {
it.remove();
return true;
}
}
} else {
while (it.hasNext()) {
if (it.next() == null) {
it.remove();
return true;
}
}
}
return false;
}
public boolean removeAll(Collection> collection) {
boolean result = false;
Iterator> it = iterator();
while (it.hasNext()) {
if (collection.contains(it.next())) {
it.remove();
result = true;
}
}
return result;
}
public boolean retainAll(Collection> collection) {
boolean result = false;
Iterator> it = iterator();
while (it.hasNext()) {
if (!collection.contains(it.next())) {
it.remove();
result = true;
}
}
return result;
}
public abstract int size();
public Object[] toArray() {
int size = size(), index = 0;
Iterator> it = iterator();
Object[] array = new Object[size];
while (index T[] toArray(T[] contents) {
int size = size(), index = 0;
if (size > contents.length) {
Class> ct = contents.getClass().getComponentType();
contents = (T[]) Array.newInstance(ct, size);
}
for (E entry : this) {
contents[index++] = (T) entry;
}
if (index it = iterator();
while (it.hasNext()) {
Object next = it.next();
if (next != this) {
buffer.append(next);
} else {
buffer.append("(this Collection)"); //$NON-NLS-1$
}
if (it.hasNext()) {
buffer.append(", "); //$NON-NLS-1$
}
}
buffer.append(']');
return buffer.toString();
}
}
//AbstractCollection 抽象的實現了Collection這個接口的所有方法,沒有存在任何具體的Method,因爲所有遍歷過程都依賴於自身的迭代器。
舉個例子:
public void clear() {
Iterator it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
照着設計者的意思,加黑這句代碼iterator()指望着子類去實現了,所有的框架都給你搭好了,子類或者任何具體的容器要去實現這個迭代器,更進一步說,具體容器的數據結構與迭代器緊密相關,文鄒鄒說起來這叫耦合度高。在這裏,一切都是個模子。小總結:
1、方法卻是一個抽象的方法,顯然設計者的目的是讓所有的ConcreteCollection(具體Collection)去繼承抽象的Collection。
2、容器的結構與迭代方式都因自身的種類而不同。
困死了,待續。。。。