设计模式入门之迭代器模式Iterator

迭代器模式定义:提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部实现

迭代器模式的结构和说明


Iterator::迭代器接口。定义访问和遍历元素的接口

ConcreteIterator:具体的迭代器实现对象。实现对聚合对象的遍历,并跟踪遍历时的当前位置

Aggregate:聚合对象。定义创建相应迭代器对象的接口

ConcreteAggregate:具体聚合对象。实现创建相应的迭代器对象

实例:一个公司,工资列表是用List实现的,后收购一家公司,工资列表是用Array实现的,现在需要开发一种统一的方式来访问两种不同的聚合对象,上代码

//工资描述模型对象
public class PayModel {
	private String userName;
	private double pay;
	//get and set methods
}
//获取访问聚合接口的接口
public interface Aggregate {
	public Iterator createIterator();
	public int size();
	public Object get(int index);
}
//已有的工资管理对象
public class PayManager implements Aggregate{
	public Iterator createIterator() {
		return new AgIteratorImpl((Aggregate)this);
	}
	private List list = new ArrayList();
	public List getPayList() {
		return list;
	}
	public void calcPay() {
		PayModel pm1 = new PayModel();
		pm1.setPay(3800);
		pm1.setUserName("张三");
		PayModel pm2 = new PayModel();
		pm2.setPay(5800);
		pm2.setUserName("李四");
		list.add(pm1);
		list.add(pm2);
	}
	public Object get(int index) {
		Object obj = null;
		if(index < this.list.size()) {
			obj = this.list.get(index);
		}
		return obj;
	}
	public int size() {
		return this.list.size();
	}
}
//被收购放的工资管理类
public class SalaryManager implements Aggregate {
	public Iterator createIterator() {
		return new AgIteratorImpl((Aggregate)this);
	}
	private PayModel[] pms = null;
	public PayModel[] getPays() {
		return pms;
	}
	public void calcSalary() {
		PayModel pm1 = new PayModel();
		pm1.setPay(2800);
		pm1.setUserName("王五");
		PayModel pm2 = new PayModel();
		pm2.setPay(6800);
		pm2.setUserName("赵六");
		pms = new PayModel[2];
		pms[0] = pm1;
		pms[1] = pm2;
	}
	public Object get(int index) {
		Object obj = null;
		if(index < this.pms.length) {
			obj = this.pms[index];
		}
		return obj;
	}
	public int size() {
		return this.pms.length;
	}
}
//统一访问聚合对象的接口
public interface Iterator {
	public void first();
	public void next();
	public boolean isDone();
	public Object currentItem();
}
//访问数组的统一迭代接口
public class AgIteratorImpl implements Iterator{
	private Aggregate aggregate = null;
	private int index = -1;
	public AgIteratorImpl(Aggregate aggregate) {
		this.aggregate = aggregate;
	}
	public void first() {
		index = 0;
	}
	public void next() {
		if(index < this.aggregate.size()) {
			index = index + 1;
		}
	}
	public boolean isDone() {
		if(index == this.aggregate.size()) {
			return true;
		}
		return false;
	}
	public Object currentItem() {
		return this.aggregate.get(index);
	}
}
//客户端调用
public class Client {
	public static void main(String[] args) {
		PayManager payManager = new PayManager();
		payManager.calcPay();
		System.out.println("集团工资列表:");
		test(payManager.createIterator());

		SalaryManager salaryManager = new SalaryManager();
		salaryManager.calcSalary();
		System.out.println("新收购公司工资列表:");
		test(salaryManager.createIterator());
	}
	private static void test(Iterator it) {
		it.first();
		while(!it.isDone()) {
			Object obj = it.currentItem();
			System.out.println("the obj="+obj);
			it.next();
		}
	}
}
//以上并没有使用Java自带的Iterator,这样可以更好地理解


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值