static void Main(string[] args) { //客户端代码可能不知道具体的Iterator或Collection类,这取决于要在程序中保留的间接级别。 var collection = new WordsCollection(); collection.AddItem("first item"); collection.AddItem("second item"); collection.AddItem("third item"); Console.WriteLine("Straight traversal:"); foreach (var item in collection) { Console.WriteLine(item); } collection.ReverseDirection(); Console.WriteLine(); Console.WriteLine("reverse traversal:"); foreach (var item in collection) { Console.WriteLine(item); } Console.ReadKey(); }
public AlphabeticalOrderIterator(WordsCollection collection, bool reverse) { _collection = collection; _reverse = reverse; if (reverse) { _postsion = collection.GetItems().Count; } }