GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Design Patterns - Iterator Pattern
Creational Design Patterns
Structural Design Patterns
Behavioral Design Patterns
J2EE Design Patterns
Design Patterns Useful Resources
Selected Reading
Design Patterns - Iterator PatternIterator pattern is very commonly used design pattern in Java and .Net programming environment. This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation. Iterator pattern falls under behavioral pattern category. ImplementationWe're going to create a Iterator interface which narrates navigation method and a Container interface which retruns the iterator . Concrete classes implementing the Container interface will be responsible to implement Iterator interface and use it IteratorPatternDemo, our demo class will use NamesRepository, a concrete class implementation to print a Names stored as a collection in NamesRepository.
Step 1Create interfaces. Iterator.javapackage com.tutorialspoint;
public interface Iterator {
public boolean hasNext();
public Object next();
}
Container.javapackage com.tutorialspoint;
public interface Container {
public Iterator getIterator();
}
Step 2Create concrete class implementing the Container interface. This class has inner class NameIterator implementing the Iterator interface. NameRepository.javapackage com.tutorialspoint;
public class NameRepository implements Container {
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;
}
}
}
Example - Usage of Iterator PatternUse the NameRepository to get iterator and print names. IteratorPatternDemo.javapackage com.tutorialspoint;
public class IteratorPatternDemo {
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);
}
}
}
interface Iterator {
public boolean hasNext();
public Object next();
}
interface Container {
public Iterator getIterator();
}
class NameRepository implements Container {
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;
}
}
}
OutputVerify the output. Name : Robert Name : John Name : Julie Name : Lora Advertisements
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |