Iterator design pattern (the Iterator) and code examples Detailed

First, the definition of the pattern and characteristics

  Iterator is defined (the Iterator) mode: Iterator behavioral pattern is an object to provide a sequential access object to a series of data objects in the polymerization, the polymerization without exposing the internal representation of the object.

Second, the advantages and disadvantages of the iterative mode

  Its main advantages are as follows:

  • Accessing a content object polymerization without exposing its internal representation.
  • Handed over the task to complete traversal iterator, which simplifies the aggregation class.
  • It supports different ways to traverse a polymerization, even customize iterator subclass to support the new traversal.
  • Add a new class of polymeric iterator class and easy, without modifying the original code.
  • Good encapsulation provides a unified interface to traverse different polymeric structure.

  The main disadvantages are:

  • The number of classes increases, which increases the complexity of the system to some extent.

Third, to achieve the iterator pattern

  Iterative mode is isolated by traversing the object out of the polymerization behavior, abstracted into iterator class to achieve its object in the case of an internal structure of the aggregate object is not exposed, so that access to the interior transparent external code data aggregation. Now we have to analyze the basic structure and implementation.

  Iterator pattern mainly includes the following roles.

  • Abstract polymerization (Aggregate) roles: the definition of storage, add, delete, and create interfaces aggregate objects iterator object.
  • Specific polymerization (ConcreteAggregate) Role: implement the abstract class polymerization, it returns an iterator specific examples.
  • Abstract iterator (the Iterator) Role: defining access interface elements and traverse the polymerization, generally comprising hasNext (), first (), next () method and the like.
  • DETAILED iterator (Concretelterator) Role: Abstract iterator interface implementation defined, complete traversal of the aggregate object, recording the current position of the traverse.

  The structure shown in Figure:

           

  Code is implemented as follows:

public class IteratorPattern
{
    public static void main(String[] args)
    {
        Aggregate ag=new ConcreteAggregate(); 
        ag.add ( "Sun Yat-sen" );
        ag.add ( "South China" );
        ag.add ( "Shaoguan University" );
        Of System.out.print ( "aggregated content are:" );
        Iterator it=ag.getIterator(); 
        while(it.hasNext())
        { 
            Object ob=it.next(); 
            System.out.print(ob.toString()+"\t"); 
        }
        Object ob=it.first();
        System.out.println("\nFirst:"+ob.toString());
    }
}
// Abstract polymerization 
interface Aggregate
{ 
    public void add(Object obj); 
    public void remove(Object obj); 
    public Iterator getIterator(); 
}
// specific polymerization 
class ConcreteAggregate the implements Aggregate
{ 
    private List<Object> list=new ArrayList<Object>(); 
    public void add(Object obj)
    { 
        list.add(obj); 
    }
    public void remove(Object obj)
    { 
        list.remove(obj); 
    }
    public Iterator getIterator()
    { 
        return(new ConcreteIterator(list)); 
    }     
}
// abstract iterator 
interface Iterator
{
    Object first();
    Object next();
    boolean hasNext();
}
// specific iterator 
class ConcreteIterator the implements Iterator
{ 
    private List<Object> list=null; 
    private int index=-1; 
    public ConcreteIterator(List<Object> list)
    { 
        this.list=list; 
    } 
    public boolean hasNext()
    { 
        if(index<list.size()-1)
        { 
            return true;
        }
        else
        {
            return false;
        }
    }
    public Object first()
    {
        index=0;
        Object obj=list.get(index);;
        return obj;
    }
    public Object next()
    { 
        Object obj=null; 
        if(this.hasNext())
        { 
            obj=list.get(++index); 
        } 
        return obj; 
    }   
}

  Results are as follows:

Aggregated content are: Shaoguan University, Zhongshan University, South China   
First: Sun Yat-sen

Fourth, the iterative mode of application scenarios

  The structure and characteristics of the front Iterated mode, the following describes the application scenario, normally used in an iterative mode following situations.

  • When it is desired to provide a variety of ways to traverse the aggregate object.
  • When it is desired to provide a unified interface when traversing different polymeric structure.
  • When accessing a content object polymerization without exposing the details of its internal representation.

  Because of polymerization and iterators are very close, so most language classes in achieving polymerization provides an iterator class, using the following language in the case of large numbers of existing polymerization iterator class had enough.

Extended Five mode

  Iterative mode is often used in combination with the combination mode when the combination of the container member access mode, iterator often hidden in the container assembly configuration mode class. Of course, a configuration may also be accessed iterator external container member, the structure shown in FIG. FIG:

              

Guess you like

Origin www.cnblogs.com/jing99/p/12610115.html