iterator pattern definition: Provides a way to access a set of aggregated elements in a sequential manner without exposing the object's internal structure.
Description: First of all, the object using an iterator must be a set of aggregated objects, such as a bus is a set of aggregated objects, the people in the car may have a relationship between each other, it may not matter, they grouped together to form this aggregation object. Also, this order can be in any order, such as from large to small, or from small to large, that is, when an iterator iterates over an object, the next object is determined, and the object is unique. The last is the internal structure, in fact we can only get the value of this object from the outside, but the structure of the aggregation object inside, we are not clear.
iterator class diagram:
Code implementation:
Class Program {static void Main (string[] args) {concreteaggregate a = new Concreteaggregate ( ); Bus, that is, the Gathering object a[0] = "Big Bird"; New passengers, that is, object group a[1] = "side dishes"; A[2] = "baggage"; A[3] = "foreigner"; A[4] = "internal employee"; A[5] = "thief"; A[6] = "personnel within the system"; Iterator i = new Concreteiterator (a); When the conductor comes in, look for those people to get on the bus, that is, declare the iterator object item = I.first (); Starting with the first person while (!i.isdone ())//Judging whether to buy tickets {Console.WriteLine ("{0} Please purchase tickets", I.curr Entitem ()); Let the current object buy tickets i.next (); Next passenger} console.read (); }} abstract class Iterator//Iterator abstraction classes, interface {Public abstract object first (); Define start object public abstract Object Next (); Next object public abstract bool IsDone (); Determine whether to the end public abstract object CurrentItem (); Current Object} class Concreteiterator:iteraTor {private concreteaggregate aggregate; Defines a concrete clustered object private int current = 0; Public Concreteiterator (concreteaggregate aggregate)//Initializes a specific clustered object into {this.aggregate = aggregate; } public override object First ()///get aggregated into the amount of the initial object {return aggregate[0]; public override object Next ()//Gets the next object {object ret = null; current++; if (current < aggregate. Count) {ret = aggregate[current]; } return ret; The public override bool IsDone ()//Determines whether it is convenient to the end and returns true at the end of {return current >= aggregate. Count? True:false; } public override Object CurrentItem ()//Returns the current clustered object {return aggregate[current]; }}} abstract class Aggregate//Aggregates abstraction classes {public abstract Iterator createiterator (); Create iterator} class Concreteaggregate:aggregate {private IList <object > items = new List<object > ();//Life an IList generic variable, with The public override Iterator Createiterator () {return new Concreteiterator (T) can also be implemented with ArrayList for storing aggregation objects. His); } public int count//Returns the total number of aggregates {get {return items. Count; }} public Object This[int index]//Declare an indexer {get {return items[index];} set {items. Insert (index, value); } } }
because iterators are common in programming, high-level languages abstract iterators highly, such as arrays,arrylist, collections, hash tables, dictionaries, and so on. So we don't usually notice this pattern.
pick it up and see . NET built-in iterators.
Class program { static void Main (string[] args) { ilist<string > A = new list<string > ();
a.add ("Big Bird"); A.add ("Side dishes"); A.add ("baggage"); A.add ("foreigner"); A.add ("Personnel within the system"); A.add ("Employees"); A.add ("thief"); foreach (string item in a) { Console.WriteLine ("{0} Please buy ticket", item); } Console.read (); } } Public interface Ieumerator { Object- Current//Gets the present element in the collection { get; } BOOL MoveNext (); The enumeration is made to the next element in the collection. marked with a bool value. void Reset (); Restore initializes only the location that you want, before the first element in the collection } public interface IEnumerable //Returns an enumerator that iterates through the collection { Ieumerator GetEnumerator (); }
If we want to add iterators to a custom data structure, how do we do that?
We only need to inherit and implement the interface IEnumerable .
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design Pattern iterator mode