The iterator pattern is for collection objects, and for collection objects, it is certain that adding and removing operations to the collection, and certainly supporting the traversal of the collection elements, we can put the traversal in the collection object, but then the collection object takes too much responsibility, One of the principles of object-oriented design is the principle of single responsibility, all of which we want to separate as much as possible from these responsibilities, taking different responsibilities with different classes, and the iterator pattern is to assume the responsibility of iterating over the set with the iterator class.
- Definition of the iterator pattern
The iterator pattern provides a way to sequentially access individual elements in an aggregated object without exposing the object's internal implementation, so that the internal structure of the collection is not exposed and external code transparently accesses the data inside the collection
- Structure diagram of the iterator pattern
- The composition of the iterator pattern
- Iterator role (Iterator): The iterator role is responsible for defining the interfaces for access and traversal
- Specific iterator role (concrete Iterator): The specific iterator role implements the iterator interface and needs to record the current position in the traversal.
- Aggregation Role (Aggregate): The aggregation role is responsible for defining the interface that gets the iterator role
- Specific aggregation roles (concreteaggregate): Aggregate role implementation aggregation role interfaces
Iterator Code implementation
classprogram {Static voidMain (string[] args) {Concreteaggregate a =NewConcreteaggregate (); A[0] = "a"; A[1] = "b"; A[2] = "C"; A[3] = "D"; A[4] = "e"; A[5] = "F"; A[6] = "g"; Iterator i =NewConcreteiterator (a);Objectitem = I.first (); while(!i.isdone ()) {Console.WriteLine (I.currentitem ()); I.next (); } } }/// Iterator interface // </summary> Public Abstract classIterator { Public Abstract ObjectFirst (); Public Abstract ObjectNext (); Public Abstract BOOLIsDone (); Public Abstract ObjectCurrentItem (); }//Aggregation abstract class Public Abstract classAggregate { Public AbstractIterator Createiterator (); }// <summary> /// Concrete abstract class // </summary> Public classconcreteaggregate:aggregate {Privateilist<Object> items =Newlist<Object> (); Public OverrideIterator Createiterator () {return NewConcreteiterator ( This); } Public intCount {Get{returnItems. Count; } } Public Object This[intIndex] {Get{returnItems[index]; }Set{Items. Insert (Index,value); } } } Public classConcreteiterator:iterator {PrivateConcreteaggregate aggregate;Private intcurrent = 0; PublicConcreteiterator (concreteaggregate aggregate) { This. aggregate = aggregate; } Public Override ObjectFirst () {returnAGGREGATE[0]; } Public Override ObjectNext () {ObjectRET =NULL; current++;if(Current<aggregate. Count) {ret = aggregate[current]; }returnRet } Public Override BOOLIsDone () {returnCurrent >= aggregate. Count?true:false; } Public Override ObjectCurrentItem () {returnAggregate[current]; } }View CodeThe application of the iterator pattern in net
There is a namespace in the mscorlib assembly that is: System.Collections, which has already implemented the iterator pattern in the namespace. For the aggregation interface and iterator interface already exists, where IEnumerator plays the role of an iterator, it is implemented as follows:
Public Interface IEnumerator { object current { get; } BOOL MoveNext (); void Reset (); }
The Reset () method returns the value of the element in the current collection, the MoveNext () method reverts to the position, and true indicates that the iterator successfully advances to the next element in the collection, and the return value of FALSE indicates that it is already at the end of the collection. A collection object capable of providing element traversal, with the IEnumerator interface implemented in. NET.
IEnumerable is the role of abstract aggregation , there is only one GetEnumerator () method, if the collection object needs to have the ability to traverse, it must implement the interface.
Public Interface ienumerable{ ieumerator GetEnumerator ();}
Abstract aggregation roles (Aggregate) and abstract iterator roles (Iterator) are IEnumerable interfaces and IEnumerator interfaces, respectively, with a queue type for the specific aggregation role (concreteaggregate). BitArray and other types
Advantages and disadvantages of the iterator pattern
Because iterators assume the responsibility of traversing collections, the following advantages are available:
- The iterator pattern allows access to the contents of an aggregated object without exposing its internal representation, which is an iterative abstraction.
- The iterator pattern provides a unified interface for traversing different collection structures, enabling the same algorithm to operate on different sets of structures
The flaw in the iterator pattern:
- An iterator pattern that changes the collection structure in which iterators are traversed causes an exception to occur. So using a foreach statement can only iterate over the collection and not change the elements in the collection while traversing.
Summarize
An iterator pattern is the abstraction of an iterator class to detach the traversal behavior of a collection object, so that it does not expose the internal structure of the collection, but also allows external code to transparently access the data inside the collection
Today I see a sentence that makes sense, and then share it to everyone--our life today is decided by 35 years ago, and 35 years later, life is decided by today! Good night