• 每天一个设计模式之迭代器模式(Iterator Pattern)


    一、UML类图

    在这里插入图片描述

    二、代码示例

    Container角色

    public interface BookCollection extends Iterable<Book> {
        Iterator<Book> iterator();
    }
    
    
    • 1
    • 2
    • 3
    • 4

    Container具体类

    public class BookLibrary implements BookCollection {
        private Collection<Book> books;
    
        public BookLibrary(Collection<Book> books) {
            this.books = books;
        }
    
        public boolean addBook(Book book) {
            return books.add(book);
        }
    
        public Collection<Book> getBooks() {
            return books;
        }
    
        public Iterator<Book> iterator() {
            return books.iterator();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    测试

    public class Client {
        public static void main(String[] args) {
            Book[] books = new Book[5];
            books[0] = new Book("English", "Head First Java, 2nd Edition", "Kathy Sierra", 10123758943l, 10.5d);
            books[1] = new Book("English", "Effective Java (3rd Edition)", "Bloch Joshua", 34422235432l, 8.65d);
            books[2] = new Book("English", "Mastering Java Machine Learning", "Dr Uday Kamath", 2123765476l, 21.45d);
            books[3] = new Book("English", "Cloud-Native Java", "Kenny Bastani ", 21332343434l, 28.99d);
            books[4] = new Book("English", "Java 9 Modularity", "Paul Bakker", 1645243587l, 36.68d);
    
            BookLibrary bookLibrary = new BookLibrary(Arrays.asList(books));
            for (Book book : bookLibrary) {
                System.out.println(book);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行结果

    Book{language='English', title='Head First Java, 2nd Edition', author='Kathy Sierra', ISBN=10123758943, price=10.5}
    Book{language='English', title='Effective Java (3rd Edition)', author='Bloch Joshua', ISBN=34422235432, price=8.65}
    Book{language='English', title='Mastering Java Machine Learning', author='Dr Uday Kamath', ISBN=2123765476, price=21.45}
    Book{language='English', title='Cloud-Native Java', author='Kenny Bastani ', ISBN=21332343434, price=28.99}
    Book{language='English', title='Java 9 Modularity', author='Paul Bakker', ISBN=1645243587, price=36.68}
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三、参考

    1. https://www.runoob.com/design-pattern/iterator-pattern.html
    2. https://www.geeksforgeeks.org/design-patterns-in-java-iterator-pattern/
    3. https://www.digitalocean.com/community/tutorials/iterator-design-pattern-java
  • 相关阅读:
    css实现贴合滚动
    python之 pyCharm pip安装pandas库失败
    935. 骑士拨号器
    深信服SG上网优化管理系统存在任意文件读取漏洞 附POC
    Java岗吃透这份pdf,拿下阿里、腾讯等大厂offer
    【QT开发笔记-基础篇】| 第四章 事件QEvent | 4.7 拖放事件
    Cesium 空间量算——方位角量测
    Symmetric crypto AES
    仿网易云移动端项目Vue3.2+Pinia+Vant+axios
    如何设计实现系统应支持至少300个并行用户的同时访问和使用的需求
  • 原文地址:https://blog.csdn.net/M_sdn/article/details/126343901