Iterator in VB.NET
Sequentially access the elements of a collection
ReturnProvide 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
- Abstract Factory in VB.NET - Creates an instance of several families of classes.
- Builder in VB.NET - Separates object construction from its representation.
- Factory Method in VB.NET - Creates an instance of several derived classes.
- Prototype in VB.NET - A fully initialized instance to be copied or cloned.
- Singleton in VB.NET - A class of which only a single instance can exist.
- Adapter in VB.NET - Match interfaces of different classes
- Bridge in VB.NET - Separates an object???s interface from its implementation
- Composite in VB.NET - A tree structure of simple and composite objects
- Decorator in VB.NET - Add responsibilities to objects dynamically
- Facade in VB.NET - A single class that represents an entire subsystem
- Flyweight in VB.NET - A fine-grained instance used for efficient sharing
- Proxy in VB.NET - An object representing another object
- Chain of Resp. in VB.NET - A way of passing a request between a chain of objects
- Command in VB.NET - Encapsulate a command request as an object
- Interpreter in VB.NET - A way to include language elements in a program
- Iterator in VB.NET - Sequentially access the elements of a collection
- Mediator in VB.NET - Defines simplified communication between classes
- Memento in VB.NET - Capture and restore an object's internal state
- Observer in VB.NET - A way of notifying change to a number of classes
- State in VB.NET - Alter an object's behavior when its state changes
- Strategy in VB.NET - Encapsulates an algorithm inside a class
- Template Method in VB.NET - Defer the exact steps of an algorithm to a subclass
- Visitor in VB.NET - Defines a new operation to a class without change
Comments (
)
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> <> |