Null Object
Example - Empty iterator instead of null
interface Iterator<T> {
boolean hasNext();
T next();
}
class IteratorImpl<T> implements Iterator {
@Override
public boolean hasNext() {
return true; // TODO: here would be checking if the collection has more items to iterate through
}
@Override
public T next() {
return null;
}
}
class NullIterator<T> implements Iterator {
@Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
return null;
}
}Last updated