柳州 网站建设,中国全案设计联盟,商丘网站建设哪家专业,大宗交易平台有哪些迭代器模式是一种行为设计模式#xff0c;可以在不暴露底层实现(列表、栈或树等)的情况下#xff0c;遍历一个聚合对象中所有的元素。
Iterator is a behavior design pattern that can traverse all elements of an aggregate object
without exposing the internal imple…迭代器模式是一种行为设计模式可以在不暴露底层实现(列表、栈或树等)的情况下遍历一个聚合对象中所有的元素。
Iterator is a behavior design pattern that can traverse all elements of an aggregate object
without exposing the internal implementation (list, stack, tree, etc.).结构设计
迭代器模式包含如下角色 Iterator迭代器基类声明遍历聚合对象所需的操作获取下一个元素、获取当前位置、下一个元素是否存在等。 ConcreteIterator迭代器实现类实现遍历聚合对象的算法。 Aggregate聚合基类声明一个获取迭代器的接口。 ConcreteAggregate具体聚合实现类实现获取一个迭代器的接口并返回一个具体迭代器实例。 Client客户端通过聚合基类和迭代器基类的接口与两者进行交互。 迭代器模式类图表示如下
伪代码实现
接下来将使用代码介绍下迭代器模式的实现。
// 1、聚合接口声明一个获取迭代器的接口以及元素添加及删除接口
public interface IAggregate {IIterator createIterator();void append(Object element);void removeLast();
}//2、具体聚合实现类获取一个迭代器的接口并实现元素添加及删除接口这里使用数组存储元素
public class ConcreteAggregate implements IAggregate {private String[] elements;private int cursor -1;public ConcreteAggregate(int size) {elements new String[size];}Overridepublic void append(Object element) {cursor;elements[cursor] (String) element;}Overridepublic void removeLast() {elements[cursor] ;cursor--;}public int getCursor() {return this.cursor;}public String[] getElements() {return this.elements;}Overridepublic IIterator createIterator() {return new ConcreteIterator(this);}
}// 3、迭代器接口声明遍历聚合对象所需的操作获取下一个元素、获取当前位置、下一个元素是否存在等
public interface IIterator {Object first();Object next();boolean hasNext();Object currentItem();
}// 4、迭代器实现类实现遍历聚合对象的算法及其他已声明接口
public class ConcreteIterator implements IIterator {private ConcreteAggregate aggregate;private int index;public ConcreteIterator(ConcreteAggregate aggregate) {this.aggregate aggregate;this.index 0;}Overridepublic Object first() {String[] elements aggregate.getElements();return elements[0];}Overridepublic Object next() {int cursor aggregate.getCursor();if (cursor 0 || index cursor) {return null;}String[] elements aggregate.getElements();return elements[index];}Overridepublic boolean hasNext() {int cursor aggregate.getCursor();if (cursor 0 || index cursor) {return false;}return true;}Overridepublic Object currentItem() {int cursor aggregate.getCursor();if (cursor 0 || index cursor) {return null;}String[] elements aggregate.getElements();return elements[index];}
}// 5、客户端
public class IteratorClient {public void test() {// (1) 创建迭代对象实例IAggregate aggregate new ConcreteAggregate(10);// (2) 增删元素aggregate.append(hello);aggregate.append(world);aggregate.append(foo);aggregate.removeLast();// (3) 获取迭代器实例IIterator iterator aggregate.createIterator();// (4) 执行遍历操作while (iterator.hasNext()) {System.out.println(iterator.next());}}
}适用场景
在以下情况下可以考虑使用迭代器模式 (1) 当聚合对象的数据结构较复杂且希望对客户端隐藏其复杂性时(出于易用性或安全性考虑)可考虑使用迭代器模式。迭代器封装了与复杂数据结构进行交互的细节为客户端提供多个访问集合元素的简单方法。 这种方式不仅对客户端来说非常方便而且能避免客户端在直接与集合交互时执行错误或有害的操作从而起到保护集合的作用。 (2) 使用该模式可以减少程序中重复的遍历代码。重要迭代算法的代码往往体积非常庞大。当这些代码被放置在程序业务逻辑中时它会让原始代码的职责模糊不清降低其可维护性。因此 将遍历代码移到特定的迭代器中可使程序代码更加精炼和简洁。 (3) 如果希望代码能够遍历不同的甚至是无法预知的数据结构可考虑使用迭代器模式。该模式为集合和迭代器提供了一些通用接口。如果在代码中使用了这些接口那么将其他实现了这些接口的集合和迭代器传递给它时 它仍将可以正常运行。
优缺点
迭代器模式有以下优点 (1) 符合单一职责原则。通过将体积庞大的遍历算法代码抽取为独立的类可对客户端代码和集合进行整理。 (2) 符合开闭原则。可实现新型的集合和迭代器并将其传递给现有代码无需修改现有代码。 (3) 可以并行遍历同一集合因为每个迭代器对象都包含其自身的遍历状态。 但是该模式也存在以下缺点 (1) 如果只与简单的集合进行交互应用该模式可能会矫枉过正。 (2) 对于某些特殊集合使用迭代器可能比直接遍历的效率低。 (3) 增加了系统的复杂性。因为迭代器模式将存储数据和遍历数据的职责分离增加了新的聚合类需要对应增加新的迭代器类增加了系统的复杂性。
参考
《设计模式 可复用面向对象软件的基础》 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 著, 李英军, 马晓星等译 https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/mediator.html 迭代器模式 https://refactoringguru.cn/design-patterns/mediator 迭代器模式 https://www.runoob.com/design-pattern/mediator-pattern.html 迭代器模式 https://www.cnblogs.com/adamjwh/p/10959987.html 简说设计模式——迭代器模式 文章转载自: http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.hpggl.cn.gov.cn.hpggl.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.rpdmj.cn.gov.cn.rpdmj.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.tnbas.com.gov.cn.tnbas.com http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.khpx.cn.gov.cn.khpx.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.rszt.cn.gov.cn.rszt.cn http://www.morning.rdkt.cn.gov.cn.rdkt.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.cwqln.cn.gov.cn.cwqln.cn http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.qxrct.cn.gov.cn.qxrct.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.gjtdp.cn.gov.cn.gjtdp.cn http://www.morning.bpp999.com.gov.cn.bpp999.com http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn