(NET) NET (2016)

Iterator in VB.NET

Sequentially access the elements of a collection

Return


Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Iterator pattern provides for a way to traverse (iterate) over a collection of items without detailing the underlying structure of the collection.







Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern



   1:      ' Iterator Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/Iterator.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              Dim a As New ConcreteAggregate()
   7:              a(0) = "Item A"
   8:              a(1) = "Item B"
   9:              a(2) = "Item C"
  10:              a(3) = "Item D"
  11:              ' Create Iterator and provide aggregate
  12:              Dim i As Iterator = a.CreateIterator()
  13:              Console.WriteLine("Iterating over collection:")
  14:              Dim item As Object = i.First()
  15:              While item IsNot Nothing
  16:                  Console.WriteLine(item)
  17:                  item = i.[Next]()
  18:              End While
  19:              ' Wait for user
  20:              Console.ReadKey()
  21:          End Sub
  22:      End Class
  23:   
  24:      ' The 'Aggregate' abstract class
  25:      MustInherit Class Aggregate
  26:          Public MustOverride Function CreateIterator() As Iterator
  27:      End Class
  28:   
  29:      ' The 'ConcreteAggregate' class
  30:      Class ConcreteAggregate
  31:          Inherits Aggregate
  32:          Private _items As New ArrayList()
  33:          Public Overrides Function CreateIterator() As Iterator
  34:              Return New ConcreteIterator(Me)
  35:          End Function
  36:          ' Gets item count
  37:          Public ReadOnly Property Count() As Integer
  38:              Get
  39:                  Return _items.Count
  40:              End Get
  41:          End Property
  42:          ' Indexer
  43:          Default Public Property Item(index As Integer) As Object
  44:              Get
  45:                  Return _items(index)
  46:              End Get
  47:              Set(value As Object)
  48:                  _items.Insert(index, value)
  49:              End Set
  50:          End Property
  51:      End Class
  52:   
  53:      ' The 'Iterator' abstract class
  54:      MustInherit Class Iterator
  55:          Public MustOverride Function First() As Object
  56:          Public MustOverride Function [Next]() As Object
  57:          Public MustOverride Function IsDone() As Boolean
  58:          Public MustOverride Function CurrentItem() As Object
  59:      End Class
  60:   
  61:      ' The 'ConcreteIterator' class
  62:      Class ConcreteIterator
  63:          Inherits Iterator
  64:          Private _aggregate As ConcreteAggregate
  65:          Private _current As Integer = 0
  66:          ' Constructor
  67:          Public Sub New(aggregate As ConcreteAggregate)
  68:              Me._aggregate = aggregate
  69:          End Sub
  70:          ' Gets first iteration item
  71:          Public Overrides Function First() As Object
  72:              Return _aggregate(0)
  73:          End Function
  74:          ' Gets next iteration item
  75:          Public Overrides Function [Next]() As Object
  76:              Dim ret As Object = Nothing
  77:              If _current < _aggregate.Count - 1 Then
  78:                  _current = _current - 1
  79:                  ret = _aggregate(_current)
  80:              End If
  81:              Return ret
  82:          End Function
  83:          ' Gets current iteration item
  84:          Public Overrides Function CurrentItem() As Object
  85:              Return _aggregate(_current)
  86:          End Function
  87:          ' Gets whether iterations are complete
  88:          Public Overrides Function IsDone() As Boolean
  89:              Return _current >= _aggregate.Count
  90:          End Function
  91:      End Class




See also:
Creational Patterns Structural Patterns Behavioral Patterns

Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/ProgramTheory/Iterator.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  <>