Iterator Pattern Iterator pattern is very commonly used

  • Slides: 15
Download presentation
Iterator Pattern

Iterator Pattern

Iterator pattern is very commonly used design pattern in Java and . Net programming

Iterator 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.

Intent Provide a way to access the elements of an aggregate object sequentially without

Intent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Also Known As Cursor

Motivation An aggregate object such as a list should give you a way to

Motivation An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. Moreover, you might want to traverse the list in different ways, depending on what you want to accomplish. But you probably don't want to bloat the List interface with operations for different traversals, even if you could anticipate the ones you will need. You might also need to have more than one traversal pending on the same list. The Iterator pattern lets you do all this. The key idea in this pattern is to take the responsibility for access and traversal out of the list object and put it into an iterator object. The Iterator class defines an interface for accessing the list's elements. An iterator object is responsible for keeping track of the current element; that is, it knows which elements have been traversed already.

Applicability Use the Iterator pattern: Øto access an aggregate object's contents without exposing its

Applicability Use the Iterator pattern: Øto access an aggregate object's contents without exposing its internal representation. Øto support multiple traversals of aggregate objects. Ø to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).

Participants: Ø Iterator o defines an interface for accessing and traversing elements. ØConcrete. Iterator

Participants: Ø Iterator o defines an interface for accessing and traversing elements. ØConcrete. Iterator o implements the Iterator interface. o keeps track of the current position in the traversal of the aggregate. Ø Aggregate o defines an interface for creating an Iterator object. ØConcrete. Aggregate o implements the Iterator creation interface to return an instance of the proper Concrete. Iterator.

Collaborations · A Concrete. Iterator keeps track of the current object in the aggregate

Collaborations · A Concrete. Iterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal.

Consequences The Iterator pattern has three important consequences: 1. It supports variations in the

Consequences The Iterator pattern has three important consequences: 1. It supports variations in the traversal of an aggregate. 2. Iterators simplify the Aggregate interface. 3. More than one traversal can be pending on an aggregate.

Implementation We're going to create a Iterator interface which narrates navigation method and a

Implementation We're going to create a Iterator interface which narrates navigation method and a Container interface which returns the iterator . Concrete classes implementing the Container interface will be responsible to implement Iterator interface and use it Iterator. Pattern. Demo, our demo class will use Names. Repository, a concrete class implementation to print a Names stored as a collection in Names. Repository.

Step 1 Create interfaces. Iterator. java public interface Iterator { public boolean has. Next();

Step 1 Create interfaces. Iterator. java public interface Iterator { public boolean has. Next(); public Object next(); } Container. java public interface Container { public Iterator get. Iterator(); }

Step 2 Create concrete class implementing the Container interface. This class has inner class

Step 2 Create concrete class implementing the Container interface. This class has inner class Name. Iterator implementing the Iterator interface. Name. Repository. java public class Name. Repository implements Container { public String names[] = {"Robert" , "John" , "Julie" , "Lora"}; @Override public Iterator get. Iterator() { return new Name. Iterator(); } private class Name. Iterator implements Iterator { int index; @Override public boolean has. Next()

{ if(index < names. length) { return true; } return false; } @Override public

{ if(index < names. length) { return true; } return false; } @Override public Object next() { if(this. has. Next()) { return names[index++]; } return null; } } }

Step 3 Use the Name. Repository to get iterator and print names. Iterator. Pattern.

Step 3 Use the Name. Repository to get iterator and print names. Iterator. Pattern. Demo. java public class Iterator. Pattern. Demo { public static void main(String[] args) { Name. Repository names. Repository = new Name. Repository(); for(Iterator iter = names. Repository. get. Iterator(); iter. has. Next(); ) { String name = (String)iter. next(); System. out. println("Name : " + name); } } }

Step 4 Verify the output. Name : Robert Name : John Name : Julie

Step 4 Verify the output. Name : Robert Name : John Name : Julie Name : Lora