1 / 5

<<interface>> Aggregate

Ch.1 Iterator Pattern. <<interface>> Iterator. <<interface>> Aggregate. +hasNext +next. +iterator. BookShelfIterator. -bookShelf -index. BookShelf. -books[] -last. +hasNext +next. +getbookAt +appendBook +getLength +iterator. Book. -name +getName. Creates ▶.

rufus
Download Presentation

<<interface>> Aggregate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch.1 Iterator Pattern <<interface>> Iterator <<interface>> Aggregate +hasNext +next +iterator BookShelfIterator -bookShelf -index BookShelf -books[] -last +hasNext +next +getbookAt +appendBook +getLength +iterator Book -name +getName Creates ▶ 장 덕 성 계명대학교 컴퓨터공학과 정보공학실험실

  2. Ch.1 Iterator Pattern ▌Book Class public class Book { private String name = ""; public Book(String name) { this.name = name; } public String getName() { return name; } } 장 덕 성 계명대학교 컴퓨터공학과 정보공학실험실

  3. Ch.1 Iterator Pattern ▌BookShelf Class public class BookShelf implements Aggregate { private Book[] books; private int last = 0; public BookShelf(int maxsize) { this.books = new Book[maxsize]; } public Book getBookAt(int index) { return books[index]; } public void appendBook(Book book) { this.books[last] = book; last++; } public int getLength() { return last; } public Iterator iterator() { return new BookShelfIterator(this); } } 장 덕 성 계명대학교 컴퓨터공학과 정보공학실험실

  4. Ch.1 Iterator Pattern ▌BookShelfIterator Class public class BookShelfIterator implements Iterator { private BookShelf bookShelf; private int index; public BookShelfIterator(BookShelf bookShelf) { this.bookShelf = bookShelf; this.index = 0; } public boolean hasNext() { if (index < bookShelf.getLength()) { return true; } else { return false; } } public Object next() { Book book = bookShelf.getBookAt(index); index++; return book; } } 장 덕 성 계명대학교 컴퓨터공학과 정보공학실험실

  5. Ch.1 Iterator Pattern ▌Main Class public class Main { public static void main(String[] args) { BookShelf bookShelf = new BookShelf(4); bookShelf.appendBook(new Book("Around the World in 80 Days")); bookShelf.appendBook(new Book("Bible")); bookShelf.appendBook(new Book("Cinderella")); bookShelf.appendBook(new Book("Daddy-Long-Legs")); Iterator it = bookShelf.iterator(); while (it.hasNext()) { Book book = (Book)it.next(); System.out.println("" + book.getName()); } } } 장 덕 성 계명대학교 컴퓨터공학과 정보공학실험실

More Related