设计模式——迭代器模式

迭代器模式(Iterator Pattern):
1.又称为游标(Cursor)模式,是行为型设计模式之一;
2.源于对容器的访问,把遍历方法封装到容器中。
3.提供一种方法顺序访问一个容器对象中的各个元素,而又不需要暴露该对象的内部表示。

使用场景:遍历一个容器对象时,例如,乘火车排队检票,对所有乘客进行遍历。

票务系统:相当于一个容器集合Aggregate
车票:遍历对象Object
检票口:迭代器Iterator

迭代器模式.jpg

代码示例:

/**
 * @author apple
 * 
 * 容器接口
 * 
 * 添加/删除元素
 * 
 * 获取容器迭代器
 *
 */
public interface Aggregate {
    void addObject(T object);
    void deleteObject(T object);
    public Iterator iterator();
}

/**
 * @author apple
 * 
 * 迭代器接口
 *
 */
public interface Iterator {
    // 判断是否有下一个元素
    boolean hasNext();
    
    T first();
    
    // 返回当前位置的元素并将位置移至下一位
    T next();
}


/**
 * 
 * @author apple
 *
 * @param 
 * 
 * 具体集合类
 * 
 */

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

public class ConcreteAggregate  implements Aggregate {

    private List list=new ArrayList();
    
    @Override
    public void addObject(T object) {
        // TODO Auto-generated method stub
        list.add(object);
    }

    @Override
    public void deleteObject(T object) {
        // TODO Auto-generated method stub
        list.remove(object);
    }

    @Override
    public Iterator iterator() {
        // TODO Auto-generated method stub
        return  new ConcreteIterator(list);
        
    }

}

import java.util.List;

/**
 * 
 * @author apple
 *
 * @param 
 * 
 * 具体迭代器
 */

public class ConcreteIterator implements Iterator {
    
    private List list = null;
    
    private int index = -1; 
    
    
    public ConcreteIterator(List list) {
        super();
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        // TODO Auto-generated method stub
        if (list.size() -1 > index) {
            return true;
        } else {
            return false;   
        }
    }

    @Override
    public T first() {
        // TODO Auto-generated method stub
        index = 0;
        T ticket = list.get(index); 
        return ticket;
    }

    @Override
    public T next() {
        // TODO Auto-generated method stub
        T ticket  = null;
        if (this.hasNext()) {
            ticket = list.get(++index);
        }
        return ticket;
    }

}

/**
 * @author apple
 * 
 * 票
 *
 */
public class Ticket {
    // 火车班次
    String trainNo;
    
    String name;

    public Ticket(String trainNo, String name) {
        super();
        this.trainNo = trainNo;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Ticket [trainNo=" + trainNo + ", name=" + name + "]";
    }
}

/**
 * 
 * @author apple
 * 
 * 测试类
 *
 */
public class Client {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Aggregate ag=new ConcreteAggregate();
        ag.addObject(new Ticket("108次", "张"));
        ag.addObject(new Ticket("108次", "李"));
        ag.addObject(new Ticket("108次", "王"));
        ag.addObject(new Ticket("107次", "王"));
        ag.addObject(new Ticket("108次", "棒槌"));
    
        Iterator iterator = ag.iterator();
        // 遍历 火车票
        while(iterator.hasNext()) {
             Ticket ob = (Ticket) iterator.next(); 
             System.out.println(ob.toString()+"\t"); 
        }
//      Ticket ob = (Ticket) iterator.first();
//        System.out.println("\nFirst:"+ob.toString());
    }

}

你可能感兴趣的:(设计模式——迭代器模式)