Iterator pattern

Iterator

Container

Iterator

Container

Iterator

Container

Iterator

Container

Iterator

Container

Iterator

Container

Iterator

Container

Iterator

Container

Iterator

Container

logic <=> container

decoupled

//normal approach

 

 

 

let myArray = [1, 2, 3, 4, 5];

 

for ( let i = 0 ;  i < myArray.length  ; i++ ) {

     console.log( myArray[i] );

}

Iterator interface

  • hasNext()
     
  • next()

//normal approach

 

let myArray = [1, 2, 3, 4, 5];

 

for ( let i = 0 ;  i < myArray.length  ; i++ ) {

     console.log( myArray[i] );

}

//iterator approach

 

 

 

let myArray = new IterableList ( 1, 2, 3, 4, 5 );

 

let iterator = myArray.iterator();

 






while( iterator.hasNext( ) ){

    console.log( iterator.next( ) );

}

Made with Slides.com