Iterator: provides a method to access each element of an aggregate object sequentially without exposing the internal details of the object. Structure structure Iterator abstract class (Iterator): defines interfaces for accessing and traversing elements. Define the current start object, get the next object, and get... syntaxHighlighter. all (); overview Iterator mode (Iterator): provides a way to access each element of an aggregate object sequentially without exposing the internal details of the object. Structure structure Iterator abstract class (Iterator): defines interfaces for accessing and traversing elements. Define the current start object, get the next object, get the next object, and determine whether or not to end. The list access and traversal are separated from the List objects and placed in the Iterator object. [Csharp] // Iterator abstract class Iterator {public abstract object First (); public abstract object Next (); public abstract object IsDone (); public abstract object CurrentItem ();} Concrete Iterator: implements the Iterator interface to record the current position in the traversal. [Csharp] // a specific Iterator class that inherits Iterator class ConcreteIterator: Iterator {private ConcreteAggregate aggregate; private int current = 0; public ConcreteIterator (ConcreteAggregate aggregate) {this. aggregate = aggregate;} public override object First () {return aggregate [0];} public override object Next () {object ret = null; current ++; if (current <aggregate. count) {ret = aggregate [current];} return r Et;} public override bool IsDone () {return current> = aggregate. count? True: false;} public override object CurrentItem () {return aggregate [current];} Aggregate abstract class (aggregate): provides an interface for creating a specific iterator. [Csharp] // aggregate abstract class Aggreate {public abstract Iterator CreateIterator ();} aggregate class (ConcreteAggregate): stores aggregate objects, returns the number of aggregates, and declares an indexer. [Csharp] // class ConcreteAggregate: Aggreate {private IList Items = new List(); 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) ;}} client [csharp] static void Main (string [] args) {ConcreteAggregate a = new ConcreteAggregate (); a [0] = "persona"; a [1] = "personb"; Iterator I = new ConcreteIterator (); Object item = I. First (); while (! I. IsDone () {Console. WriteLine ("{0} Please buy a ticket first! ", I. CurrentItem (); I. Next ();} Console. ReadLine ();}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.