Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
TypeScript Design Patterns

You're reading from  TypeScript Design Patterns

Product type Book
Published in Aug 2016
Publisher Packt
ISBN-13 9781785280832
Pages 256 pages
Edition 1st Edition
Languages
Author (1):
Vilic Vane Vilic Vane
Profile icon Vilic Vane

Iterator Pattern


Iterator Pattern provides a universal interface for accessing internal elements of an aggregate without exposing the underlying data structure. A typical iterator contains the following methods or getters:

  • first(): moves the cursor to the first element in the aggregates

  • next(): moves the cursor to the next element

  • end: a getter that returns a Boolean indicates whether the cursor is at the end

  • item: a getter that returns the element at the position of the current cursor

  • index: a getter that returns the index of the element at the current cursor

Iterators for aggregates with different interfaces or underlying structures usually end with different implementations as shown in the following figure:

Though the client does not have to worry about the structure of an aggregate, an iterator would certainly need to. Assuming we have everything we need to build an iterator, there could be a variety of ways for creating one. The factory method is widely used when creating iterators...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}