基本介绍
(1)迭代器模式是常用的设计模式,属于行为型模式
(2)如果我们的集合元素是用不同的方式实现的,有数组,还有java的集合类,或者还有其他方式,当客户端要遍历这些集合元素的时候就要使用多种遍历方式,而且还会暴露元素的内部结构,可以使用迭代器模式。
(3)迭代器模式,提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,即:不暴露其内部的结构
代码实现
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class IteratorMode { public static void main(String[] args) { List<FilmAndTelevision> FilmAndTelevisions=new ArrayList<>(); CartoonFilmAndTelevision cartoonFilmAndTelevision = new CartoonFilmAndTelevision(); cartoonFilmAndTelevision.addDepartment("鬼灭之刃","呼吸法的世界"); cartoonFilmAndTelevision.addDepartment("JOJO的奇妙冒险","JOJO,我不做人了!!!!"); MovieFilmAndTelevision movieFilmAndTelevision=new MovieFilmAndTelevision(); movieFilmAndTelevision.addDepartment("复仇者联盟","未知"); FilmAndTelevisions.add(cartoonFilmAndTelevision); FilmAndTelevisions.add(movieFilmAndTelevision); OutPutImpl outPut=new OutPutImpl(FilmAndTelevisions); outPut.printlnAll(); } } //影视接口 interface FilmAndTelevision { public String getName(); //增加的方法 public void addDepartment(String name, String desc); //返回一个迭代器,遍历 public Iterator createIterator(); } class Department { private String name; private String desc; public Department(String name, String desc) { super(); this.name = name; this.desc = desc; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } } //动漫迭代器 class CartoonIterator implements Iterator { Department[] departments; int index = 0; public CartoonIterator(Department[] departments) { this.departments = departments; } @Override public boolean hasNext() { if (index >= departments.length || departments[index] == null) { return false; } return true; } @Override public Object next() { Department department = departments[index]; index+=1; return department; } @Override public void remove() { } } //电影迭代器 class MovieIterator implements Iterator { List<Department> departments; int index = -1; public MovieIterator(List<Department> departments) { this.departments = departments; } @Override public boolean hasNext() { if (index >= departments.size() - 1) { return false; } index+=1; return true; } @Override public Object next() { return departments.get(index); } } //动漫实现类 class CartoonFilmAndTelevision implements FilmAndTelevision{ private static Department[] departments=new Department[5]; int numOfDepartment = 0 ;// 保存当前数组的对象个数 @Override public String getName() { return "动漫"; } @Override public void addDepartment(String name, String desc) { departments[numOfDepartment]=new Department(name,desc); numOfDepartment+=1; } @Override public Iterator createIterator() { return new CartoonIterator(departments); } } class MovieFilmAndTelevision implements FilmAndTelevision{ List<Department> departments=new ArrayList<>(); @Override public String getName() { return "电影"; } @Override public void addDepartment(String name, String desc) { departments.add(new Department(name, desc)); } @Override public Iterator createIterator() { return new MovieIterator(departments); } } //总体输出 class OutPutImpl{ List<FilmAndTelevision>FilmAndTelevisions; public OutPutImpl(List<FilmAndTelevision> colleges) { this.FilmAndTelevisions = colleges; } public void printlnAll(){ Iterator<FilmAndTelevision> iterator = FilmAndTelevisions.iterator(); System.out.println("--------影视分类--------"); while (iterator.hasNext()){ FilmAndTelevision next = iterator.next(); System.out.println(next.getName()+":"); println(next.createIterator()); } } public void println(Iterator iterator){ while (iterator.hasNext()){ Department next = (Department) iterator.next(); System.out.println(next.getName()+" :"+next.getDesc()); } } }
注意事项和细节
优点:
(1)提供了一个统一的方法遍历对象,客户不用再考虑聚合的类型,使用一种方法就可以遍历对象
(2)隐藏了聚合的内部结构,客户端要遍历聚合的时候只能取到迭代器,而不会知道聚合的具体组成
(3)提供了一种设计思想,就是一个类应该只有一个引起变化的原因(叫做单一责任原则)。在聚合类中,我们吧迭代器分开,就是要吧管理对象集合和遍历对象集合的责任分开,这样一来集合改变的话,只影响到聚合对象。而如果遍历方式改变的话,只影响到了迭代器。
(4)当要展示一组相似对象,或者遍历一组相同对象使用,适合使用迭代器模式
缺点:
每个聚合对象都要一个迭代器,会生成多个迭代器不好管理类