博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
迭代器模式的应用
阅读量:4968 次
发布时间:2019-06-12

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

类图:

 

代码示例:

 

package iterator;public interface Aggregate {    public Iterator iterator();}
package iterator;public interface Iterator {    public boolean hasNext();    public Object next();}
package iterator;public abstract class BookShelfs {        public abstract void appendBook(Book book);        public abstract Book getBookAt(int index);        public abstract int getLength();        }
package iterator;/** * 具体的聚合器 */public class BookShelf extends BookShelfs implements Aggregate {        private Book[] books;    //标记目前书架上有多少书    private int last;        public BookShelf(int maxsize){        books = new Book[maxsize];        last = 0;    }        @Override    public Book getBookAt(int index){        return books[index];    }        @Override    public void appendBook(Book book){        /*实际上应该有一个最大容量的判断,如果超出了数组边界,会抛出越界异常        (就看这里是由编程者自己做处理还是让编译器抛异常,上层程序再来处理)        如果是类的开发者,应该是会选择让上层程序来处理        */        books[last]=book;        last++;    }        @Override    public int getLength(){        return last;    }        @Override    public Iterator iterator() {        //直接创建使用一个迭代器        return new BookShelfIterator(this);    }}
package iterator;import java.util.Vector;public class BookShelf2 extends BookShelfs implements Aggregate {        private Vector
books; public BookShelf2(){ books = new Vector
(); } @Override public void appendBook(Book book){ books.add(book); } @Override public Book getBookAt(int index){ return books.get(index); } @Override public int getLength() { return books.size(); } @Override public Iterator iterator() { return new BookShelfIterator(this); }}
package iterator;public class BookShelfIterator implements Iterator {        private BookShelfs bookShelf;    //记录下当前迭代的位置    private int index;        public BookShelfIterator(BookShelfs bookShelf){        this.bookShelf = bookShelf;        index = 0;    }        @Override    public boolean hasNext() {        if(index
package iterator;public class Book {    private String name;    public Book(){}        public Book(String name){        this.name = name;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

 

package iterator;public class Client {    public static void main(String[] args) {        //数组存储书籍        BookShelf myShelf = new BookShelf(10);        //以一种不同的组织方式组织书的书架(Vector存放书籍)        BookShelf2 otherShelf = new BookShelf2();                myShelf.appendBook(new Book("thinking in java"));        myShelf.appendBook(new Book("effective java"));        myShelf.appendBook(new Book("design patterns"));                otherShelf.appendBook(new Book("head first java"));        otherShelf.appendBook(new Book("head first design pattern"));                Iterator it = new BookShelfIterator(myShelf);        while(it.hasNext()){            Book b = (Book) it.next();            System.out.println(b.getName());        }                it = new BookShelfIterator(otherShelf);        while(it.hasNext()){            Book b = (Book) it.next();            System.out.println(b.getName());        }                                    }}

 

运行结果:

thinking in javaeffective javadesign patternshead first javahead first design pattern

 

转载于:https://www.cnblogs.com/tisakong/p/4707600.html

你可能感兴趣的文章
socket tcp
查看>>
DataMining--Python基础入门
查看>>
单片机复位电路
查看>>
php json_decode失败,返回null
查看>>
获取单选按钮选中的值
查看>>
oracle 分页
查看>>
助教学期总结
查看>>
绘制基本 图形之矩形与多边形
查看>>
3-day3-list-truple-map.py
查看>>
02: djangorestframework使用
查看>>
7zip 自解压安装程序
查看>>
Edit控件显示多行文字
查看>>
JS第二周
查看>>
dataTable.NET的search box每輸入一個字母進行一次檢索的問題
查看>>
Python 文件处理
查看>>
邻接表详解
查看>>
服务器一:分布式服务器结构
查看>>
迭代dict的value
查看>>
eclipse package,source folder,folder区别及相互转换
查看>>
Py 可能是最全面的 python 字符串拼接总结(带注释版)
查看>>