최근 포토로그


2008/07/20 21:36

[DesignPattern - Iterator] sample1 study. note

Design Pattern - Iterator

1. 클래스다이어그램


2. Source
  • Aggregate.java
    package com.dazzilove.iterator.sample1;

    import junit.framework.TestCase;

    public class BookAggregateTest extends TestCase {
    public void testBookAggregateTest() {
    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());
    }
    }
    }
  • Iterator.java
    package com.dazzilove.iterator.sample1;

    public interface Iterator {
    public abstract boolean hasNext();
    public abstract Object next();
    }
  • BookShelf.java
    package com.dazzilove.iterator.sample1;

    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 this.last;
    }

    public Iterator iterator() {
    return new BookShelfIterator(this);
    }
    }
  • BookShelfIterator.java
    package com.dazzilove.iterator.sample1;

    public class BookShelfIterator implements Iterator {

    private BookShelf bookShelf;
    private int index;

    public BookShelfIterator(BookShelf bookShelf) {
    this.bookShelf = bookShelf;
    this.index = 0;
    }

    @Override
    public boolean hasNext() {
    if(index < bookShelf.getLength()) {
    return true;
    } else {
    return false;
    }
    }

    @Override
    public Object next() {
    Book book = bookShelf.getBookAt(index);
    index++;
    return book;
    }
    }
  • Book.java
    package com.dazzilove.iterator.sample1;

    public class Book {
    private String name = "";

    public Book(String name) {
    this.name = name;
    }

    public String getName() {
    return name;
    }
    }
  • BookAggregateTest.java
    package com.dazzilove.iterator.sample1;

    import junit.framework.TestCase;

    public class BookAggregateTest extends TestCase {
    public void testBookAggregateTest() {
    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());
    }
    }
    }


3. 결과출력

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://dazzilove.egloos.com/tb/605056 [도움말]

덧글

덧글 입력 영역



미투데이

문화꽃 키우기