Iterator Pattern

该博客介绍了迭代器模式在对象容器中的应用,通过InfoScienceIterator和ComputerIterator两个类实现了对Department对象的迭代。InfoScienceCollege和ComputerCollege分别提供了创建相应迭代器的方法,允许外部通过hasNext和next方法顺序访问容器中的所有元素。

Iterator pattern can ignore set object combination method, always use hasNext method to see if there's continues object and use next method to get the following object, it gives a more standard way to list all the objects.

Each object container we create one iterator class to implement Iterator interface, fulfill hasNext and next methods,

/**
 * @Author Carter Deng
 * @Date 2021/11/21
 */
public class InfoScienceIterator implements Iterator {

    List<Department> departmentList;
    int position;

    public InfoScienceIterator(List<Department> departmentList) {
        this.departmentList = departmentList;
        this.position = 0;
    }

    @Override
    public boolean hasNext() {
        if (departmentList.size() > position + 1) {
            return true;
        }
        return false;
    }

    @Override
    public Object next() {
        if (hasNext()) {
            this.position++;
            return departmentList.get(position);
        }
        return null;
    }
}



/**
 * @Author Carter Deng
 * @Date 2021/11/21
 */
public class ComputerIterator implements Iterator {

    Department[] departments;
    int position;

    public ComputerIterator(Department[] departments) {
        this.departments = departments;
        this.position = 0;
    }

    @Override
    public boolean hasNext() {
        if (this.departments != null) {
            if (this.departments.length - 1 > position) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Department next() {
        if (hasNext()) {
            this.position++;
            return departments[position];
        }
        return null;
    }
}

Object container provides one method to return its corssponding iterator object,

/**
 * @Author Carter Deng
 * @Date 2021/11/21
 */
public class InfoScienceCollege implements College {

    @Override
    public Iterator createIterator() {
        return new InfoScienceIterator(departments);
    }

}


/**
 * @Author Carter Deng
 * @Date 2021/11/21
 */
public class ComputerCollege implements College {

    @Override
    public Iterator createIterator() {
        return new ComputerIterator(departments);
    }

}

When we want to list down all items in object container, we can ask object to return iterator object and use hasNext/Next methods.

/**
 * @Author Carter Deng
 * @Date 2021/11/22
 */
public class Test {

    public static void main(String[] args) {
        ComputerCollege computerCollege = new ComputerCollege();
        InfoScienceCollege infoScienceCollege = new InfoScienceCollege();

        Iterator computerCollegeIterator = computerCollege.createIterator();
        Iterator infoScienceCollegeIterator = infoScienceCollege.createIterator();

        while(computerCollegeIterator.hasNext()){
            System.out.println(computerCollegeIterator.next());
        }

        System.out.println("=============================");

        while(infoScienceCollegeIterator.hasNext()){
            System.out.println(infoScienceCollegeIterator.next());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值