一、迭代器模式
類(lèi)型: 行為型模式
目的: 用于順序訪問(wèn)集合對(duì)象的元素,使用者不需要知道集合對(duì)象的底層表示。
二、例子
2.1 菜鳥(niǎo)例子
2.1.1 定義迭代器接口
public interface Iterator {
public boolean hasNext();
public Object next();
}
2.1.2 定義迭代對(duì)象接口——用于返回一個(gè)迭代器
public interface Container {
public Iterator getIterator();
}
2.1.3 實(shí)現(xiàn) 迭代對(duì)象 和 迭代器
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;
}
}
}
2.1.4 使用
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);
}
}
}
2.2 JDK源碼——ArrayList
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];
}
}
2.3 Spring源碼——DefaultListableBeanFactory
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()");
}
}
三、其他設(shè)計(jì)模式
創(chuàng)建型模式
結(jié)構(gòu)型模式文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-744530.html
- 1、設(shè)計(jì)模式——裝飾器模式(Decorator Pattern)+ Spring相關(guān)源碼
行為型模式文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-744530.html
- 1、設(shè)計(jì)模式——訪問(wèn)者模式(Visitor Pattern)+ Spring相關(guān)源碼
- 2、設(shè)計(jì)模式——中介者模式(Mediator Pattern)+ JDK相關(guān)源碼
- 3、設(shè)計(jì)模式——策略模式(Strategy Pattern)+ Spring相關(guān)源碼
- 4、設(shè)計(jì)模式——狀態(tài)模式(State Pattern)
- 5、設(shè)計(jì)模式——命令模式(Command Pattern)+ Spring相關(guān)源碼
- 6、設(shè)計(jì)模式——觀察者模式(Observer Pattern)+ Spring相關(guān)源碼
- 7、設(shè)計(jì)模式——備忘錄模式(Memento Pattern)
- 8、設(shè)計(jì)模式——模板方法模式(Template Pattern)+ Spring相關(guān)源碼
- 9、設(shè)計(jì)模式——迭代器模式(Iterator Pattern)+ Spring相關(guān)源碼
- 10、設(shè)計(jì)模式——責(zé)任鏈模式(Chain of Responsibility Pattern)+ Spring相關(guān)源碼
- 11、設(shè)計(jì)模式——解釋器模式(Interpreter Pattern)+ Spring相關(guān)源碼
到了這里,關(guān)于設(shè)計(jì)模式——迭代器模式(Iterator Pattern)+ Spring相關(guān)源碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!