According to Gang of Four, the iterator pattern provides a process to obtain the aggregator object without knowing its implementation. Use Case Let us take an example of a collection list of cars and string[] an array of motorcycles; we need to design an aggregator object to iterate over the collection without knowing whether it's a list or an array. The helps solve this problem wherein a standard iterator will traverse different collection types. iterator design pattern Prerequisites Basic knowledge of OOPs concepts. Any programming language knowledge. The article demonstrates the usage of iterator design patterns using the C# programming language. Getting Started Considering the above use case, let us define a custom iterator interface that acts as an abstract layer over the list and array iterator. { ; ; ; ; } public interface IVehicleIterator ( ) void First ( ) bool IsDone ( ) string Next ( ) string Current Now write car and motorcycle iterators that implement the above interface according to the use case. CarIterator.cs The car iterator is implemented over collection and provides an implementation of interface methods. List<string> IVehicleIterator { List< > _cars; _current; { _cars = cars; _current = ; } { _cars.ElementAt(_current); } { _current = ; } { _current >= _cars.Count; } { _cars.ElementAt(_current++); } } public : class CarIterator private string private int public CarIterator (List< > cars) string 0 public string Current () return public void First () 0 public bool IsDone () return public string Next () return MotorcycleIterator.cs The motorcycle iterator is implemented over collection and provides an implementation of interface methods. string[] IVehicleIterator { [] _motercylces; _current; { _motercylces = motercylces; _current = ; } { _motercylces[_current]; } { _current = ; } { _current >= _motercylces.Length; } { _motercylces[_current++]; } } public : class MotercycleIterator private string private int public MotercycleIterator ( [] motercylces) string 0 public string Current () return public void First () 0 public bool IsDone () return public string Next () return After all the above iterators are defined, define a standard aggregator object interface that creates iterators. public IVehicleAggregate{ IVehicleIterator CreateIterator(); } interface Finally, write down the classes which implement the above aggregator interface. According to the use-case, both Car and Motorcycle classes will implement the aggregator interface. Car.cs The method of aggregator interface returns the relevant iterator as shown below. IVehicleAggregate { List< > _cars; { _cars = List< > { , , }; } { CarIterator(_cars); } } public : class Car private string public Car () new string "Car 1" "Car 2" "Car 3" IVehicleIterator public CreateIterator () return new Motorcycle.cs The method of aggregator interface returns the relevant iterator as shown below. IVehicleAggregate { [] _motercycles; { _motercycles = [] { , , }; } { MotercycleIterator(_motercycles); } } public : class Motercycle private string public Motercycle () new "Bike 1" "Bike 2" "Bike 3" IVehicleIterator public CreateIterator () return new Iterator Pattern in Action static void { IVehicleAggregate car = Vehicles. ; IVehicleAggregate motercycle = Vehicles. ; IVehicleIterator carIterator = car. ; IVehicleIterator motercycleIterator = motercycle. ; ; ; } static void { iterator. ; (!iterator. ) { Console. ); } } Main( [] ) string args new Car() new Motercycle() CreateIterator() CreateIterator() PrintVehicles( ) carIterator PrintVehicles( ) motercycleIterator PrintVehicles(IVehicleIterator ) iterator First() while IsDone() WriteLine( .Next() iterator The PrintVehicles methods check if then output the collection element. No matter what collection we’re dealing with, implement methods like First, IsDone, and Next. !iterator.isDone Output We don’t know the underlying collection type, but it is still iterated over it via Iterator Design Pattern. If you go ahead and run, it displays the following output: Github Sample Repo Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section. Also published on Medium's subdomain .