`

设计模式(13)- 迭代器模式

阅读更多

迭代器模式

1.定义

        提供一种方法,顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示。

2.示例代码

         使用迭代器模式,遍历数组和链表两个聚合对象。

  

/*工资模型描述对象*/
public class PayModel{
    //员工姓名
    private String userName;
    //支付工资
    private double pay;
    public String getUserName(){
       return userName;
    }
    public void setUserName(String userName){
        this.userName = userName;
    }
    public double getPay(){
       return pay;
    }
    public void setPay(double pay){
        this.pay = pay;
    }
}

/*统一访问聚合的接口*/
public interface Iterator{
   public void first();
   public void next();
   public boolean isDone();
   public Object currentItem();
}

/*获取访问聚合对象的接口*/
pubic abstract class Aggregate{
    /*工厂方法,创建相应的迭代器对象接口*/
    pubic abstract Iterator createIterator();
}

   

/*子公司1的工资管理,内部通过array管理*/
public class SalaryManager extends Aggregate{
    //返回聚合对象迭代器
    public Iterator createIterator(){
       return new ArrayIteratorImpl(this);
    }
    public Object get(int index){
       Object retObj = null;
       if(index < pms.length){
          retObj = pms[index];
       }
       return retObj;
    }
    public int size(){
       return this.list.length;
    }    
    //聚合对象为数组 
    private PayModel[] pms = null;
    //获取工资列表
    public PayModel[] getPays(){
       return pms;
    }
    //计算工资
    public void calcSalary(){    
       PayModel pm1 = new PayModel();
       pm1.setPay(3800);
       pm1.setUserName("张三");
       PayModel pm2 = new PayModel();
       pm2.setPay(3600);
       pm2.setUserName("李四");  
       pms = new PayModel[2];
       pms[0] = pm1;
       pms[1] = pm2;
    }
}

/*用来实现数组访问的迭代接口*/
public class ArrayIteratorImpl implements Iterator{
    /*存放被迭代的聚合对象*/
    private SalaryManager aggregate = null;
    //当前记录的索引
    private int index = -1;
    public ArrayIteratorImpl(SalaryManager aggregate){
        this.aggregate = aggregate;
    }
    public void first(){
         index = 0;
    }
    public void next(){
       if(next < 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);
    }
}

/*子公司2的工资管理,内部通过list管理*/
public class PayManager extends Aggregate{
    //返回聚合对象迭代器
    public Iterator createIterator(){
       return new CollectionIteratorImpl(this);
    }
    public Object get(int index){
       Object retObj = null;
       if(index < this.list.size()){
          retObj = this.list.get(index);
       }
       return retObj;
    }
    public int size(){
       return this.list.size();
    }
    //聚合对象为java的集合对象
    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(3600);
        pm2.setUserName("李四");
        list.add(pm1);
        list.add(pm2);
    }
}
/*用来实现链表访问的迭代接口*/
public class CollectionIteratorImpl implements Iterator{
    /*存放被迭代的聚合对象*/
    private PayManager aggregate = null;
    //当前记录的索引
    private int index = -1;
    public CollectionIteratorImpl(PayManager aggregate){
        this.aggregate = aggregate;
    }
    public void first(){
         index = 0;
    }
    public void next(){
       if(next < 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[]){
       //访问子公司1的工资列表
       SalaryManager salaryManager = new SalaryManager();
       salaryManager.calcSalary();
       System.out.println("子公司1的工资列表:");
       test(salaryManager.createIterator());
       //访问子公司2的工资列表
       PayManager payManager = new PayManager();
       payManager.calcSalary();
       System.out.println("子公司2的工资列表:");
       test(payManager.createIterator());
    }
    /*通过迭代器访问*/
    private static void test(Iterator it){
       it.first();
       while(!it.isDone()){
          Object obj = it.currentItem();
          System.out.println("this ojb = " + obj);
          it.next();
       }
    }
}

 

3.实际应用

      迭代器模式的功能主要用于提供对集合对象的迭代访问,就是围绕这个“访问”做文章,延伸出很多功能,比如以不同的方式遍历集合对象、对同一个对象同时进行多个遍历、以不同的遍历策略来遍历集合,如是否需要过滤、多态迭代,上边的实例已经实现了多态迭代。

 

迭代器模式的本质:控制访问集合对象中的元素

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics