Loading... # 设计模式(十七)——迭代器模式 ## 参考 > 大话设计模式  ——  程杰 著 ## 目录 [设计模式(一)——简单工厂模式](https://www.princelei.club/archives/67.html) [设计模式(二)——策略模式](https://www.princelei.club/archives/68.html) [设计模式(三)——设计原则](https://www.princelei.club/archives/116.html) [设计模式(四)——装饰模式](https://www.princelei.club/archives/117.html) [设计模式(五)——代理模式](https://www.princelei.club/archives/119.html) [设计模式(六)——工厂方法模式](https://www.princelei.club/archives/132.html) [设计模式(七)——原型模式](https://www.princelei.club/archives/133.html) [设计模式(八)——模板方法模式](https://www.princelei.club/archives/134.html) [设计模式(九)——外观模式](https://www.princelei.club/archives/135.html) [设计模式(十)——建造者模式](https://www.princelei.club/archives/136.html) [设计模式(十一)——观察者模式](https://www.princelei.club/archives/137.html) [设计模式(十二)——抽象工厂模式](https://www.princelei.club/archives/138.html) [设计模式(十三)——状态模式](https://www.princelei.club/archives/139.html) [设计模式(十四)——适配器模式](https://www.princelei.club/archives/140.html) [设计模式(十五)——备忘录模式](https://www.princelei.club/archives/141.html) [设计模式(十六)——组合模式](https://www.princelei.club/archives/147.html) [设计模式(十七)——迭代器模式](https://www.princelei.club/archives/148.html) [设计模式(十八)——单例模式](https://www.princelei.club/archives/157.html) [设计模式(十九)——桥接模式](https://www.princelei.club/archives/159.html) [设计模式(二十)——命令模式](https://www.princelei.club/archives/160.html) [设计模式(二十一)——职责链模式](https://www.princelei.club/archives/161.html) [设计模式(二十二)——中介者模式](https://www.princelei.club/archives/162.html) [设计模式(二十三)——享元模式](https://www.princelei.club/archives/163.html) [设计模式(二十四)——解释器模式](https://www.princelei.club/archives/173.html) [设计模式(二十五)——访问者模式](https://www.princelei.club/archives/174.html) ## 何为迭代器模式 - 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。 ~~现今来看迭代器模式使用价值远不如学习价值大了,MartinFlower甚至在自己的网站上提出撤销此模式。因为现在高级编程语言如C# 、JAVA等本身已经把这个模式做在语言中了。~~ ## 示例 实现迭代器 ``` /** * 实现迭代器 */ public class MyList<T> implements Iterable<T>, Collection<T> { private Object[] array; private int length; public MyList() { this.array = new Object[15]; this.length = 0; } @Override public int size() { return this.length; } @Override public boolean isEmpty() { return this.length == 0; } @Override public boolean contains(Object o) { if (o == null) { return false; } for (Object obj : array) { if (o.equals(obj)) { return true; } } return false; } @Override public Object[] toArray() { return Arrays.copyOf(this.array, this.length); } @Override public <T1> T1[] toArray(T1[] a) { for (int i = 0; i < a.length; i++) { a[i] = (T1) array[i]; } return a; } @Override public boolean add(T t) { if (array.length == this.length) { Object[] arr = new Object[this.length + 15]; System.arraycopy(array, 0, arr, 0, array.length); this.array = arr; } this.array[this.length] = t; this.length++; return true; } public T get(int i) { return (T) array[i]; } public boolean remove(int index) { if (index < 0 || index >= length) { return false; } for (int i = index; i < length; i++) { array[i] = array[i + 1]; } array[length - 1] = null; length--; return true; } @Override public boolean remove(Object o) { for (int i = 0; i < length; i++) { if (array[i].equals(o)) { return this.remove(i); } } return false; } @Override public boolean containsAll(Collection<?> c) { for (Object obj : c) { if (!this.contains(obj)) { return false; } } return true; } @Override public boolean addAll(Collection<? extends T> c) { for (Object obj : c) { this.add((T) obj); } return true; } @Override public boolean removeAll(Collection<?> c) { for (Object obj : c) { this.remove(obj); } return true; } @Override public boolean retainAll(Collection<?> c) { for (Object obj : c) { if (this.contains(obj)) { return true; } } return false; } @Override public void clear() { this.array = new Object[15]; this.length = 0; } @Override public Iterator<T> iterator() { return new MyIterator<>(); } class MyIterator<T> implements Iterator<T> { private int current = 0; private MyIterator() { } @Override public boolean hasNext() { if (current < length) { return true; } return false; } @Override public T next() { T t = (T) array[current]; current++; return t; } @Override public void remove() { MyList.this.remove(array[current - 1]); current--; } } @Override public String toString() { StringBuilder sb = new StringBuilder("["); for (int i = 0; i < length; i++) { sb.append(array[i]); if (i != length - 1) { sb.append(","); } } sb.append("]"); return sb.toString(); } } ``` 这里我实现了一个完整的集合类。 ``` /** * 客户端 */ public class MainClass { public static void main(String[] args) { MyList<String> myList = new MyList<>(); myList.add("张三"); myList.add("李四"); myList.add("王五"); for (String s : myList) { System.out.println(s); } } } ``` ## 何时使用迭代器模式 - 当你需要访问一个聚集对象,而且不管这些对象是什么都需要遍历的时候。 - 需要对聚集有多种方式遍历时。 - 为遍历不同的聚集结构提供如开始、下一个、是否结束、当前哪一项等统一的接口。 ## 迭代器模式优点 迭代器模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合内部结构,又可以让外部代码透明地访问集合内部的数据。 Last modification:June 11th, 2020 at 06:22 pm © 允许规范转载
这篇文章如同一首动人的乐章,触动了读者内心深处的柔软。
看的我热血沸腾啊https://www.237fa.com/
叼茂SEO.bfbikes.com
叼茂SEO.bfbikes.com