Background: If a file is increasing data incrementally, if you need to read this file incrementally, you can iterate through the incremental data using the iterator design pattern.
For example: a document Aa.txt
Aaa
Aaa
Aaa
Aaa
You can divide the article into fragments (file, start, end), paragraph (file, start, end)
The new data in the file requires sequential access, so you can analyze it using the iterator design pattern
Start and end of newly added data
public class Paragraph Implemnets iterator<segment>{
Private String FileName;
Private long start;
private long end;
Public itertaor<segment> iterator () {
return new Linereader ();
}
}
The beginning and end of each piece of data
public class segment{
Private String FileName;
Private long start;
private long end; }
public class Linereader Implemnets iterator<segment>{
Long start;
Long end;
public Boolean hasnext () {
Business logic for start and end
}
Public Segment Next () {
Segment segment=new Segment ();
The business logic builds the object while updating the start value
}
}
Here is an example of another blog http://blog.csdn.net/xw13106209/article/details/6912873
roles and responsibilities for iterative patterns
Iterator (Iterator interface): The interface must define a minimal set of methods for implementing the iteration functionality, such as providing Hasnext () and Next () methods.
Concreteiterator (Iterator implementation Class): For example, Bookshelfiterator, iterator interface iterator implementation class. Can be achieved on the basis of specific circumstances.
Aggregate (Container Interface): Defines basic functionality and provides methods similar to iterator iterator ().
Concreteaggregate (Container implementation Class): For example, bookshelf, container interface implementation class. The iterator iterator () method must be implemented.
the advantages of iterative mode are to realize functional separation and simplify the container interface. Let the container only realize its own basic functions, the iterative function Committee to the external class implementation, consistent with the design principles of the class. Hides the implementation details of the container. Provides a unified interface for a container or its child containers for easy invocation on the one hand, and on the other the caller does not have to focus on the implementation details of the iterator. You can implement different iterative methods or multiple iterative methods for a container or its child containers. Code Instances Container interface Aggregate
[Java] view plain copy public interface Aggregate {public abstract iterator iterator (); }
iterator interface class iterator [Java] view plain copy public interface iterator {public abstract Boolean hasnext (); Public abstract Object Next (); } Book.java
[Java] View Plain copy public class book { private string name= ""; public book (string name) { this.name = name; } /** * Get book name * @return String */ public string getname () { return name; } }
Container interface Implementation Class Bookshelf [Java] View plain copy/** * Bookshelf * ConcreateAggregate */ public class bookshelf implements aggregate{ private Book[] books;