类型: 行为型模式
目的: 用于顺序访问集合对象的元素,使用者不需要知道集合对象的底层表示。
public interface Iterator {
public boolean hasNext();
public Object next();
}
public interface Container {
public Iterator getIterator();
}
public class NameRepository implements Container {
public String[] names = {"Robert" , "John" ,"Julie" , "Lora"};
@Override
public Iterator getIterator() {
return new NameIterator();
}
private class NameIterator implements Iterator {
int index;
@Override
public boolean hasNext() {
if(index < names.length){
return true;
}
return false;
}
@Override
public Object next() {
if(this.hasNext()){
return names[index++];
}
return null;
}
}
}
public class IteratorPatternDemo {
public static void main(String[] args) {
NameRepository namesRepository = new NameRepository();
for(Iterator iter = namesRepository.getIterator(); iter.hasNext();){
String name = (String)iter.next();
System.out.println("Name : " + name);
}
}
}
private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable {
@Override
public Iterator<E> iterator() {
return new ArrayItr<>(a);
}
}
private static class ArrayItr<E> implements Iterator<E> {
private int cursor;
private final E[] a;
ArrayItr(E[] a) {
this.a = a;
}
@Override
public boolean hasNext() {
return cursor < a.length;
}
@Override
public E next() {
int i = cursor;
if (i >= a.length) {
throw new NoSuchElementException();
}
cursor = i + 1;
return a[i];
}
}
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
private volatile List<String> beanDefinitionNames;
private volatile Set<String> manualSingletonNames;
public Iterator<String> getBeanNamesIterator() {
CompositeIterator<String> iterator = new CompositeIterator();
iterator.add(this.beanDefinitionNames.iterator());
iterator.add(this.manualSingletonNames.iterator());
return iterator;
}
}
迭代器CompositeIterator
public class CompositeIterator<E> implements Iterator<E> {
private final Set<Iterator<E>> iterators = new LinkedHashSet();
private boolean inUse = false;
public CompositeIterator() {
}
public void add(Iterator<E> iterator) {
Assert.state(!this.inUse, "You can no longer add iterators to a composite iterator that's already in use");
if (this.iterators.contains(iterator)) {
throw new IllegalArgumentException("You cannot add the same iterator twice");
} else {
this.iterators.add(iterator);
}
}
public boolean hasNext() {
this.inUse = true;
Iterator var1 = this.iterators.iterator();
Iterator iterator;
do {
if (!var1.hasNext()) {
return false;
}
iterator = (Iterator)var1.next();
} while(!iterator.hasNext());
return true;
}
public E next() {
this.inUse = true;
Iterator var1 = this.iterators.iterator();
Iterator iterator;
do {
if (!var1.hasNext()) {
throw new NoSuchElementException("All iterators exhausted");
}
iterator = (Iterator)var1.next();
} while(!iterator.hasNext());
return iterator.next();
}
public void remove() {
throw new UnsupportedOperationException("CompositeIterator does not support remove()");
}
}
创建型模式
结构型模式
行为型模式