• 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    7.2 课程新增移除迭代接口

    public interface ICourseAggree {
    
    
        void addCourse(Course course);
    
        void removeCourse(Course course);
    
        ICourseAggreeIterator getCourseAggreeIterato();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    7.4 课程迭代接口

    public interface ICourseAggreeIterator {
    
        Course nextCourse();
    
        Boolean isNextCourse();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    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());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    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();
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    springboot遇到的错误
    Java项目:SSM医药信息管理系统
    Java之各平台快递对接
    第七章 数学 6 AcWing 1593. 整数分解
    Spring 事务一些探讨
    Django定时任务Django-crontab的使用
    SpringMVC(一、快速入门)
    Maven
    【手写Nacos系列】上厕所的功夫,带你手写一个注册中心的服务注册功能
    设计模式(三)| 行为型模式(责任链模式、观察者模式等)
  • 原文地址:https://blog.csdn.net/hyzsuccess/article/details/127681225