Design Patterns (13) - Iterator Pattern

iterator pattern

1. Definition

        Provides a way to sequentially access elements in an aggregate object without exposing the object's internal representation.

2. Sample code

         Use the iterator pattern to traverse both the array and linked list aggregate objects.

  

/* Salary model description object */
public class PayModel{
    //name of worker
    private String userName;
    //Payment of wages
    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;
    }
}

/* Unified access to the aggregated interface */
public interface Iterator{
   public void first();
   public void next();
   public boolean isDone();
   public Object currentItem();
}

/* Get the interface for accessing the aggregate object */
pubic abstract class Aggregate{
    /* Factory method, create the corresponding iterator object interface */
    pubic abstract Iterator createIterator();
}

   

/* Salary management of subsidiary 1, internally managed by array */
public class SalaryManager extends Aggregate{
    //return aggregate object iterator
    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;
    }    
    //The aggregate object is an array
    private PayModel[] pms = null;
    //get salary list
    public PayModel[] getPays(){
       return pms;
    }
    //calculate salary
    public void calcSalary(){    
       PayModel pm1 = new PayModel();
       pm1.setPay(3800);
       pm1.setUserName("Zhang San");
       PayModel pm2 = new PayModel();
       pm2.setPay(3600);
       pm2.setUserName("Li Si");  
       pms = new PayModel[2];
       pms[0] = pm1;
       pms[1] = pm2;
    }
}

/*Iterative interface used to implement array access*/
public class ArrayIteratorImpl implements Iterator{
    /* store the iterated aggregate object */
    private SalaryManager aggregate = null;
    // index of the current record
    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);
    }
}

/* Salary management of subsidiary 2, internal management through list */
public class PayManager extends Aggregate{
    //return aggregate object iterator
    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();
    }
    //The aggregate object is a collection object of java
    private List list = new ArrayList();
    //get salary list
    public List getPayList(){
        return list;
    }
    //calculate salary
    public void calcPay(){
        PayModel pm1 = new PayModel();
        pm1.setPay(3800);
        pm1.setUserName("Zhang San");
        PayModel pm2 = new PayModel();
        pm2.setPay(3600);
        pm2.setUserName("Li Si");
        list.add(pm1);
        list.add(pm2);
    }
}
/*Iterative interface used to implement linked list access*/
public class CollectionIteratorImpl implements Iterator{
    /* store the iterated aggregate object */
    private PayManager aggregate = null;
    // index of the current record
    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);
    }
}

   

/* Unified access client */
public class Client{
    public static void main(String args[]){
       //Access the salary list of subsidiary 1
       SalaryManager salaryManager = new SalaryManager();
       salaryManager.calcSalary();
       System.out.println("Salary list of subsidiary 1:");
       test(salaryManager.createIterator());
       //Access the salary list of subsidiary 2
       PayManager payManager = new PayManager();
       payManager.calcSalary();
       System.out.println("The salary list of subsidiary 2:");
       test(payManager.createIterator());
    }
    /* access via iterator */
    private static void test(Iterator it){
       it.first();
       while(!it.isDone()){
          Object obj = it.currentItem();
          System.out.println("this ojb = " + obj);
          it.next();
       }
    }
}

 

3. Practical application

      The function of the iterator mode is mainly used to provide iterative access to collection objects. It is to make a fuss about this "access" and extend many functions, such as traversing collection objects in different ways, performing multiple traversals on the same object at the same time, to Different traversal strategies are used to traverse the collection, such as whether to filter and polymorphic iteration. The above example has implemented polymorphic iteration.

 

The essence of the iterator pattern: controlling access to elements in a collection object

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326331179&siteId=291194637