迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。
有这样的一个需求。一家集团公司在北京和上海分别有一家子公司,每家公司内部有自己的部门,且自己提供了打印部门的方法。其中一个子公司以数组来保存部门列表,另外一个以ArrayList来保存。现在需要打印所有的部门。
一、原始实现
1 2 3 4 5 6 7 8 9 10 11 12
| public class Dept { private String name;
public Dept(String name){ this.name= name; }
public String getName(){ return this.name; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class BJBranch { ArrayList<Dept> depts;
public BJBranch(){ depts = new ArrayList<Dept>(); depts.add(new Dept("北京-财务部")); depts.add(new Dept("北京-研发部")); depts.add(new Dept("北京-开发部")); }
public ArrayList<Dept> getDepts(){ return depts; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class SHBranch { Dept[] depts;
public SHBranch(){ depts = new Dept[3]; depts[0] = new Dept("上海-财务部"); depts[1] = new Dept("上海-研发部"); depts[2] = new Dept("上海-开发部"); }
public Dept[] getDepts(){ return depts; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class TestOld { public static void main(String[] args) { BJBranch BJ = new BJBranch(); ArrayList<Dept> BJDepts = BJ.getDepts(); SHBranch SH = new SHBranch(); Dept[] SHDepts = SH.getDepts(); for(int i=0;i<BJDepts.size();i++){ System.out.println(BJDepts.get(i).getName()); } for(int i=0;i<SHDepts.length;i++){ System.out.println(SHDepts[0].getName()); } } }
|
从上面的代码中可以看出,由于两家子公司的实现方式不一样,造成循环遍历时自能使用对应的方式来遍历,造成相当大的不便。
二、使用迭代器模式来解决问题
1 2 3 4 5
| public interface Iterator { boolean hasNext(); Object next(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class BJBranchIterator implements Iterator{ ArrayList<Dept> depts; int position = 0; public BJBranchIterator(ArrayList<Dept> depts){ this.depts = depts; } @Override public boolean hasNext() { if(position>=depts.size() || depts.get(position)==null){ return false; }else{ return true; } }
@Override public Object next() { Dept dept = depts.get(position); position = position + 1; return dept; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class BJBranch { ArrayList<Dept> depts;
public BJBranch(){ depts = new ArrayList<Dept>(); depts.add(new Dept("北京-财务部")); depts.add(new Dept("北京-研发部")); depts.add(new Dept("北京-开发部")); }
public Iterator createrIterator(){ return new BJBranchIterator(depts); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class SHBranchIterator implements Iterator{ Dept[] depts; int position = 0; public SHBranchIterator(Dept[] depts){ this.depts = depts; } @Override public boolean hasNext() { if(position>=depts.length|| depts[position]==null){ return false; }else{ return true; } }
@Override public Object next() { Dept dept = depts[position]; position = position + 1; return dept; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class SHBranch { Dept[] depts;
public SHBranch(){ depts = new Dept[3]; depts[0] = new Dept("上海-财务部"); depts[1] = new Dept("上海-研发部"); depts[2] = new Dept("上海-开发部"); }
public Iterator createrIterator(){ return new SHBranchIterator(depts); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class TestNew {
public static void main(String[] args) { Iterator BJ = new BJBranch().createrIterator(); Iterator SH = new SHBranch().createrIterator();
printDeptName(BJ); printDeptName(SH); }
private static void printDeptName(Iterator iterator){ while(iterator.hasNext()){ Dept dept = (Dept) iterator.next(); System.out.println(dept.getName()); } } }
|
从改造后的代码中可以看出,使用迭代器模式改造后,成功的屏蔽了北京与上海分公司遍历的差异。
在Java中使用Iterator去遍历ArrayList应该大部分人都知道,实际上这个Iterator就是迭代器模式的实现。如果不使用迭代器模式,可以试想一下HasTable、HashSet、HashMap等类型,每一个都得按对应的方式去循环遍历,相当的不方便。
上一篇:设计模式系列之九模板方法模式
下一篇:设计模式系列之十一组合模式
留言
欢迎交流想法。留言会通过 GitHub Issues 保存,首次使用需要登录 GitHub。