004 Behavioral -04- iterative mode (Iterator)

I. Overview

  A method of providing a sequential access to each element in an aggregate object, but without exposing the interior of the object representation.

  Iterator pattern is the separation of the traversal behavior collection of objects, abstract an iterator class to be responsible, so that both can be done without exposing the internal structure of the collection, but also allow external code transparent access to internal data collection.

  Iterative mode uses less, JDK collection also provides a concrete realization of Iterator can be directly used, do not realize their own

1.1 application scenarios

  1, access to a polymeric content object without exposing its internal representation.

  2, it is necessary to traverse a variety of ways to provide a polymeric object.

  3, provides a uniform interface for traversing different polymeric structure.

1.2, the advantages and disadvantages

Advantages:  1, which supports a different way to traverse aggregate object. 2, simplifying the polymerization iterator class. 3, in the same polymerization may have a plurality of traverse. 4, in an iterative mode, add a new class of polymerizable iterator class and easy, without having to modify the existing code.

Disadvantages: Due to the separation of duties iterative mode and stores data through the data, adding new classes need a corresponding increase in new polymerization iterator class, the class number of pairs increases, which increases the complexity of the system to some extent.

1.3, class diagram roles and responsibilities

   

  Roles and responsibilities of the iterative mode

  . 1, the Iterator (iterator interface) :

  The interface must implement an iterative function defines the minimum set of methods defined

  Such as providing hasNext () and next () method.

  2, ConcreteIterator (iterator implementation class) :

  Iterator iterator interface implementation class. It can be implemented depending on the circumstances.

  . 3, Aggregate (trap port) :

  The method defined basic functions provide similar Iterator iterator () a.

  . 4, concreteAggregate (container implementation class) :

  Container interface implementation class. We must implement Iterator iterator () method.

1.4 evolution

1.4.1 initialization

Book create content to be stored in a container

public class Book {
    private String id;
    private String name;
    private double price;

    public Book(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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("ID=" + id + ",\tname=" + name + ",\tprice" + price);
    }
}
View Code

 

Create a container BookList

public  class BookList {
     // inner container is a List, the array can also be used 
    Private List <Book> = bookList new new the ArrayList <Book> ();
     Private  int index; 

    // add books 
    public  void the addBook (Book Book) { 
        bookList.add (book); 
    } 

    // delete books 
    public  void removeBook (book book) {
         int bookIndex = bookList.indexOf (book); 
        bookList.remove (bookIndex); 
    } 

    // determine whether the next book 
    public  boolean hasNext () {
         IF (index> =bookList.size ()) {
             return  to false ; 
        } 
        return  to true ; 
    } 

    // get next book 
    public Book getNext () {
         return bookList.get (index ++ ); 
    } 

    // Get the length of the collection 
    public  int getSize () {
         return bookList .size (); 
    } 

    // index acquired Book The 
    public Book the getByIndex ( int index) {
         return bookList.get (index); 
    } 
}

  Next, is the iteration container

Mode 1 (the order of traversal achieved by the container themselves. Direct addition order traversal method directly in the container class)

    @Test
    public void test1() {
        BookList bookList = new BookList();

        Book book1 = new Book("001","设计模式",200);
        Book book2 = new Book("002","Java核心编程",200);
        Book book3 = new Book("003","计算机组成原理",200);

        bookList.addBook(book1);
        bookList.addBook(book2);
        bookList.addBook(book3);

        while(bookList.hasNext()){
            Book book = bookList.getNext();
            book.display();
        }
    }

Export

ID = 001, name = design patterns, price200.0 
ID = 002, name = core of the Java programming, price200.0 
ID = 003, name = computer composition principle, price200.0

 

Second way, let the caller himself traversal. Details directly exposed to the external data)

    @Test
    public void test2() {
        BookList bookList = new BookList();

        Book book1 = new Book("001", "设计模式", 200);
        Book book2 = new Book("002", "Java核心编程", 200);
        Book book3 = new Book("003", "计算机组成原理", 200);

        bookList.addBook(book1);
        bookList.addBook(book2);
        bookList.addBook(book3);

        for (int i = 0; i < bookList.getSize(); i++) {
            Book book = bookList.getByIndex(i);
            book.display();
        }
    }

The results above

Disadvantage of not using an iterative mode

  Methods 1 and 2 above method can be implemented to traverse, but these problems

  1, container classes took on too much functions: on the one hand the need to provide proper function to add or delete itself; on the one hand need to provide access to traverse.

  2, often container in the process of implementing traversal, traverse the state needs to be saved, when with features such as add and delete elements mixed together, can easily lead to confusion and running errors.

Conditions applying an iterative mode

  Iterator pattern is to efficiently process proceeds to access a design pattern traversing sequentially Briefly, Iterator mode provides an efficient method, the implementation details can be shielded container classes aggregated set of objects, but can contain a container object elements for effective access to traverse in order.

  So, Iterator mode of application scenarios can be summarized as the following conditions are met:

  1, access to internal objects contained in the container
  2, in order to access

1.4.2, evolution

Implemented in code about the iterative mode, you can simply modify the BookList, BookListIterator

public  class BookListIterator {
     // inner container is a List, the array can also be used 
    Private List <Book> = bookList new new the ArrayList <Book> ();
     Private  int index; 

    // add books 
    public  void the addBook (Book Book) { 
        bookList.add (book); 
    } 

    // remove Books 
    public  void removeBook (book book) {
         int bookIndex = bookList.indexOf (book); 
        bookList.remove (bookIndex); 
    } 

    // Get the length of the collection 
    public  int getSize () {
         returnbookList.size (); 
    } 

    // index acquired Book The 
    public Book the getByIndex ( int index) {
         return bookList.get (index); 
    } 

    // get an Iterator 
    public Iterator Iterator () {
         return  new new Itr (); 
    } 

    // inner class, instance of the Iterator (due to the use of internal information container, it should be rewritten inner class) 
    Private  class Itr the implements the Iterator {
         // determines whether there is a next book, the hasNext () just copied contents to 
        public  Boolean the hasNext () {
             IF (index> = bookList.size ()) {
                 return false ; 
            } 
            return  to true ; 
        } 
        // get the next book will getNext () just copied the contents to 
        public Object the Next () {
             return bookList.get (index ++ ); 
        } 

        public  void the Remove () { 

        } 
    } 
}

test

    @Test
    public void test3() {
        BookListIterator bookList = new BookListIterator();

        Book book1 = new Book("001","设计模式",200);
        Book book2 = new Book("002","Java核心编程",200);
        Book book3 = new Book("003","计算机组成原理",200);

        bookList.addBook(book1);
        bookList.addBook(book2);
        bookList.addBook(book3);

        Iterator iterator = bookList.Iterator();
        while(iterator.hasNext()){
            Book book = (Book) iterator.next();
            book.display();
        }
    }

Ibid output

  We can see, Iterator and use of this method on the JDK collection exactly the same as.

Second, the extension

2.1 JDK

java.util.Iterator
java.util.ArrayList中的Itr

2.2 Mybatis

org.apache.ibatis.cursor.defaults.DefaultCursor的cursorIterator

 

 

 

c

 

Guess you like

Origin www.cnblogs.com/bjlhx/p/11545378.html