Home > Article > Backend Development > What is iterator in php
What is the iterator pattern
Definition: Provides a method to sequentially access the individual elements of an aggregate object without exposing the internal display of the object.
The following is the iterator pattern structure diagram:
The following sample code has a brief understanding:
//抽象聚合类 abstract class Aggregate { public abstract Iterator CreateIterator(); } //具体聚合类 class ConcreteAggregate : Aggregate { private IList<object> items = new List<object>(); public override Iterator CreateIterator() { return new ConcreteIterator(this); } public int Count { get { return items.Count; } } public object this[int index] { get { return items[index]; } set { items.Insert(index, value); } } } //抽象迭代器类 abstract class Iterator { public abstract object First(); public abstract object Next(); public abstract bool IsDone(); public abstract object CurrentItem(); } //具体迭代器类 class ConcreteIterator : Iterator { private ConcreteAggregate aggregate; private int Current = 0; public ConcreteIterator(ConcreteAggregate aggregate) { this.aggregate = aggregate; } public override object CurrentItem() { return aggregate[Current]; } public override object First() { return aggregate[0]; } public override bool IsDone() { return Current >= aggregate.Count ? true : false; } public override object Next() { object ret = null; Current++; if(Current<aggregate.Count) { ret = aggregate[Current]; } return ret; } } static void Main(string[] args) { ConcreteAggregate aggregate = new ConcreteAggregate(); aggregate[0] = "michael"; aggregate[1] = "jarle"; aggregate[2] = "cumming"; aggregate[3] = "andy"; ConcreteIterator iterator = new ConcreteIterator(aggregate); iterator.First(); while (!iterator.IsDone()) { Console.WriteLine($"{iterator.CurrentItem()},请打卡后再下班"); iterator.Next(); } Console.Read(); }
Analysis: Think about it, if we put the collection object and the operation on the collection object together, when we want to traverse the elements in the collection object in another way, we need to modify the collection object, which violates the "single responsibility principle", and the iterator The pattern separates the data structure and the algorithm of the data structure, so that the two can be developed independently.
Advantages:
1. Supports multiple traversal methods. For example, in an ordered list, we provide two iterators for forward traversal and reverse order traversal as needed. Users only need to get our iterator to perform traversal operations on the collection
2. Simplified aggregate classes. Due to the introduction of iterators, the original collection object does not need to traverse the collection elements by itself
3. It is very convenient to add new aggregation classes and iterator classes, and the two dimensions can be changed independently
4. Provide a unified interface for different collection structures, thereby supporting the same algorithm to operate on different collection structures
Disadvantages:
1. Iteration The iterator pattern separates the responsibilities of storing data and traversing data. When adding a new collection object, you need to add a corresponding iterator class. The number of classes increases in pairs, which increases the system complexity to a certain extent
Usage scenarios:
1. Access the contents of an aggregate object without exposing its internal display
2. Need to provide multiple traversal methods for the aggregate object
3 .Provide a unified interface for traversing different aggregate structures
The above is the detailed content of What is iterator in php. For more information, please follow other related articles on the PHP Chinese website!