CreateIterator() public method

public CreateIterator ( ) : Iterator
return Iterator
Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var aggregate = new ConcreteAggregate();

            aggregate[0] = "Item A";
            aggregate[1] = "Item B";
            aggregate[2] = "Item C";
            aggregate[3] = "Item D";

            var iterator = aggregate.CreateIterator();

            Console.WriteLine("interagindo com a colecao:");

            object item = iterator.First();

            while (item != null)
            {
                Console.WriteLine($"Current Item:{iterator.CurrentItem()}");
                item = iterator.Next();
                Console.WriteLine($"Is done:{iterator.IsDone()}");
            }

            iterator.Next();
            Console.WriteLine($"Is done:{iterator.IsDone()}");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            ConcreteAggregate concreteAggregate_A = new ConcreteAggregate();

            concreteAggregate_A[0] = "Item A";
            concreteAggregate_A[1] = "Item B";
            concreteAggregate_A[2] = "Item C";
            concreteAggregate_A[3] = "Item D";

            Iterator iterator_A = concreteAggregate_A.CreateIterator();

            Console.WriteLine("Iteragindo com a coleção: ");

            object objItem = iterator_A.First();
            int    x       = 0;

            while (objItem != null)
            {
                Console.WriteLine(objItem);
                if (x == 2)
                {
                    objItem = iterator_A.CurrentItem();
                }

                objItem = iterator_A.Next();
                x++;
            }

            Console.ReadKey();
        }
        public static void Run()
        {
            Console.WriteLine("This structural code demonstrates the Iterator pattern which provides for a way to traverse (iterate) over a collection of items without detailing the underlying structure of the collection.");
            ConcreteAggregate a = new ConcreteAggregate();

            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";

            Iterator i = a.CreateIterator();

            Console.WriteLine("Iterating over collection");

            object item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }

            /*
             * Iterating over collection:
             * Item A
             * Item B
             * Item C
             * Item D
             */
        }
Ejemplo n.º 4
0
        static void Main()
        {
            Aggregate aggregate = new ConcreteAggregate();

            aggregate[0] = "Element A";
            aggregate[1] = "Element B";
            aggregate[2] = "Element C";
            aggregate[3] = "Element D";

            Iterator iterator = aggregate.CreateIterator();


            Console.WriteLine("Первый элемент коллекции:");

            object element = iterator.First();

            Console.WriteLine(element);

            Console.WriteLine(new string('-', 25));

            Console.WriteLine("Итерация по коллекции:");
            while (!iterator.IsDone())
            {
                Console.WriteLine(element);
                element = iterator.Next();
            }

            // Delay.
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            /*
             * More info: https://en.wikipedia.org/wiki/Iterator_pattern
             * http://www.dofactory.com/net/iterator-design-pattern
             * What problems can the Iterator design pattern solve?
             * The elements of an aggregate object should be accessed and traversed without exposing its representation (data structures).
             * New traversal operations should be defined for an aggregate object without changing its interface.
             *
             * Defining access and traversal operations in the aggregate interface is inflexible
             * because it commits the aggregate to particular access and traversal operations
             * and makes it impossible to add new operations later without having to change the aggregate interface.
             */

            var a = new ConcreteAggregate
            {
                [0] = "Item A",
                [1] = "Item B",
                [2] = "Item C",
                [3] = "Item D"
            };

            Iterator.Iterator i = a.CreateIterator();

            Console.WriteLine("Iterating over collection:");

            object item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }

            Console.WriteLine("========================== using the built in mechanisms or .Net==============================");

            var myCollection = new MyCollection <int> {
                2, 3, 4, 5
            };

            myCollection.Add(25);
            myCollection.Add(34);
            myCollection.Add(32);

            foreach (var collectionItem in myCollection)
            {
                Console.WriteLine(collectionItem);
            }

            Console.WriteLine("===============================================================================================");

            var enumerator = myCollection.GetEnumerator();

            while (enumerator.MoveNext())
            {
                Console.WriteLine(enumerator.Current);
            }

            Console.ReadKey();
        }
Ejemplo n.º 6
0
        static void Main()
        {
            ConcreteAggregate a = new ConcreteAggregate();

            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";

            // Create Iterator and provide aggregate

            Iterator i = a.CreateIterator();

            Console.WriteLine("Iterating over collection:");

            object item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }

            // Wait for user

            Console.ReadKey();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            ConcreteAggregate aggregate = new ConcreteAggregate();

            aggregate.Add(new Category()
            {
                Name = "Csharp"
            });
            aggregate.Add(new Category()
            {
                Name = "Asp.Net"
            });
            aggregate.Add(new Category()
            {
                Name = "EntityFramework"
            });
            aggregate.Add(new Category()
            {
                Name = ".NET CORE"
            });

            IIterator iterator = aggregate.CreateIterator();

            Console.WriteLine($"İlk eleman : {((Category)iterator.First()).Name}");
            Console.WriteLine($"Son eleman : {((Category)iterator.Last()).Name}");

            while (iterator.MoveNext())
            {
                Console.WriteLine(((Category)iterator.Current()).Name);
            }

            Console.ReadLine();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            ConcreteAggregate a = new ConcreteAggregate();

            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";

            Iterator i = a.CreateIterator();

            Console.WriteLine("Interagindo com a coleção: ");
            object item = i.First();

            int x = 0;

            while (item != null)
            {
                Console.WriteLine(item);

                if (x == 2)
                {
                    item = i.CurrentItem();
                }

                item = i.Next();
                x++;
            }

            Console.ReadKey();
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Aggregate a = new ConcreteAggregate();

            Iterator.Abstract.Iterator i = a.CreateIterator();

            object item = i.First();

            while (!i.IsDone())
            {
                item = i.Next();
            }
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            #region 结构实现
            ConcreteAggregate a = new ConcreteAggregate();
            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";

            ConcreteIterator i = a.CreateIterator() as ConcreteIterator;

            Console.WriteLine("Iterating over collection:");
            object item = i.First();
            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }
            #endregion

            Console.WriteLine("******************************");

            #region 实践应用
            Practical.ExtrinsicIterator.Iterator  _iterator;
            Practical.ExtrinsicIterator.Aggregate _aggregate = new Practical.ExtrinsicIterator.ConcreteAggregate();
            // 获得迭代器
            _iterator = _aggregate.CreateIterator();
            while (!_iterator.IsDone())
            {
                Console.WriteLine(_iterator.CurrentItem());
                _iterator.Next();
            }


            Practical.IntrinsicIterator.Iterator  _iterator1;
            Practical.IntrinsicIterator.Aggregate _aggregate1 = new Practical.IntrinsicIterator.ConcreteAggregate();
            // 获得迭代器
            _iterator1 = _aggregate1.CreateIterator();
            while (!_iterator1.IsDone())
            {
                Console.WriteLine(_iterator1.CurrentItem());
                _iterator1.Next();
            }

            #endregion

            Console.ReadKey();
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            var aggregate = new ConcreteAggregate
            {
                [0] = "A",
                [1] = "B",
                [2] = "C",
                [3] = "D"
            };

            var iterator = aggregate.CreateIterator();

            for (var e = iterator.First(); !iterator.IsDone(); e = iterator.Next())
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 12
0
        public static void Main()
        {
            var a = new ConcreteAggregate <string>
            {
                [0] = "String one", [1] = "String two", [2] = "String three", [3] = "String four"
            };

            var iter = a.CreateIterator();

            Console.WriteLine("Iterating over the items...");
            var item = iter.First();

            while (item != null)
            {
                Console.WriteLine($"Item is {item}");
                item = iter.Next();
            }
        }
Ejemplo n.º 13
0
        //fornece uma maneira de acessar os elementos de um objeto agregado sequencialmente
        //sem expor sua representacao subjacente
        static void Main(string[] args)
        {
            ConcreteAggregate aggregate = new ConcreteAggregate();

            aggregate[0] = "Item1";
            aggregate[1] = "Item2";
            aggregate[2] = "Item3";
            aggregate[3] = "Item4";

            Iterator iterator = aggregate.CreateIterator();

            Console.WriteLine("Interagindo com a coleçao :");
            object item = iterator.First();

            while (item != null)
            {
                Console.WriteLine(item);
                Console.WriteLine("---{0}", iterator.CurrentItem());;
                item = iterator.Next();
            }
        }
Ejemplo n.º 14
0
        public static void Run()
        {
            Console.Write("Iterator Structural Practice");
            ConcreteAggregate a = new ConcreteAggregate();

            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";

            Iterator i = a.CreateIterator();

            Console.WriteLine("Iterating over collection");

            object item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }
        }
Ejemplo n.º 15
0
        static void Main()
        {
            Aggregate a = new ConcreteAggregate();

            a[0] = "Element A";
            a[1] = "Element B";
            a[2] = "Element C";
            a[3] = "Element D";

            Iterator i = a.CreateIterator();

            for (object e = i.First(); !i.IsDone(); e = i.Next())
            {
                Console.WriteLine(e);
            }

            Console.WriteLine(new string('-', 10));

            for (object e = i.First(); !i.IsDone(); e = i.Next())
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            ConcreteAggregate a = new ConcreteAggregate();

            a[0] = "item A";
            a[1] = "item B";
            a[2] = "item C";
            a[3] = "item D";

            Iterator i = a.CreateIterator();

            Console.WriteLine("iteragindo com a colecao:");

            object item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }

            Console.ReadKey();
        }
Ejemplo n.º 17
0
        private static void Main()
        {
            var concreteAggregate = new ConcreteAggregate
            {
                [0] = "Item A",
                [1] = "Item B",
                [2] = "Item C",
                [3] = "Item D"
            };

            var iterator = concreteAggregate.CreateIterator();

            Console.WriteLine("Iterating:");

            var item = iterator.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = iterator.Next();
            }

            Console.ReadKey();
        }