Iterator pattern and its application in Java API

Source: Internet
Author: User

1. Question:

When we see the collection,list,set,map in Java and other collection classes can be used iterator to traverse the elements, we feel very magical. We cannot help but ask how Java is achieving this goal. This is the topic I am going to talk about today.

According to iterator pattern, I divide the collection classes in Java into four categories:

Iterator (abstract iterator corresponds to Iterator in Java, in fact, Java this Iterator also has the function of abstract aggregation, because it implements iterable this interface, and iterable this interface is an abstract aggregation class. ), Concreteiterator (corresponds to a subclass of two large classes with Collection,map, and the corresponding subclasses of these two large classes. )

Aggregate (abstract aggregation, which corresponds to the Iterable interface in Java, which is essentially a factory class, producing iterator factory)

Concreteraggregate (Specific aggregation, implementation of the aggregate class. This class has a clustered object, how to get the aggregation element, and the number of clustered objects.

2. Brief introduction: Iterator Patten

By classifying the Java collection classes above, it is easy to know that the roles of the Iterator pattern pattern are: Iterator, Concreteiterator, Aggregate, Concreteaggregate, Client (quite to a test class. )

The code for the iterator class is as follows:

Package com.qls.IteratorPattern2;

Public interface Iterator {
void first ();//Element One
void next ();//Next element
Boolean isDone ();//whether to the last element
Object CurrentItem ();//returns the current element
}

The code for the Concreteiterator class is as follows:

Package com.qls.IteratorPattern2;

public class Concreteiterator implements Iterator {
private int index;
private int size;
Private Concreteaggregate concreteaggregate;
Public Concreteiterator (Concreteaggregate concreteaggregate) {
This.concreteaggregate=concreteaggregate;
Size=concreteaggregate.size ();
index=0;
}

@Override
public void First () {
TODO auto-generated Method Stub
index=0;
}

@Override
public void Next () {
TODO auto-generated Method Stub
if (index<size) {
index++;
}
}

@Override
public Boolean isDone () {
TODO auto-generated Method Stub
Return index>=size;
}

@Override
Public Object CurrentItem () {
TODO auto-generated Method Stub
Return concreteaggregate.getelement (index);
}

}

The code for aggregate is as follows:

Package com.qls.IteratorPattern2;

Public interface Aggregate {
Iterator Createiterator ();
}

The code for Concreteaggregate is as follows:

Package com.qls.IteratorPattern2;

public class Concreteaggregate implements Aggregate {
Clustered objects
Private object[] obj={"1", "2", "3", "4", "5", "6"};

@Override
Public Iterator Createiterator () {
TODO auto-generated Method Stub
return new Concreteiterator (this);//this representative: Concreteaggregate
}
/**
* Returns the size of the aggregation.
* @return
*/
public int size () {
return obj.length;
}
/**
* Get the aggregation element
* @param the subscript for the index aggregation element.
*/
Public Object getelement (int index) {
if (index<obj.length) {
return Obj[index];
}else{
return null;
}
}
}

The code for the client is as follows:

Package com.qls.IteratorPattern2;

/**
* Print out the aggregation elements.
* @author Quinlinson
*
*/
public class Client {
Private concreteaggregate concreteaggregate=new concreteaggregate ();
Private Iterator it;
public void operation () {
it = Concreteaggregate.createiterator ();
while (!it.isdone ()) {
System.out.println (It.currentitem ());
It.next ();
}
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
New Client (). operation ();
}

}

3. Summary:

By a simple understanding of iterator pattern, we can eliminate the mystery of the iterator, its implementation is simply: there is a Createiterator () method in the aggregate class to generate iterator. The cluster object's

The element is given to this iterator. An array of object[] when the clustered object is in the above example.

Iterator pattern and its application in Java API

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.