Iterator design mode

Source: Internet
Author: User

 PackageIterator;ImportJava.util.*;/**implements the interface that creates the specific iterator role, which is related to the structure of the container **/ Public classConcretecontainerImplementsContainer {PrivateVector vector =NULL;  PublicVector Getvector () {returnVector; }     Public voidsetvector (vector vector) { This. Vector =Vector; } @Override PublicIterator Createiterator () {//TODO auto-generated Method Stub        return Newconcreteiterator (vector); }     PublicConcretecontainer () {vector=NewVector (); Vector.add ("Vector 1"); Vector.add ("Vector 2"); Vector.add ("Vector 3"); }}
 PackageIterator;/**implement the Iterator interface and record the current position in the traversal **/ImportJava.util.*; Public classConcreteiteratorImplementsIterator {Private intCurrentindex = 0; PrivateVector vector =NULL;  PublicConcreteiterator (Finalvector vector) {         This. Vector =Vector; } @Override PublicObject First () {//TODO auto-generated Method StubCurrentindex = 0; returnVector.get (Currentindex); } @Override PublicObject Next () {//TODO auto-generated Method Stubcurrentindex++; returnVector.get (Currentindex); } @Override PublicObject Currenitem () {//TODO auto-generated Method Stub        returnVector.get (Currentindex); } @Override Public BooleanIsDone () {//TODO auto-generated Method Stub        if(Currentindex >= This. Vector.size ()-1) {            return true; } Else {            return false; }} @Override Public intcount () {//TODO auto-generated Method Stub        return0; }}
 Package Iterator; /** responsible for providing the interface that creates the specific iterator role * */  Public Interface Container {    public  Iterator createiterator ();}
 package   Iterator;  /**   */ public  interface   Iterator {  Object first ();     public   Object next ();     public   Object Currenitem ();  public  boolean   IsDone ();  public  int   
 PackageJTest;Import Staticorg.junit.assert.*;Importorg.junit.Test;ImportIterator.concretecontainer;ImportIterator.container;ImportIterator.iterator; Public classiteratortest {@Test Public voiditeratortest () {FinalContainer Container =NewConcretecontainer (); FinalIterator Iterator =Container.createiterator ();        System.out.println (Iterator.first ());  while(!Iterator.isdone ())        {System.out.println (Iterator.next ()); }    }}

Http://www.cnblogs.com/itTeacher/archive/2012/12/03/2799534.html
The iterator pattern has a pattern called cursor. Gof gives the definition of providing a way to access individual elements in a container (container) object without exposing the internal details of the object.

The iterator pattern consists of the following roles:
Iterator role (Iterator): the interface responsible for defining access and traversal elements.
Specific iterator role (concrete Iterator): Implements the iterator interface and records the current position in the traversal.
Container Role (Container): is responsible for providing the interface that creates the specific iterator role.
Specific container role (concrete Container): Implements the interface that creates the specific iterator role, which is related to the structure of the container.


Advantages and disadvantages of the iterator pattern
The advantages of the iterator pattern are:
The traversal method is simplified, and the traversal of the object collection is cumbersome, and we can still get the array or the list with the cursor, but the user needs to iterate over the object on the premise that the set is well understood.
But for the hash table, the user is more troublesome to traverse. With the introduction of the iterator method, the user is much simpler to use.
can provide a variety of traversal methods, such as the sequence of the table, we can provide a positive sequence traversal, reverse-traverse the two iterators, the user only need to get our implementation of a good iterator, we can easily traverse the collection.
Encapsulation is good, users only need to get iterators to traverse, and for the traversal algorithm is not to care.

Disadvantages of the iterator pattern:
For a simpler traversal (like an array or a sequence table), it's tedious to iterate with an iterator, and everyone might feel like ArrayList, and we'd rather use the for loop and get methods to iterate through the collection.

Applicable scenarios for the iterator pattern
The iterator pattern is associated with the collection, in general, as long as we implement a set, we need to provide an iterator to this collection, like Collection,list, set, map, etc. in Java, these collections have their own iterators. If we were to implement a new container like this, we would certainly need to introduce an iterator pattern to implement an iterator to our container.
However, because the container is too closely related to iterators, most languages provide iterators when implementing the container, and the containers and iterators provided by those languages can meet our needs in the vast majority of cases, so it is still relatively uncommon for us to practice the scenario of the iterator pattern ourselves. We just need to use the containers and iterators that are already in the language.

Iterator design mode

Related Article

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.