Java设计模式之迭代器模式

Java设计模式之迭代器模式

1.定义

提供一种方法,顺序访问一个集合对象中的各个元素,而又不暴露该对象的内部表示

2. 类型

行为型

3. 适用场景

  1. 访问一个集合对象的内容而无需暴露它的内部表示
  2. 为遍历不同的集合结构提供一个统一的接口

4. 优点

  1. 分离了集合对象的遍历行为

5. 缺点

  1. 类的个数增加

6. 相关设计模式

迭代器模式和访问者模式

都是迭代的访问一个集合对象中的某一个元素,访问模式作用于操作上,迭代器模式作用于种类上

7. coding

7.1 课程类

public class Course {

    private String name;

    public Course(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

7.2 课程新增移除迭代接口

public interface ICourseAggree {


    void addCourse(Course course);

    void removeCourse(Course course);

    ICourseAggreeIterator getCourseAggreeIterato();
}

7.3 课程新增移除迭代接口实现类

public class CourseAggreeImpl implements ICourseAggree{

    private List list;

    public CourseAggreeImpl() {
    }

    public CourseAggreeImpl(List list) {
        this.list = list;
    }

    @Override
    public void addCourse(Course course) {
        list.add(course);
    }

    @Override
    public void removeCourse(Course course) {
        list.remove(course);
    }

    @Override
    public ICourseAggreeIterator getCourseAggreeIterato() {
        return new CourseIteratorImpl(list);
    }
}

7.4 课程迭代接口

public interface ICourseAggreeIterator {

    Course nextCourse();

    Boolean isNextCourse();
}

7.5 迭代实现

public class CourseIteratorImpl implements ICourseAggreeIterator {

    private List list;

    private int position;

    private Course course;
    public CourseIteratorImpl(List list) {
         this.list = list;
    }

    @Override
    public Course nextCourse() {

        System.out.println("返回课程位置: " + position);
        course = (Course)list.get(position);
        position++;
        return course;
    }

    @Override
    public Boolean isNextCourse() {
        if (position < list.size()) {
            return false;
        }
        return true;
    }
}

7.6 课程单元测试

public class Test {


    public static void main(String[] args) {
        Course course1 = new Course("Java电商一期");
        Course course2 = new Course("Java电商二期");
        Course course3 = new Course("Java设计模式精讲");
        Course course4 = new Course("Python课程");
        Course course5 = new Course("算法课程");
        Course course6 = new Course("前端课程");


        ICourseAggree courseAggregate = new CourseAggreeImpl(new ArrayList());

        courseAggregate.addCourse(course1);
        courseAggregate.addCourse(course2);
        courseAggregate.addCourse(course3);
        courseAggregate.addCourse(course4);
        courseAggregate.addCourse(course5);
        courseAggregate.addCourse(course6);

        System.out.println("-----课程列表-----");
        printCourses(courseAggregate);

        courseAggregate.removeCourse(course4);
        courseAggregate.removeCourse(course5);

        System.out.println("-----删除操作之后的课程列表-----");
        printCourses(courseAggregate);
    }


    public static void printCourses(ICourseAggree courseAggregate) {
        ICourseAggreeIterator courseAggreeIterato = courseAggregate.getCourseAggreeIterato();
        while (!courseAggreeIterato.isNextCourse()) {
            Course course = courseAggreeIterato.nextCourse();
            System.out.println(course.getName());
        }
    }
}

8. UML

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vI0HjcsC-1667494196593)(C:\Users\maido\AppData\Roaming\Typora\typora-user-images\image-20221103225448087.png)]

9. 源码解析

9.1 Iterator

9.1.1 来源

java.util.Iterator

9.1.2 实现 ArrayListt 内部类
    private class ListItr extends Itr implements ListIterator<E> {
        ListItr(int index) {
            super();
            cursor = index;
        }

        public boolean hasPrevious() {
            return cursor != 0;
        }

        public int nextIndex() {
            return cursor;
        }

        public int previousIndex() {
            return cursor - 1;
        }

        @SuppressWarnings("unchecked")
        public E previous() {
            checkForComodification();
            int i = cursor - 1;
            if (i < 0)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i;
            return (E) elementData[lastRet = i];
        }

        public void set(E e) {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.set(lastRet, e);
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        public void add(E e) {
            checkForComodification();

            try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值