博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式之迭代器(Iterator Pattern)-笔记
阅读量:4028 次
发布时间:2019-05-24

本文共 2159 字,大约阅读时间需要 7 分钟。

设计模式之迭代器
    Iterator Pattern 是指依序遍历并处理多个数字或变量,Iterate英文单字本身也是“反复”的意思,Iterator这个名词可译为"迭代器"(书本原文)
   
代码如下:
   
  1. /**
  2.  * 程序主类
  3.  * DemoTest.java
  4.  */
  5. public class DemoTest {
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.         BookShelf bookShelf = new BookShelf(4);
  11.         bookShelf.appendBook(new Book("Java core1"));
  12.         bookShelf.appendBook(new Book("Java core2"));
  13.         bookShelf.appendBook(new Book("Java core3"));
  14.         bookShelf.appendBook(new Book("Java core4"));
  15.         
  16.         //遍历
  17.         Iterator iterator = bookShelf.iterator();
  18.         while(iterator.hasNext()) {
  19.             Book book = (Book)iterator.next();
  20.             System.out.println(" " + book.getName());
  21.         }
  22.     }
  23. }
  1. /**
  2.  * 聚合接口
  3.  * Aggregate.java
  4.  *
  5.  */
  6. public interface Aggregate {
  7.     public abstract Iterator iterator();
  8. }
  1. /**
  2.  * 迭代接口
  3.  * Iterator.java
  4.  */
  5. public interface Iterator {
  6.     public abstract boolean hasNext();
  7.     public abstract Object next();
  8. }
  1. /**
  2.  * 书架迭代者
  3.  * BookShelfIterator.java
  4.  */
  5. public class BookShelfIterator implements Iterator {
  6.     private BookShelf bookShelf;
  7.     private int index;
  8.     
  9.     /**
  10.      * 构造方法
  11.      * @param bookShelft 书架对象
  12.      */
  13.     public BookShelfIterator(BookShelf bookShelft) {
  14.         this.bookShelf = bookShelft;
  15.         this.index = 0;
  16.     }
  17.     
  18.     /**
  19.      * 是否有下一个元素
  20.      */
  21.     public boolean hasNext() {
  22.         if (index < bookShelf.getLength()) {
  23.             return true;
  24.         }else {
  25.             return false;
  26.         }
  27.     }
  28.     
  29.     /**
  30.      * 获得下一个元素
  31.      */
  32.     public Object next() {
  33.         Book book = (Book)bookShelf.getBook(index);
  34.         index++;
  35.         return book;
  36.     }
  37.     
  38.     
  39. }
  1. /**
  2.  * 书架类
  3.  * BookShelf.java
  4.  */
  5. public class BookShelf implements Aggregate {
  6.     private Book[] books;
  7.     private int last = 0;
  8.     
  9.     /**
  10.      * 构造方法
  11.      * @param maxSize 书架的最大藏书量
  12.      */
  13.     public BookShelf(int maxSize) {
  14.         this.books = new Book[maxSize];
  15.     }
  16.     
  17.     /**
  18.      * 获得书本对象
  19.      * @param index 书本在书架的索引值
  20.      * @return 书对对象的引用
  21.      */
  22.     public Book getBook(int index) {
  23.         return books[index];
  24.     }
  25.     
  26.     /**
  27.      * 向书架添加书本
  28.      * @param book 添加的书本对象
  29.      */
  30.     public void appendBook(Book book) {
  31.         books[last] = book;
  32.         last++;
  33.     }
  34.     
  35.     /**
  36.      * 获得书架中书本的个数
  37.      * @return 书本的个数
  38.      */
  39.     public int getLength() {
  40.         return last;
  41.     }
  42.     
  43.     /**
  44.      * 迭代方法
  45.      */
  46.     public Iterator iterator() {
  47.         return new BookShelfIterator(this);
  48.     }
  49.     
  50.     
  51. }
  1. /**
  2.  * 书本类
  3.  * Book.java
  4.  */
  5. public class Book {
  6.     private String name = "";
  7.     /**
  8.      * 构造方法
  9.      * @param name 书名
  10.      */
  11.     public Book(String name) {
  12.         this.name = name;
  13.     }
  14.     /**
  15.      * 获得书本书名的方法
  16.      * @return 书名
  17.      */
  18.     public String getName() {
  19.         return name;
  20.     }
  21. }

转载地址:http://ofmbi.baihongyu.com/

你可能感兴趣的文章
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt5 everywhere编译完成后,找不到qmake
查看>>
arm-linux开机读取硬件时钟,设置系统时钟。
查看>>
交叉编译在x86上调试好的qt程序
查看>>
/dev/input/event0 键盘输入
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
输入设备节点自动生成
查看>>
opencv test code-1
查看>>
eclipse 导入先前存在的项目
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
busybox passwd修改密码
查看>>
wpa_supplicant控制脚本
查看>>
rfkill: WLAN hard blocked
查看>>
gstreamer相关工具集合
查看>>
arm 自动升级脚本
查看>>