欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

迭代器模式

程序员文章站 2022-05-04 13:37:44
...

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

一旦实现迭代器,我们只需要一个循环,就可以多态地处理任何项的集合。

 

有两个系统:绩效查询系统,工资单查询系统。

绩效查询系统(PerformanceSystem),拥有公司所有的员工信息,使用数组实现。

工资查询系统(SalarySystem),拥有公司所有的员工信息,使用List实现。

 

我们必须使用两个循环遍历这两个系统的人员信息。

使用迭代器模式,使用一种方法就可以访问所有系统的人员信息,而且不需要关心各个系统的人员数据结构。

package com.ez.biz;

/**
 * @author 窗外赏雪(EZ编程网)
 */
public class Test {
	public static void main(String[] args) {
		PerformanceSystem ps = new PerformanceSystem();
		SalarySystem ss = new SalarySystem();
		Programmer programmer = new Programmer(ps, ss);
		System.out.println("======对照系统间的人员信息=====");
		programmer.comparePeople();
	}
}

 

package com.ez.biz;

import com.ez.ApplicationSystem;
import com.ez.Iterator;

/**
 * 程序员需要隔段时间检查下两个系统的人员数据是否一致。 程序员不需要知道系统是使用数组还是List来实现的,他只关心取到迭代器。
 * 
 * @author 窗外赏雪(EZ编程网)
 */
public class Programmer {
	private ApplicationSystem performanceSystem;
	private ApplicationSystem salarySystem;

	public Programmer(ApplicationSystem performanceSystem,
			ApplicationSystem salarySystem) {
		this.performanceSystem = performanceSystem;
		this.salarySystem = salarySystem;
	}

	/**
	 * 对照系统间的人员信息
	 * 实现迭代器,我们只需要一个循环,就可以多态地处理任何项的集合。
	 */
	public void comparePeople() {
		Iterator performanceIterator = performanceSystem.createIterator();
		Iterator salaryIterator = salarySystem.createIterator();
		System.out.println("绩效系统的人员列表");
		printPeople(performanceIterator);
		System.out.println("工资系统的人员列表");
		printPeople(salaryIterator);
	}

	private void printPeople(Iterator iterator) {
		while (iterator.hasNext()) {
			People people = (People) iterator.next();
			System.out.println(people.getName());
		}
	}
}

 

package com.ez;
/**
 * 使用迭代器系统接口
 * @author 窗外赏雪(EZ编程网)
 */
public interface ApplicationSystem {
	Iterator createIterator();
}

 

package com.ez.biz;

import com.ez.Iterator;
import com.ez.ApplicationSystem;
import com.ez.impl.ArrayIterator;
/**
 * 绩效系统
 * 实现创建迭代器,返回数组迭代器。
 * @author 窗外赏雪(EZ编程网)
 */
public class PerformanceSystem implements ApplicationSystem{
	static final int MAX_COUNT=10;
	private People[] peoples;
	
	public PerformanceSystem() {
		peoples=new People[MAX_COUNT];
		peoples[0]=new People("李四",19);
		peoples[1]=new People("王五",24);
		peoples[2]=new People("赵六",13);
		peoples[3]=new People("孙七",42);
	}
	
	public Iterator createIterator(){
		return new ArrayIterator(peoples);
	}
}

 

package com.ez.biz;

import java.util.ArrayList;
import java.util.List;

import com.ez.Iterator;
import com.ez.ApplicationSystem;
import com.ez.impl.ListIterator;
/**
 * 工资系统
 * 实现创建迭代器,返回List迭代器。
 * @author 窗外赏雪(EZ编程网)
 */
public class SalarySystem implements ApplicationSystem{
	private List<People> peoples;
	public SalarySystem() {
		peoples=new ArrayList<People>();
		peoples.add(new People("李四",19));
		peoples.add(new People("王五",24));
		peoples.add(new People("赵六",13));
		peoples.add(new People("孙七",42));
	}
	public Iterator createIterator(){
		return new ListIterator(peoples);
	}
}

 

package com.ez.biz;
/**
 * 
 * @author 窗外赏雪(EZ编程网)
 */
public class People {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public People(String name, int age) {
		this.name = name;
		this.age = age;
	}
}

 

package com.ez;
/**
 * 迭代器接口
 * @author 窗外赏雪(EZ编程网)
 */
public interface Iterator {
	/**
	 * 是否还有更多的元素
	 * @return
	 */
	boolean hasNext();
	/**
	 * 返回下一个元素
	 * @return
	 */
	Object next();
}

 

package com.ez.impl;

import com.ez.Iterator;
import com.ez.biz.People;
/**
 * 专门用于迭代数组类型的人员信息。
 * @author 窗外赏雪(EZ编程网)
 */
public class ArrayIterator implements Iterator {

	private People[] peoples;
	private int position = 0;

	public ArrayIterator(People[] peoples) {
		this.peoples = peoples;
	}

	@Override
	public boolean hasNext() {
		if (position >= peoples.length || peoples[position] == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Object next() {
		People people = peoples[position];
		position++;
		return people;
	}
}

 

package com.ez.impl;

import java.util.List;

import com.ez.Iterator;
import com.ez.biz.People;
/**
 * 专门用于迭代List类型的人员信息。
 * @author 窗外赏雪(EZ编程网)
 */
public class ListIterator implements Iterator {

	private List<People> peoples;
	private int position = 0;

	public ListIterator(List<People> peoples) {
		this.peoples = peoples;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		if (position >= peoples.size() || peoples.get(position) == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Object next() {
		People people = peoples.get(position);
		position++;
		return people;
	}
}

 

使用Java自带的Iterator接口,实现迭代器模式。

package com.ez.biz;

/**
 * @author 窗外赏雪(EZ编程网)
 */
public class Test {
	public static void main(String[] args) {
		PerformanceSystem ps = new PerformanceSystem();
		SalarySystem ss = new SalarySystem();
		AttendanceSystem as = new AttendanceSystem();
		Programmer programmer = new Programmer(ps, ss, as);
		System.out.println("======对照系统间的人员信息=====");
		programmer.comparePeople();
	}
}

 

package com.ez;

import java.util.Iterator;

/**
 * 使用迭代器系统接口
 * @author 窗外赏雪(EZ编程网)
 */
public interface ApplicationSystem {
	Iterator createIterator();
}

 

 

package com.ez.biz;

import java.util.Hashtable;
import java.util.Iterator;
import com.ez.ApplicationSystem;

/**
 * 考勤系统
 * Collection间接实现了一个返回迭代器的iterator()方法
 * @author 窗外赏雪(EZ编程网)
 */
public class AttendanceSystem implements ApplicationSystem{
	private Hashtable<String,People> peoples;
	public AttendanceSystem() {
		peoples=new Hashtable<String,People>();
		peoples.put("李四",new People("李四",19));
		peoples.put("王五",new People("王五",24));
		peoples.put("赵六",new People("赵六",13));
		peoples.put("孙七",new People("孙七",42));
	}
	public Iterator<People> createIterator(){
		return peoples.values().iterator();
	}
}

 

package com.ez.biz;

import java.util.Iterator;

import com.ez.ApplicationSystem;
import com.ez.impl.ArrayIterator;
/**
 * 绩效系统
 * 实现创建迭代器,返回数组迭代器。
 * @author 窗外赏雪(EZ编程网)
 */
public class PerformanceSystem implements ApplicationSystem{
	static final int MAX_COUNT=10;
	private People[] peoples;
	
	public PerformanceSystem() {
		peoples=new People[MAX_COUNT];
		peoples[0]=new People("李四",19);
		peoples[1]=new People("王五",24);
		peoples[2]=new People("赵六",13);
		peoples[3]=new People("孙七",42);
	}
	
	public Iterator createIterator(){
		return new ArrayIterator(peoples);
	}
}

 

package com.ez.biz;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ez.ApplicationSystem;
/**
 * 工资系统
 * ArrayList已经实现了一个返回迭代器的iterator()方法
 * @author 窗外赏雪(EZ编程网)
 */
public class SalarySystem implements ApplicationSystem{
	private List<People> peoples;
	public SalarySystem() {
		peoples=new ArrayList<People>();
		peoples.add(new People("李四",19));
		peoples.add(new People("王五",24));
		peoples.add(new People("赵六",13));
		peoples.add(new People("孙七",42));
	}
	public Iterator<People> createIterator(){
		return peoples.iterator();
	}
}

 

package com.ez.impl;

import java.util.Iterator;

import com.ez.biz.People;
/**
 * 专门用于迭代数组类型的人员信息,实现java.util的迭代器接口。
 * @author 窗外赏雪(EZ编程网)
 */
public class ArrayIterator implements Iterator {

	private People[] peoples;
	private int position = 0;

	public ArrayIterator(People[] peoples) {
		this.peoples = peoples;
	}

	@Override
	public boolean hasNext() {
		if (position >= peoples.length || peoples[position] == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Object next() {
		People people = peoples[position];
		position++;
		return people;
	}

	@Override
	public void remove() {
		
	}
}

 

package com.ez.biz;

import java.util.Iterator;

import com.ez.ApplicationSystem;

/**
 * 程序员需要隔段时间检查下两个系统的人员数据是否一致。 
 * 程序员不需要知道系统是使用数组还是List来实现的,他只关心取到迭代器。
 * @author 窗外赏雪(EZ编程网)
 */
public class Programmer {
	private ApplicationSystem performanceSystem;
	private ApplicationSystem salarySystem;
	private ApplicationSystem attendanceSystem;

	public Programmer(ApplicationSystem performanceSystem,
			ApplicationSystem salarySystem, ApplicationSystem attendanceSystem) {
		this.performanceSystem = performanceSystem;
		this.salarySystem = salarySystem;
		this.attendanceSystem = attendanceSystem;
	}

	/**
	 * 对照系统间的人员信息 实现迭代器,我们只需要一个循环,就可以多态地处理任何项的集合。
	 */
	public void comparePeople() {
		Iterator performanceIterator = performanceSystem.createIterator();
		Iterator salaryIterator = salarySystem.createIterator();
		Iterator attendanceIterator = attendanceSystem.createIterator();
		System.out.println("绩效系统的人员列表");
		printPeople(performanceIterator);
		System.out.println("工资系统的人员列表");
		printPeople(salaryIterator);
		System.out.println("考勤系统的人员列表");
		printPeople(attendanceIterator);
	}

	private void printPeople(Iterator iterator) {
		while (iterator.hasNext()) {
			People people = (People) iterator.next();
			System.out.println(people.getName());
		}
	}
}

 

Collection和Iterator的好处在于,每个Collection都知道如何创建自己的Iterator。例如,只要调用ArrayList上的iterator(),就可以返回一个具体的Iterator。

 

Java5 包含一种新形式的for语句,称为for/in。可以让你在一个集合或者一个数组中遍历,而不需要显式创建迭代器。

 

程序员调了三次createIterator(),printPeople(Iterator)。

我们可以把这些系统打包进一个List中,然后取到它的迭代器,遍历每个系统,这样,程序员代码就变得很简单,并且新增删除应用都不用修改代码。

 

优化后的程序员代码:

package com.ez.biz;

import java.util.Iterator;
import java.util.List;

import com.ez.ApplicationSystem;
/**
 * 程序员升级版
 * 我们把系统打包进一个List中,这样我们可以通过迭代器,遍历每个系统。
 * @author 窗外赏雪(EZ编程网)
 */
public class ProgrammerUpgrade {
	private List<ApplicationSystem> applicationSystems;

	public ProgrammerUpgrade(List<ApplicationSystem> applicationSystem) {
		this.applicationSystems=applicationSystem;
	}

	/**
	 * 对照系统间的人员信息 实现迭代器,我们只需要一个循环,就可以多态地处理任何项的集合。
	 */
	public void comparePeople() {
		Iterator systemIterator = applicationSystems.iterator();
		while(systemIterator.hasNext()){
			ApplicationSystem as=(ApplicationSystem)systemIterator.next();
			System.out.println("=============================");
			printPeople(as.createIterator());	//每个系统的迭代器
		}
	}

	private void printPeople(Iterator iterator) {
		while (iterator.hasNext()) {
			People people = (People) iterator.next();
			System.out.println(people.getName());
		}
	}
}

 

相关标签: 迭代器模式