设计模式(Design Pattern) - 行为型模式(Behavioral Pattern) - 迭代器模式(Iterator) - Java实现

设计模式(Design Pattern) - 行为型模式(Behavioral Pattern) - 迭代器模式(Iterator)
提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示。

一、说明:
1、Iterator 迭代器(接口);
2、Collection 聚合对象(接口);
3、MyIterator 迭代器.实现类;
4、MyCollection 聚合对象.实现类。

二、Java实现,代码如下:
1、Iterator

package com.java.designPattern.iterator;

/**
 * 迭代器 - 接口
 *
 */
public interface Iterator {

    /**
     * 前一个元素
     * @return
     */
    public Object previous();

    /**
     * 后一个元素
     * @return
     */
    public Object next();

    /**
     * 是否有下一个元素
     * @return
     */
    public boolean hasNext();

    /**
     * 获取第一个元素
     * @return
     */
    public Object first();

}

2、Collection

package com.java.designPattern.iterator;

/**
 * 聚合对象 - 接口
 *
 */
public interface Collection {

    /**
     * 获取迭代器
     * @return
     */
    public Iterator iterator();

    /**
     * 获取聚合对象的元素
     * @param i
     * @return
     */
    public Object get(int i);

    /**
     * 获取聚合对象大小
     * @return
     */
    public Integer size();

}

3、MyIterator

package com.java.designPattern.iterator;

/**
 * 迭代器.实现类
 *
 */
public class MyIterator implements Iterator {

    private Collection collection;
    private int pos = -1;

    public MyIterator(Collection collection) {
        this.collection = collection;
    }

    @Override
    public Object previous() {
        if (pos > 0) {
            pos--;
        }
        return collection.get(pos);
    }

    @Override
    public Object next() {
        if (pos < collection.size() - 1) {
            pos++;
        }
        return collection.get(pos);
    }

    @Override
    public boolean hasNext() {
        if (pos < collection.size() - 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Object first() {
        pos = 0;
        return collection.get(pos);
    }

}

4、MyCollection

package com.java.designPattern.iterator;

/**
 * 聚合对象.实现类
 *
 */
public class MyCollection implements Collection {

    public Object[] obj;

    public MyCollection(Object[] obj) {
        this.obj = obj;
    }

    @Override
    public Iterator iterator() {
        return new MyIterator(this);
    }

    @Override
    public Object get(int i) {
        return this.obj[i];
    }

    @Override
    public Integer size() {
        return obj.length;
    }

}

5、Test

package com.java.designPattern.iterator;

/**
 * 测试类
 *
 */
public class Test {

    public static void main(String[] args) {
        Object[] obj = { "A", "B", "C", "D", "E" };
        MyCollection col = new MyCollection(obj);
        Iterator it = col.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("--- --- ---");
        System.out.println(it.next());
        System.out.println(it.previous());
        System.out.println(it.first());
        System.out.println(it.previous());
    }

}

输出:
A
B
C
D
E
E
D
A
A

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值