Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of iterator pattern in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "sample analysis of iterator patterns in C#", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample Analysis of iterator patterns in C#".

What is the iterator pattern?

Iterator pattern (Iterator): provides a way to access elements in an aggregated object sequentially without exposing the internal representation of the object.

When to use iterator mode?

When you need to access an aggregate object, and you need to traverse whatever those objects are, you need to consider using the iterator pattern.

Composition of iterator pattern

Iterator: iterator abstract class, used to define the start object, to the next object, to determine whether to the end, the current object and other abstract methods, unified interface.

ConcreteAggregate: saves the aggregate object.

ConcreteIterator: inherits from Iterator to implement specific operations on aggregate objects.

Concrete implementation of iterator pattern

The structure of iterator pattern

Implementation of the iterator pattern:

Iterator class:

Abstract class Iterator {public abstract object First (); public abstract object Next (); public abstract bool IsDone (); public abstract object CurrentItem ();}

ConcreteIterator class:

/ / sequentially traverse class ConcreteIterator: Iterator {private ConcreteAggregate aggregate; private int current = 0; / / transfer the current data set to 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 obj = null; current++; if (current

< aggregate.Count) { obj = aggregate[current]; } return obj; }}//逆序遍历class ConcreteIteratorDesc : Iterator{ private ConcreteAggregate aggregate; private int current = 0; //传输数据进来 public ConcreteIteratorDesc(ConcreteAggregate aggregate) { this.aggregate = aggregate; current = aggregate.Count - 1; } public override object CurrentItem() { return aggregate[current]; } public override object First() { return aggregate[aggregate.Count - 1]; } public override bool IsDone() { return current < 0 ? true:false; } public override object Next() { object obj = null; current--; if (current >

= 0) {obj = aggregate [current];} return obj;}}

ConcreteAggregate class:

/ create an iterator / it doesn't seem to be of any specific use here / abstract class Aggregate {public abstract Iterator CreateIterator ();} / the function is to save data, which is a series of data, so use the array / and then transfer the data to ConcreteIterator/// class ConcreteAggregate: Aggregate {/ / to store the aggregate object private IList items = new List () Public override Iterator CreateIterator () {return new ConcreteIterator (this);} / / the length of the array, that is, the ConcreteAggregate attribute public int Count {get {return items.Count;}} / / ConcreteAggregate is now in array form / get gets the current data / set inserts the new data into the ConcreteAggregate public object this [int index] {get {return items [index];} set {items.Insert (index, value);}

The main function call:

Static void Main (string [] args) {ConcreteAggregate a = new ConcreteAggregate (); a [0] = "A"; a [1] = "B"; a [2] = "C"; a [3] = "D"; a [4] = "E"; a [5] = "F"; Iterator I = new ConcreteIterator (a); object item = i.First (); while (! i.IsDone ()) {Console.WriteLine ("{0} buy ticket,please", i.CurrentItem ()); i.Next () } Iterator id = new ConcreteIteratorDesc (a); object itemdec = id.First (); while (! id.IsDone ()) {Console.WriteLine ("{0} buy ticket,please", id.CurrentItem ()); id.Next ();} Console.Read ();}

Implementation of iterator in. Net

The iterator pattern is actually less troublesome in our current use, because the .NET Framework has prepared the relevant interfaces and just needs to implement them.

Static void Main (string [] args) {IList a = new List (); a.Add ("A"); a.Add ("B"); a.Add ("C"); a.Add ("D"); a.Add ("E"); a.Add ("F"); / / consider foreach foreach (string item in a) {Console.WriteLine ("{0} buy ticket,please", item) first when you see traversal;} / / support simple iterations over generic sets. IEnumerator e = a.GetEnumerator (); while (e.MoveNext ()) {Console.WriteLine ("{0} buy ticket,please", e.Current);} Console.Read ();}

Supplement: IEnumerator

Note: all the codes and knowledge points in this article come from the "lie design pattern". I belong to the stage of learning and tapping the code while summarizing.

The above is all the content of the article "sample Analysis of iterator patterns in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report