迭代模式

package org.designPattern.iterator20;


public class Book {
    private String ISBN;
    private String name;
    private double price;
    
    public Book(String isbn, String name, double price) {
        ISBN = isbn;
        this.name = name;
        this.price = price;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String isbn) {
        ISBN = isbn;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    
    public void display() {
        System.out.println("ISBN=" + ISBN + ",name=" + name + ",price" + price);
    }
    
    
}

package org.designPattern.iterator20;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class BookList {
    private List<Book> bookList;
    private int index;
    private Iterator iterator;
    
    public BookList() {
        bookList = new ArrayList<Book>();
    }
    
    //添加书籍
    public void addBook(Book book) {
        bookList.add(book);
    }
    
    //删除书籍
    public void deleteBook(Book book) {
        int bookIndex = bookList.indexOf(book);
        bookList.remove(bookIndex);
    }
    
//    //判断是否有下一本书
//    public boolean hasNext() {
//        if(index >= bookList.size()) {
//            return false;
//        }
//        return true;
//    }
//    
//    //获得下一本书
//    public Book getNext() {
//        return bookList.get(index++);
//    }
    
//    public List<Book> getBookList() {
//        return bookList;
//    }
    
    public Iterator Iterator() {
        return new Itr();
    }
    
    private class Itr implements Iterator{

        public boolean hasNext() {
            if(index >= bookList.size()) {
                return false;
            }
            return true;
        }

        public Object next() {
            return bookList.get(index++);
        }

        public void remove() {
            
        }
        
    }
    
}

package org.designPattern.iterator20;

import java.util.Iterator;


public class MainClss {
    public static void main(String[] args) {
        BookList bookList = new BookList();
        
        Book book1 = new Book("010203","Java编程思想",90);
        Book book2 = new Book("010204","Java从入门到精通",60);
        
        bookList.addBook(book1);
        bookList.addBook(book2);
        
//        while(bookList.hasNext()) {
//            Book book = bookList.getNext();
//            book.display();
//        }
        
//        List<Book> bookDateList = bookList.getBookList();
//        for(int i = 0; i < bookDateList.size(); i++) {
//            Book book = bookDateList.get(i);
//            book.display();
//        }
        
        Iterator iter = bookList.Iterator();
        while(iter.hasNext()) {
            Book book = (Book) iter.next();
            book.display();
        }
        
        
    }
}


转载于:https://my.oschina.net/goudingcheng/blog/538702

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值