码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之迭代器模式

时间:2018-07-23 14:55:30      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:迭代   str   ret   模式   code   是什么   rri   arraylist   style   

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

当需要访问一个聚集对象,而不管这些对象是什么都需要遍历的时候,就应该考虑使用迭代器模式。当需要对聚集有多种方式遍历时,可以考虑使用迭代器模式。

技术分享图片

Iterator迭代器抽象类:

public abstract class Iterator {
    public abstract Object first();
    public abstract Object next();
    public abstract boolean isDone();
    public abstract Object currentItem();
}

Aggregate聚集抽象类:

public abstract class Aggregate {
    public abstract Iterator createIterator();
}
public class ConcreteAggregate extends Aggregate{
    private List<Object> items = new ArrayList<Object>();

    @Override
    public Iterator createIterator() {
        // TODO Auto-generated method stub
        return new ConcreteIterator(this);
    }
    public int count(){
        return this.items.size();
    }
    public Object getItemByIndex(int index){
        return items.get(index);
    }
    public List<Object> getItems() {
        return items;
    }
    public void setItems(List<Object> items) {
        this.items = items;
    }
}
public class ConcreteIterator extends Iterator{
    private ConcreteAggregate aggregate;
    private int current = 0;
    public ConcreteIterator(ConcreteAggregate aggregate){
        this.aggregate = aggregate;
    }
    @Override
    public Object first() {
        // TODO Auto-generated method stub
        return aggregate.getItemByIndex(0);
    }
    @Override
    public Object next() {
        // TODO Auto-generated method stub
        Object ret = null;
        current++;
        if(current < aggregate.count()){
            ret = aggregate.getItemByIndex(current);
        }
        return ret;
    }
    @Override
    public boolean isDone() {
        // TODO Auto-generated method stub
        return current >= aggregate.count() ? true : false;
    }
    @Override
    public Object currentItem() {
        // TODO Auto-generated method stub
        return aggregate.getItemByIndex(current);
    }

}
public class ConcreteIteratorDesc extends Iterator{
    private ConcreteAggregate aggregate;
    private int current = 0;
    public ConcreteIteratorDesc(ConcreteAggregate aggregate){
        this.aggregate = aggregate;
        current = this.aggregate.count() -1;
    }
    @Override
    public Object first() {
        // TODO Auto-generated method stub
        return aggregate.getItemByIndex(aggregate.count() -1);
    }
    @Override
    public Object next() {
        // TODO Auto-generated method stub
        Object ret = null;
        current--;
        if(current > 0){
            ret = aggregate.getItemByIndex(current);
        }
        return ret;
    }
    @Override
    public boolean isDone() {
        // TODO Auto-generated method stub
        return current < 0 ? true : false;
    }
    @Override
    public Object currentItem() {
        // TODO Auto-generated method stub
        return aggregate.getItemByIndex(current);
    }

}
public class Test {
    public static void main(String[] args){
        Object[] strArr = new Object[]{"xiaohei","xiaobai","xx","sfedd"};
        List<Object> passenger = Arrays.asList(strArr);
        ConcreteAggregate ca = new ConcreteAggregate();
        ca.setItems(passenger);
//        Iterator iterator = ca.createIterator();
//        Iterator iterator = new ConcreteIterator(ca);
        Iterator iterator = new ConcreteIteratorDesc(ca);
        while(!iterator.isDone()){
            System.out.println(iterator.currentItem() + "请买票");
            iterator.next();
        }
    }
}

迭代器模式就是分离了聚合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可以让外部代码透明地访问集合内部的数据。

 

设计模式之迭代器模式

标签:迭代   str   ret   模式   code   是什么   rri   arraylist   style   

原文地址:https://www.cnblogs.com/shicaiyou/p/9353858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!