Java迭代器模式

面向对象中有一些聚合对象(即一组对象的组合结构,如Java集合、数组等),迭代器模式就是为了不暴露聚合对象的内部结构的情况下,提供顺序访问聚合对象的方式。

迭代器模式:提供一个方法去顺序防问一个集合中的元素,而又不暴露这些元素的内部表示

角色分析:

抽象迭代器

具体迭代器

抽象聚合类

具体聚合类

上代码:

这个是抽象的聚合对象

public interface MyContainer {

    void addElement(Object element);

    void removeElement(Object element);

    int getSize();

    MyIterator getIterator();
}

抽象的迭代器:

public interface MyIterator {

    boolean hasNext();

    Object next();

}

具体的聚合类和具体的迭代器,迭代器作为集合的内部类:

public class MyContianerImpl implements MyContainer {

    private Object [] elements;

    private int size = 0;

    private int currentSite = 0;


    MyContianerImpl(int initSize){
        elements = new Object[initSize];
    }

    @Override
    public void addElement(Object element) {
        if (elements.length <= currentSite){
            System.out.println("集合已经满了,您要加的元素没有加进去");
        }else {
            this.elements[currentSite] = element;
            this.currentSite++;
            this.size++;
        }
    }

    @Override
    public void removeElement(Object element) {
        int index = -1;
        for (int i = 0 ; i < size ; i++ ){
            Object item = this.elements[i];
            if (item.equals(element)){
                index = i;
                break;
            }
        }
        if (index != -1){
            for (int j = index; j < currentSite -1; j++){
                elements[j] = elements[j+1];
            }
            this.elements[currentSite-1] = null;
            currentSite--;
            size--;
        } else {
            System.out.println("集合中没有该元素,未删除任何元素");
        }

    }

    @Override
    public int getSize() {
        return this.size;
    }

    @Override
    public MyIterator getIterator() {
        return new MyIteratorImpl();
    }

    private class MyIteratorImpl implements MyIterator {

        int index = 0;

        @Override
        public boolean hasNext() {
            if (this.index < currentSite){
                return true;
            }
            return false;
        }

        @Override
        public Object next() {
            if (index < currentSite){
                return elements[index++];
            } else {
                throw new RuntimeException("集合越界了");
            }

        }
    }

}

测试代码:

public class ItteratorTest {

    public static void main(String[] args) {
        MyContainer container = new MyContianerImpl(8);

        container.addElement(0);
        container.addElement(1);
        container.addElement(2);
        container.addElement(3);
        container.addElement(4);
        container.addElement(5);
        container.addElement(6);
        container.addElement(7);

        container.addElement(8); // 这个元素加不进去

        MyIterator iterator = container.getIterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next());
        }
    }
}

测试结果:

集合已经满了,您要加的元素没有加进去
01234567

优点:

1. 解耦,在JAVA中的迭代模式其实用的是COLLECTION继承了Iterator这个类。给自己提供了一个遍历器。我们通过自己进行创建遍历器,可以很方便。
2. 遍历的算法可以通过自己来定义。还可以定义N多种算法进行遍历。
由于引入了抽象迭代器,所以我们进行扩展的时候会比较方便,不用修改源码。符合开闭原则。


缺点:

1. 当你进行分离一个类能解决的功能的时候,就势必会增加系统类的数量!
2. 设计抽象迭代器的时候比较困难,因为需要考虑以后的如何扩展,一旦修改了抽象迭代器,那么需要修改的类一大堆(不过可以定义一个新的抽象迭代器,再由新的具体迭代器去实现)。


使用场景:

1. 需要为一个集合,(聚合对象)提供N种不同的遍历方式的时候。
2. 为不同的聚合类,提供统一的遍历方法的接口的时候。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值