Iterator Pattern

  • Behavioral pattern
  • The intent of this pattern is to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  •  Iterator:- defines an interface for accessing and traversing elements
  • ConcreteAggregator :- implements the iterator creation interface to return an instance of the proper ConcreteIterator
  • ConcreteIterator:- implements the Iterator interface and keeps track of the current position in the traversal of the Aggregate.
  • Aggregate defines an interface for creating an Iterator object.
				
					
namespace Iterator_Pattern
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Employee(int employeeId, string firstName, string lastName)
        {
            EmployeeId = employeeId;
            FirstName = firstName;
            LastName = lastName;
        }
    }

    public interface IEmployeeIterator
    {
        Employee First();
        Employee Next();
        bool IsDone { get; }
        Employee CurrentItem { get; }
    }

    public interface IEmployeeCollection
    {
        IEmployeeIterator CreateIterator();
    }

    public class EmployeeCollection : List<Employee>, IEmployeeCollection
    {
        public IEmployeeIterator CreateIterator()
        {
            return new EmployeeIterator(this);
        }
    }

    public class EmployeeIterator : IEmployeeIterator
    {

        private EmployeeCollection _employeeCollection;
        private int _current = 0;
        public EmployeeIterator(EmployeeCollection employeeCollection)
        {
            _employeeCollection = employeeCollection;
        }

        public bool IsDone => _current >= _employeeCollection.Count;

        public Employee CurrentItem => _employeeCollection.OrderBy(e => e.EmployeeId).ToList()[_current];

        public Employee First()
        {
            _current = 0;
            return _employeeCollection.OrderBy(e => e.EmployeeId).ToList()[_current];
        }

        public Employee Next()
        {
            _current++;

            if (!IsDone)
            {
                return _employeeCollection.OrderBy(e => e.EmployeeId).ToList()[_current];

            }
            else
            {
                return null;
            }
        }
    }
}

				
			
				
					using Iterator_Pattern;

EmployeeCollection emp = new EmployeeCollection();
emp.Add(new Employee(1, "Anurag", "Nayak"));
emp.Add(new Employee(2, "Abhishek", "Nayak"));
emp.Add(new Employee(3, "Shalin", "Bhanot"));
emp.Add(new Employee(4, "Manish", "Singh"));

var employeeIterator = emp.CreateIterator();

for (Employee employee = employeeIterator.First(); !employeeIterator.IsDone;
    employee = employeeIterator.Next())
{
    Console.WriteLine(employee?.FirstName + " " + employee?.LastName);
}
				
			

Anurag Nayak
Abhishek Nayak
Shalin Bhanot
Manish Singh

Leave a Comment