Android Ap Development and Design Pattern Article 1: iterator Pattern

Source: Internet
Author: User

Iterator Pattern Iterator Mode
Scenario Conjecture
If you write code frequently, you will encounter traversing an array. Use the for loop to obtain the array subscript and perform further operations. For example, the following code:
 
Int [] array = new int [5];
For (int I = 0; I <array. length; I ++ ){
System. out. println ("" + I );
}
Or:
 
Int [] array = new int [5];
For (int I: array ){
System. out. println ("" + I );
}
 
Of course, there are more ways to use I to traverse array elements by moving the subscript over.
When writing with the design pattern, you can abstract the I behavior into an iterator. This pattern is called the iterator pattern. The iterator mode can be used to traverse a collection. Java also provides a tool class: java. util. Iterator <E>, similar.
 
 
Scenario requirements
There are five technical books on the shelf. The names of these books must be traversed to display them on Andriod's EditText. The preceding requirements are used as an example to describe the specific requirements as follows:


 
 
 
 
Aggregate Interface
The Aggregate interface is an incremental "aggregation ". Classes that implement this interface become "Aggregation" similar to arrays and collections ". Indicates the aggregated interface.
Iterator Interface
The Iterator interface executes an increasing number of elements, and provides functions similar to cyclic variables. Indicates the interface for increasing and traversing.
Book class
Book indicates a Book, mainly used to obtain details of the Book.
BookShelf class
BookShelf indicates the bookshelves. This class implements the Aggregate interface and uses it as aggregation.
BookShelfIterator class
BookShelfIterator indicates the class used to scan bookshelves and implements the Iterator interface function.
 
-----------------------------------------
IteratorPatternsActivity class
IteratorPatternsActivity is the main interface of Android, used to display and test the code written above
-----------------------------------------
Code implementation:
Aggregate Interface
  
/**
* Declare an Iterator method to create an Iterator that can correspond to aggregation.
* If You Want To increment, traverse, or check an aggregation one by one, you can use the iterator method to create
* Class object instance that implements the Iterator interface.
* @ Author terry
*
*/
Public interface Aggregate {
Public abstract Iterator iterator ();
}
 
Iterator Interface
 
Public interface Iterator {

Public abstract boolean hasNext (); // returns true if one exists.

Public abstract Object next (); // move to the next record
}
 
The next method returns one element in the aggregate. However, this is not the only function of the next method. In fact, it will quietly go to the next step in the internal state so that the next element can be indeed returned when the next method is called. Iterator only describes two methods. The implementation of these two methods depends on the internal implementation of BookShelfIterator.
 
Book class
 
/**
* Indicates the book class.
* @ Author terry
*
*/
Public class Book {

Private String name = "";
Public Book (String name ){
This. name = name;
}

Public String getName (){
Return this. name;
}
}
 
BookShelf class
 
/**
* Class indicating the intent of the bookshelf
* The Aggreagate interface must be implemented to treat this class as aggregation.
* @ Author terry
*
*/
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 BookShelf appendBook (Book book ){
This. books [last] = book;
Last ++;
Return this;
}
Public int getLength (){
Return this. last;
}



@ Override
Public Iterator iterator (){
// TODO Auto-generated method stub
Return new BookShelfIterator (this );
}

}
BookShelf implements the Aggregate interface. This method generates and returns the object instance of the BookShelfIterator class. That is, when we send a command to scan the shelf, we will return the "aggregation.
 
BookShelfIterator class
 
1 /**
2 * perform the "Aggregation" class for scanning bookshelves
3 * @ author Administrator
4 *
5 */
6 public class BookShelfIterator implements Iterator {
7
8 private BookShelf bookShelf;
9 private int index;
10
11 public BookShelfIterator (BookShelf bookShelf ){
12 this. bookShelf = bookShelf;
13 this. index = 0;
14}
15
16
17 @ Override
18 public boolean hasNext (){
19 // TODO Auto-generated method stub
20 if (index <bookShelf. getLength ()){
21 return true;
22} else {
23
24 return false;
25}
26
27}
28
29 @ Override
30 public Object next (){
31 // TODO Auto-generated method stub
32 Book book = bookShelf. getBookAt (index );
33 index ++;
34
35 return book;
36}
37
38}
 
After writing the above series of code, we can use the Iterator above on the main interface of Android to traverse the above requirements.
 
IteratorPatternsActivity class
 
Public class IteratorPatternsActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );


EditText et = (EditText) findViewById (R. id. EditText01 );


BookShelf bookShelf = new BookShelf (5 );

BookShelf. appendBook (new Book ("android developer "))
. AppendBook (new Book ("Windows Phone 7 developer "))
. AppendBook (new Book ("IOS developer "))
. AppendBook (new Book ("BADA developer "))
. AppendBook (new Book ("Lion developer "));


Iterator iterator = bookShelf. iterator ();
While (iterator. hasNext ()){
Book book = (Book) iterator. next ();
Et. setText (et. getText () + "" + book. getName ());
}

}
}
 
The preceding execution result:


More function extensions
The above is just about two features, hasNext () and Next () methods. Think about how convenient it will be to expand on this basis, for example:
Backward Traversal
Bidirectional traversal after and after (Next, Previous ......)
Specify a subscript to get it immediately
And so on.

Related Article

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.