设计模式-行为型-迭代器模式

描述

  • 提供一个对象来顺序访问聚合列表中的元素,不暴露聚合列表中对象的内部表示。
  • 遍历单例集合时,经常使用到(Collection.iterator)。

角色

  • 抽象聚合角色:存储,添加,删除聚合原始的抽象方法。
  • 具体聚合角色:集合,容器。实现抽象聚合角色,并返回具体迭代器实例。
  • 抽象迭代器角色:定义遍历聚合元素的接口,通常包括hasNest(),next()。
  • 具体迭代器角色:实现抽象迭代器角色,完成聚合对象的遍历。

实现

public class Test {
    public static void main(String[] args) {
        Peoples peoples = new Peoples();
        peoples.add(new People("阿椰", 25));
        peoples.add(new People("阿明", 22));
        peoples.add(new People("阿发", 23));
        MyIterator iterator = peoples.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

class People {
    private String name;
    private Integer age;

    People(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

// 抽象聚合角色
interface AbstractPeoples {
    void add(People people);

    void remove(People people);

    MyIterator iterator();
}

// 具体聚合角色
class Peoples implements AbstractPeoples {
    private final List<People> peopleList = new ArrayList<>();

    @Override
    public void add(People people) {
        peopleList.add(people);
    }

    @Override
    public void remove(People people) {
        peopleList.remove(people);
    }

    @Override
    public MyIterator iterator() {
        return new PeopleIterator(peopleList);
    }
}

// 抽象迭代器角色
interface MyIterator {
    boolean hasNext();

    People next();
}

// 具体迭代器角色
class PeopleIterator implements MyIterator {
    // 指针
    private int point = 0;
    private List<People> peopleList;

    PeopleIterator(List<People> peopleList) {
        this.peopleList = peopleList;
    }

    @Override
    public boolean hasNext() {
        return peopleList.size() > point;
    }

    @Override
    public People next() {
        return peopleList.get(point++);
    }
}

优点

  • 支持不同的方式(定义不同的具体迭代角色)遍历聚合对象。
  • 遍历方式和聚合角色松耦合,满足开闭原则(新增迭代器,不改原有代码)。

缺点

  • 增加类数据,也就增加了系统的复杂度。

使用场景

  • 当聚合对象需要切换或拥有多种遍历方式时,可以使用迭代器模式。
  • 当要为不同的聚合对象,提供一个统一的遍历方式时,可以使用迭代器模式。
  • 当遍历聚合对象,且要隐藏聚合对象中对象的细节时,可以使用迭代器模式。

JDK中的应用

  • 单列集合,如 List
  • 抽象聚合角色:List
  • 具体聚合角色:ArrayList,LinkedList等
  • 抽象迭代器角色:Iterator
  • 具体迭代器角色:list.iterator() =》 Ite类
    具体迭代器角色 Itr
    在这里插入图片描述

JAVA中迭代器模式的使用

  • 我们要使用迭代器模式的话,可以让我们定义的容器去实现java.util.Iterable接口,并重写iterator()方法,返回Iterable实现类即可。
  • 如下:
public class Test {
    public static void main(String[] args) {
        PeopleList peopleList = new PeopleList();
        peopleList.add(new People("阿椰", 25));
        peopleList.add(new People("阿明", 22));
        peopleList.add(new People("阿发", 23));
        Iterator iterator = peopleList.jdkIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

// 具体聚合角色
class PeopleList implements AbstractPeoples {
    private final List<People> peopleList = new ArrayList<>();

    @Override
    public void add(People people) {
        peopleList.add(people);
    }

    @Override
    public void remove(People people) {
        peopleList.remove(people);
    }

    @Override
    public MyIterator iterator() {
        return new PeopleIterator(peopleList);
    }

    public Iterator jdkIterator() {
        return new PeopleItr(peopleList);
    }

    private class PeopleItr implements Iterator<People> {
        // 指针
        private int point = 0;
        private List<People> peopleList;

        PeopleItr(List<People> peopleList) {
            this.peopleList = peopleList;
        }

        @Override
        public boolean hasNext() {
            return peopleList.size() > point;
        }

        @Override
        public People next() {
            return peopleList.get(point++);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值