• 主页
  • 随笔
所有文章 友链 关于我

  • 主页
  • 随笔

设计模式系列之十迭代器模式

2016-12-20

迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

  有这样的一个需求。一家集团公司在北京和上海分别有一家子公司,每家公司内部有自己的部门,且自己提供了打印部门的方法。其中一个子公司以数组来保存部门列表,另外一个以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) {
// TODO Auto-generated method stub
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 ArrayList<Dept> getDepts(){
return depts;
}*/

//改造为返回Iterator对象,而不是ArrayList
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 Dept[] getDepts(){
return depts;
}*/

//改造为返回Iterator对象,而不是数组
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等类型,每一个都得按对应的方式去循环遍历,相当的不方便。

上一篇:设计模式系列之九模板方法模式
下一篇:设计模式系列之十一组合模式

赏

谢谢你请我吃糖果

  • 设计模式

扫一扫,分享到微信

微信分享二维码
Linux下搭建Maven私服Nexus3.X
坚持一年背英语单词的总结
© 2025 YiYing
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • 读书笔记
  • sql注入
  • sqlmap
  • nexus
  • HTTP安全
  • restfull
  • 随笔
  • 哲学
  • 缓存
  • HTTP状态码
  • HTTP连接
  • RSA
  • Java
  • JavaScript
  • 安全
  • 排序算法
  • POI
  • 工具类
  • 工作思考
  • Life
  • 读书
  • 前端
  • 团队管理

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 友情链接1
  • 友情链接2
  • 友情链接3
  • 友情链接4
  • 友情链接5
  • 友情链接6
很惭愧

只做了一点微小的工作
谢谢大家