Iterator pattern
Iterator pattern (Iterator Pattern) is a Java and .Net programming environment very common design pattern. This mode is used for sequential access of elements of the set of objects, without knowing the underlying representation of the object set.
Iterator pattern belongs behavioral patterns.
Introduction
It is intended: to provide a method for accessing a sequential polymerization of the respective elements in the object, but without exposing the interior of the object representation.
Mainly to solve: a different way to traverse the entire integration object.
When to use: traversing a polymeric object.
How to solve: the responsibility between the elements of the walk to the iterator, rather than aggregate objects.
The key code: define the interface: hasNext, next.
Application examples: JAVA in the iterator.
Advantages: 1, which supports a different way to traverse aggregate object. 2, simplifying the polymerization iterator class. 3, in the same polymerization may have a plurality of traverse. 4, in an iterative mode, add a new class of polymerizable iterator class and easy, without having to modify the existing code.
Disadvantages: Due to the separation of duties iterative mode and stores data through the data, adding new classes need a corresponding increase in new polymerization iterator class, the class number of pairs increases, which increases the complexity of the system to some extent.
Be used: 1, accessing a content object polymerization without exposing its internal representation. 2, it is necessary to traverse a variety of ways to provide a polymeric object. 3, provides a uniform interface for traversing different polymeric structure.
Note: iterator pattern is the separation of the traversal behavior collection of objects, abstract an iterator class to be responsible, so that both can be done without exposing the internal structure of the collection, but also allow external code transparent access to internal data collection.
achieve
We will create a navigation method described Iterator interfaces and a returned iterator Container interface. Realized Container entity class interface will be responsible for implementing the Iterator interface.
IteratorPatternDemo , our demonstration classes use the entity class NamesRepository to print NamesRepository stored as a set of Names .
step 1
Create an interface:
Iterator.java
public boolean hasNext();
public Object next();
}
Container.java
public Iterator getIterator();
}
Step 2
Creating achieved Container entity class interface. This class has achieved Iterator internal class interface NameIterator .
NameRepository.java
public String names[] = {"Robert" , "John" ,"Julie" , "Lora"};
@Override
public Iterator getIterator() {
return new NameIterator();
}
private class NameIterator implements Iterator {
int index;
@Override
public boolean hasNext() {
if(index < names.length){
return true;
}
return false;
}
@Override
public Object next() {
if(this.hasNext()){
return names[index++];
}
return null;
}
}
}
Step 3
Use NameRepository to get iterators, and print the names.
IteratorPatternDemo.java
public static void main(String[] args) {
NameRepository namesRepository = new NameRepository();
for(Iterator iter = namesRepository.getIterator(); iter.hasNext();){
String name = (String)iter.next();
System.out.println("Name : " + name);
}
}
}
Step 4
The implementation of the program, output:
Name : Robert Name : John Name : Julie Name : Lora