Design Pattern Learning Notes (13)-iterator iterator mode

Source: Internet
Author: User
Tags abstract int size iterable

Iterator mode definition:

Provides a method to sequentially access the elements of an aggregate object without exposing the internal representation of that object.

This pattern has been implemented in the Java class Library, in Java, all collection classes implement the Conllection interface, and the Conllection interface inherits the Iterable interface, which has a iterator method, That is, the collection class can be converted into iterator classes using this iterator method, and the Hasnext method in the iterator object is used to determine whether there is a next element, and the next method sequentially obtains the objects in the collection class. Today's interview to design mode, I Leng did not answer up, I faint death, oneself these few days just learn things, oneself also understand, can one to exam time is not forget is to answer inaccurate, oneself also don't know exactly why, hey, sad ... , the following is a structure diagram of the iterator pattern:

The aggregate abstract class in this structure diagram can also be defined as an interface. An example is given below to illustrate the use of iterator patterns.

Import java.util.ArrayList;

Interface iterator<e>{
public void-I ();
public void next ();
Public E CurrentItem ();
public boolean isdone ();
}
Class Concreteiterator<e> implements iterator<e>{

Private concreteaggregate<e> agg;
private int index=0;
private int size=0;

Public Concreteiterator (concreteaggregate<e> aggregate) {
This.agg=aggregate;
this.index=0;
This.size=aggregate.size ();
}

Public E CurrentItem () {
Return agg.getelement (index);
}

public void-A-I () {
index=0;
}

public Boolean isdone () {
if (index>=size) {
return true;
}
return false;
}

public void Next () {
if (index<size) {
index++;
}
}
}
Abstract class aggregate<e>{
Protected abstract iterator createiterator ();
}
Class Concreteaggregate<e> extends aggregate<e>{
Private arraylist<e> arraylist=new arraylist<e> ();
Public iterator Createiterator () {

Return to New concreteiterator<e> (this);
}
public void Add (E o) {
Arraylist.add (o);
}
Public E getelement (int index) {

if (Index<arraylist.size ()) {

Return Arraylist.get (index);

} else {

return null;

}

}

public int size () {

return Arraylist.size ();

}

}

public class Client {

public static void Main (string[] args) {
Concreteaggregate<string> aggregate=new concreteaggregate<string> ();
Aggregate.add ("A");
Aggregate.add ("B");
Aggregate.add ("C");
Aggregate.add ("D");
Aggregate.add ("E");
Iterator Iterator=aggregate.createiterator ();
For (Iterator.first ();! Iterator.isdone (); Iterator.next ()) {
System.out.println (Iterator.currentitem ());
}

}
}

Output results:

A

B

C

D

E

In this example, we create an example from the structure diagram of the iterator pattern, the collection Concreteaggregate we create can hold any type of data, and then use the Createiterator method to convert to iterator object, Use the Iterator object to display the contents of the collection sequentially. This pattern is implemented in the Java JDK, so the collection class implements the Iterable interface, which has a iterator method that converts objects of the collection class to objects of the iterator class. Understand this principle to better understand the collection classes and iterators in Java, based on which you can create more powerful collection classes.

Summary: The iterator pattern is primarily used when elements in a collection class change frequently, without changing the client's code.

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.