iis5.1怎么新建网站,中国生意网,红酒网站建设方案,华升建设集团公司网站迭代器模式 一. 迭代器模式 1.1 定义 提供一种方法顺序访问一个集合对象中的各种元素#xff0c;而又不暴露该对象的内部表示.1.2 角色 抽象迭代器接口#xff08;Iterator#xff09;.具体迭代器#xff08;ConcreteIterator#xff09;.抽象聚合接口#xff08;Aggrega… 迭代器模式 一. 迭代器模式 1.1 定义 提供一种方法顺序访问一个集合对象中的各种元素而又不暴露该对象的内部表示.1.2 角色 抽象迭代器接口Iterator.具体迭代器ConcreteIterator.抽象聚合接口Aggregate.具体聚合ConcreteAggregate.二. 具体实现 1.1 创建抽象迭代器接口 public interface Iterator {Object next();boolean hasNext();} 1.2 创建抽象聚合接口 public interface Aggregate {Iterator iterator();} 1.3 创建具体聚合及具体迭代器 public class ConcreteAggregate implements Aggregate {Overridepublic Iterator iterator() {return new ConcreteIterator();}private class ConcreteIterator implements Iterator {Overridepublic Object next() {System.out.println(ConcreteIterator next ...);return null;}Overridepublic boolean hasNext() {System.out.println(ConcreteIterator hasNext ....);return true;}}} 1.4 调用 public static void main(String[] args) {Aggregate aggregate new ConcreteAggregate();Iterator iterator aggregate.iterator();if(iterator.hasNext()){iterator.next();}} 1.5 输出 ConcreteIterator hasNext ....ConcreteIterator next ...三. 优缺点 3.1 优点 简化了聚合类的接口.3.2 缺点 增加新的聚合类需要增加新的具体迭代器.四. 源码 https://github.com/Seasons20/DisignPattern.git END