Design Pattern-iterator pattern (iterator)

Source: Internet
Author: User
ArticleDirectory
    • Container interface Aggregate
    • Iterator Interface Class iterator
    • Book. Java
    • Bookshelf
    • Bookshelfiterator
    • Main Function

1. References

Reference 1: http://topic.csdn.net/u/20080722/16/e7fcffc3-3d54-48f6-9d3a-2cd39073a287.html

Reference 2: http://www.java63.com/design_pattern/iterator_pattern.html

Reference 3: http://blog.csdn.net/aaaaaaaa0705/article/details/6282305

Problem Origin

I want to find some content about the next () and hasnext () Methods of the iterator, and finally return them to the iterator AND THE iterator pattern in the design pattern. Now let's record your learning experience.

1. What is iteration mode?

The iterator mode is also called the iteration mode. It is one of the behavior modes. It grants access to internal objects contained in the container to external classes and uses iterator to traverse access in order.

2. applications that do not use the iteration Mode

Before applying the iterator mode, you should first understand what problems the iterator mode is used to solve. Or, if you do not use the iterator mode, what problems will occur.

    1. The container implements sequential traversal by itself. Directly add the sequential Traversal method in the container class
    2. Let the caller implement traversal by themselves. Expose data details to the outside.
Iii. disadvantages of not using iteration Mode

The methods 1 and 2 above can implement traversal. What is the problem?

    1. The container class undertakes too many functions: on the one hand, it needs to provide the necessary functions such as adding and deleting; on the other hand, it also needs to provide the traversal access function.
    2. Usually, containers need to save the traversal status during traversal. When they are mixed with functions such as adding and deleting elements, it is easy to cause confusion andProgramRunning error.
Iv. Application Using iteration Mode

The iterator mode is a design mode designed to effectively process sequential traversal access. In short, the iterator mode provides an effective method, it can block the implementation details of the container class of the collection object, and effectively traverse and access the object elements contained in the container in order.
Therefore, the application scenarios of the iterator model can be summarized to meet the following conditions:

    1. Access the internal objects contained in the container
    2. Sequential access
V. Structure of iteration Mode

Vi. Roles and responsibilities of the iteration Model

Iterator (iterator Interface): This interface must define the minimum defined method set to implement the iteration function, such as providing the hasnext () and next () methods.
Concreteiterator (iterator implementation class): for example, bookshelfiterator and iterator implementation class. It can be implemented according to the actual situation.
Aggregate (container Interface): defines basic functions and provides methods similar to iterator.
Concreteaggregate (container implementation class): for example, bookshelf, container interface implementation class. The iterator () method must be implemented.

VII. Advantages of the iteration Mode
    1. Implement function separation to simplify container interfaces. Let the container only implement its own basic functions, assign iteration functions to external class implementation, and comply with the class design principles.
    2. Hide the implementation details of the container.
    3. A unified interface is provided for the container or its sub-containers to facilitate the call. On the other hand, the caller does not have to pay attention to the implementation details of the iterator.
    4. Different iteration methods or multiple iteration methods can be implemented for the container or its sub-containers.
8, Code Instance container interface Aggregate
 
Public InterfaceAggregate {
Public AbstractIterator ();
}
Iterator Interface Class iterator
 
Public InterfaceIterator {
Public Abstract BooleanHasnext ();
Public AbstractObject next ();
}
Book. Java
 
Public ClassBook {
PrivateString name = "";

PublicBook (string name ){
This. Name = Name;
}

/**
* Obtain the book name
*@ ReturnString
*/
PublicString getname (){
ReturnName;
}
}
Bookshelf
 /**  
* Bookshelves
* Concreateaggregate
*/
Public Class Bookshelf Implements Aggregate {
Private Book [] books;
Private Int Last = 0;

// Constructor
Public Bookshelf ( Int Maxsize ){
This . Books =New Book [maxsize];
}

// Searching books
Public Book getbookat ( Int Index ){
Return Books [Index];
}

// Add books
Public Void Appendbook (Book ){
This . Books [last] = book;
Last ++;
}

// Obtain the number of bookshelves
Public Int Getlength (){
Return Books. length;
}

// Get the bookshelf iterator object
@ Override
Public Iterator (){
Return New Bookshelfiterator ( This );
}
}
Bookshelfiterator
  //  Concreateiterator  
Public Class Bookshelfiterator Implements Iterator {
Private Bookshelf bookshelf;
Private Int Index;

Public Bookshelfiterator (bookshelf ){
This . Bookshelf = bookshelf;
This . Index = 0;
}

// Check if there is another book
Public Boolean Hasnext (){
If (Index <bookshelf. getlength ()){
Return True ;
}
Else {
Return False ;
}
}
// Returns the book at the specified position.
Public Object next (){
Book = bookshelf. getbookat (INDEX );
Index ++;
Return Book;
}
}
Main Function
 Public   Class Main {
Public Static Void Main (string [] ARGs ){
// Generate a bookshelf
Bookshelf bookshelf = New Bookshelf (4 );
// Add books to bookshelves
Bookshelf. appendbook ( New Book ("Zhou Enlai's later years "));
Bookshelf. appendbook ( New Book ("C ++ network programming "));
Bookshelf. appendbook ( New Book ("J2EE network programming "));
Bookshelf. appendbook ( New Book ("Java programming ideas "));

// Get the bookshelf iterator
Iterator it = bookshelf. iterator ();
While (It. hasnext ()){
Book = (book) it. Next ();
System. Out. println (book. getname ());
}
}
}
Summary

All in all, the overall idea of the iterator design mode is to "add, delete, modify, and query objects in the container implementation class, and implement traversal operations in the iterator implementation class ."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.