Iterator – It let the programs traverse all of the collection elements without their underlying representation and can non-know the set objects base layer to describe it.
From Design Pattern – Behavioral Patterns
Structure
Applicability
It lets the program travers elements of a collection without exposing its underlying representation, such as list, stack, tree, etc.
Pros and Cons
PROS
CONS
How to Implement
This pattern article will provide two basic sample codes, such as the abstract class’s iterator pattern and the interface class’s iterator pattern. The abstract class and the interface class have a few parts working differently based on the object class characteristic factor in the .NET framework. If the developers don’t know the abstract class mechanism, the author will provide you with advice on choosing the interface class mechanism.
#1 Basic Sample
Abstract class sample
static void Main(string[] args)
{
var collection = new WordsCollection();
collection.AddItem("First");
collection.AddItem("Second");
collection.AddItem("Third");
Console.WriteLine("Straight traversal:");
foreach (var element in collection)
{
Console.WriteLine(element);
}
Console.WriteLine("\nReverse traversal:");
collection.ReverseDirection();
foreach (var element in collection)
{
Console.WriteLine(element);
}
Console.ReadKey();
}
abstract class Iterator : IEnumerator
{
object IEnumerator.Current => Current();
public abstract int Key();
public abstract object Current();
public abstract bool MoveNext();
public abstract void Reset();
}
abstract class IteratorAggregate : IEnumerable
{
public abstract IEnumerator GetEnumerator();
}
class AlphabeticalOrderIterator : Iterator
{
private WordsCollection _collection;
private int _position = -1;
private bool _reverse = false;
public AlphabeticalOrderIterator(WordsCollection collection, bool reverse = false)
{
this._collection = collection;
this._reverse = reverse;
if (reverse)
{
this._position = collection.getItems().Count;
}
}
public override object Current()
{
return this._collection.getItems()[_position];
}
public override int Key()
{
return this._position;
}
public override bool MoveNext()
{
int updatedPosition = this._position + (this._reverse ? -1 : 1);
if (updatedPosition >= 0 && updatedPosition < this._collection.getItems().Count)
{
this._position = updatedPosition;
return true;
}
else
{
return false;
}
}
public override void Reset()
{
this._position = this._reverse ? this._collection.getItems().Count - 1 : 0;
}
}
class WordsCollection : IteratorAggregate
{
List<string> _collection = new List<string>();
bool _direction = false;
public void ReverseDirection()
{
_direction = !_direction;
}
public List<string> getItems()
{
return _collection;
}
public void AddItem(string item)
{
this._collection.Add(item);
}
public override IEnumerator GetEnumerator()
{
return new AlphabeticalOrderIterator(this, _direction);
}
} Interface class sample
static void Main(string[] args)
{
var collection2 = new WordsCollection2();
collection2.AddItem("First");
collection2.AddItem("Second");
collection2.AddItem("Third");
Console.WriteLine("Straight traversal:");
foreach (var element in collection2)
{
Console.WriteLine(element);
}
Console.WriteLine("\nReverse traversal:");
collection2.ReverseDirection();
foreach (var element in collection2)
{
Console.WriteLine(element);
}
Console.ReadKey();
}
interface Iterator2 : IEnumerator
{
int Key();
object Current();
bool MoveNext();
void Reset();
}
interface IteratorAggregate2 : IEnumerable
{
new IEnumerator GetEnumerator();
}
class AlphabeticalOrderIterator2 : Iterator2
{
object IEnumerator.Current => Current();
private WordsCollection2 _collection;
private int _position = -1;
private bool _reverse = false;
public AlphabeticalOrderIterator2(WordsCollection2 collection, bool reverse = false)
{
this._collection = collection;
this._reverse = reverse;
if (reverse)
{
this._position = collection.getItems().Count;
}
}
public object Current()
{
return this._collection.getItems()[_position];
}
public int Key()
{
return this._position;
}
public bool MoveNext()
{
int updatedPosition = this._position + (this._reverse ? -1 : 1);
if (updatedPosition >= 0 && updatedPosition < this._collection.getItems().Count)
{
this._position = updatedPosition;
return true;
}
else
{
return false;
}
}
public void Reset()
{
this._position = this._reverse ? this._collection.getItems().Count - 1 : 0;
}
}
class WordsCollection2 : IteratorAggregate2
{
List<string> _collection = new List<string>();
bool _direction = false;
public void ReverseDirection()
{
_direction = !_direction;
}
public List<string> getItems()
{
return _collection;
}
public void AddItem(string item)
{
this._collection.Add(item);
}
IEnumerator IteratorAggregate2.GetEnumerator()
{
return new AlphabeticalOrderIterator2(this, _direction);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new AlphabeticalOrderIterator2(this, _direction);
}
} Page: 1 2
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…
View Comments