Last active
November 6, 2016 07:17
-
-
Save shoheiyokoyama/f8aa6912e7e0ac90bcba to your computer and use it in GitHub Desktop.
デザインパターン「Iterator」 ref: http://qiita.com/shoheiyokoyama/items/3f42d0057d9d5a861039
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Aggregate { | |
public abstract Iterator iterator(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Book { | |
private String name; | |
public Book(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class BookShelf implements Aggregate { | |
private ArrayList books; | |
public BookShelf(int initialize) { | |
this.books = new ArrayList(initialize); | |
} | |
public Book getBookAt(int index) { | |
return (Book)books.get(index); | |
} | |
public void appendBook(Book book) { | |
books.add(book); | |
} | |
public int getLength() { | |
return books.size(); | |
} | |
public Iterator iterator() { | |
return new BookShelfIterator(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Git Hub | |
Qiita | |
Java | |
Iterator pattern |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Git Hub | |
Qiita | |
Java | |
Iterator pattern |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Iterator { | |
public abstract boolean hasNext(); | |
public abstract Object next(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
BookShelf bookShelf = new BookShelf(4); | |
bookShelf.appendBook(new Book("Git Hub")); | |
bookShelf.appendBook(new Book("Qiita")); | |
bookShelf.appendBook(new Book("Java")); | |
bookShelf.appendBook(new Book("Iterator pattern")); | |
Iterator iterator = bookShelf.iterator(); | |
while(iterator.hasNext()) { | |
Book book = (Book)iterator.next(); | |
System.out.println(book.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment