2012년 4월 21일 토요일

[Java Design Pattern_1]Iterator 샘플 코드1_반복자 변경




@ 기본 패턴


- Iterator(반복자) : interface Iterator


 package basic.pattern;  
 public interface Iterator {  
      public abstract Object next();  
      public abstract boolean hasNext();  
 }  


- Aggregate(집합체)   :  interface Aggregate


 package basic.pattern;  
 public interface Aggregate {  
           public abstract Iterator iterator();  
 }  


@ 구현


- Element(요소) : Book


 package Sample;  
 public class Book {  
      String name;  
      public Book(String name) {  
           this.name = name;  
      }  
      String getName(){  
           return name;  
      }  
 }  


- ConcreteAggregate(구체적인 집합체)


1. BookShelfArrange.java


 package Sample;  
 import basic.pattern.Aggregate;  
 import basic.pattern.Iterator;  
 public class BookShelfArrange implements Aggregate {  
      private Book[] books;  
      private int last=0;  
      public BookShelfArrange(int max) {  
           books=new Book[max];  
      }  
      public void appendBook(Book book){  
           this.books[last]=book;  
           last++;  
      }  
      public Book getBookAt(int index){  
           return books[index];  
      }  
      public int getLength(){  
           return last;  
      }  
 //     @Override  
 //     public Iterator iterator() {  
 //          // TODO Auto-generated method stub  
 //          return new BookShelfFrontIterator(this);  
 //     }  
      public Iterator iterator() {  
           // TODO Auto-generated method stub  
           return new BookShelfBackIterator(this);  
      }  
 }  
*주석된 부분만 바꾸면 반복자를 수정할 수 있다. 다른 클래스는 수정할 필요 없다.



- ConcreteIterator(구체적인 반복자)


1. BookShelfBackIterator


 package Sample;  
 import basic.pattern.Iterator;  
 public class BookShelfBackIterator implemeats Iterator {  
      private BookShelfArrange bookShelf;  
      private int index;  
      public BookShelfBackIterator(BookShelfArrange shelf) {  
           this.bookShelf = shelf;  
           index=0;  
      }  
      @Override  
      public Object next() {  
           Book book=bookShelf.getBookAt(index);  
           index++;  
           return book;  
      }  
      @Override  
      public boolean hasNext() {  
           if(index<bookShelf.getLength())  
                return true;  
           else  
                return false;  
      }  
 }  

2.BookShelfFrontIterator


 package Sample;  
 import basic.pattern.Iterator;  
 public class BookShelfFrontIterator implements Iterator {  
      private BookShelfArrange bookShelf;  
      private int index;  
      public BookShelfFrontIterator(BookShelfArrange shelf) {  
           this.bookShelf = shelf;  
           index=shelf.getLength()-1;  
      }  
      @Override  
      public Object next() {  
           Book book=bookShelf.getBookAt(index);  
           index--;  
           return book;  
      }  
      @Override  
      public boolean hasNext() {  
           if(index>-1)  
                return true;  
           else  
                return false;  
      }  
 }  


@ 실행


- Main


 package Sample;  
 import basic.pattern.Iterator;  
 public class Main {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           BookShelfArrange bookShelf=new BookShelfArrange(5);  
           bookShelf.appendBook(new Book("A"));  
           bookShelf.appendBook(new Book("B"));  
           bookShelf.appendBook(new Book("C"));  
           bookShelf.appendBook(new Book("D"));  
           bookShelf.appendBook(new Book("E"));  
           Iterator it=bookShelf.iterator();  
           while(it.hasNext()){  
                System.out.println(((Book)it.next()).getName());  
           }  
           System.out.println("End");  
      }  
 }  









댓글 없음:

댓글 쓰기