Design Pattern-iterator Pattern

Source: Internet
Author: User

The iterator provides a way to access an aggregate object sequentially without exposing the internal details of the object. Net foreach is a typical iterator mode.


Code: <喎?http: kf ware vc " target="_blank" class="keylink"> VcD4KPHA + logs/nytvstwvcd4kpha + PHByZSBjbGFzcz0 = "brush: java;"> class Program {static void Main (string [] args) {ConcreteAggregate a = new ConcreteAggregate (); a [0] = "laruence"; a [1] = "xiao"; a [2] = "Luggage"; a [3] = "foreigner "; a [4] = "internal employee"; a [5] = "thief"; Iterator I = new ConcreteIterator (a); object item = I. first (); while (! I. IsDone () {Console. WriteLine ("{0} Please buy a ticket! ", I. currentItem (); I. next ();} Console. read () ;}// the abstract Iterator class abstract class Iterator {public abstract object First (); public abstract object Next (); public abstract bool IsDone (); public abstract object CurrentItem () ;}// Aggregate abstract class Aggregate {public abstract Iterator createIterator (); // create Iterator} // class ConcreteIterator of the specific Iterator class: iterator {private ConcreteAggregate aggregate; private in T current = 0; public ConcreteIterator (ConcreteAggregate aggregate) {this. aggregate = aggregate;} public override object First () {// throw new NotImplementedException (); return aggregate [0];} public override object Next () {// throw new NotImplementedException (); object ret = null; current ++; if (current <aggregate. count) {ret = aggregate [current];} return ret;} public override bool IsDone () {// Throw new NotImplementedException (); return current> = aggregate. Count? True: false;} public override object CurrentItem () {// throw new NotImplementedException (); return aggregate [current];} class ConcreteAggregate: aggregate {// declare an IList generic variable to store the Aggregate object private IList <object> items = new List <object> (); public override Iterator createIterator () {// throw new NotImplementedException (); 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 );}}}
Running result:

Contact Us

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: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.