Beispiel #1
0
        static void Main(string[] args)
        {
            // TODO: шаг 5

            /*
             * В клиентском коде и в классах коллекций не должно остаться кода обхода элементов.
             * Клиент должен получать новый итератор из объекта коллекции каждый раз, когда ему нужно перебрать её элементы.
             */
            Aggregate a = new ConcreteAggregate();
            Iterator  i = a.CreateIterator();

            if (!i.IsDone())
            {
                object item = i.First();
                while (!i.IsDone())
                {
                    item = i.Next();
                }
            }
            //------------------------------------------------------------------------------------------------------------

            // step 2
            Library library = new Library();
            Reader  reader  = new Reader();

            reader.SeeBooks(library);

            Console.Read();
        }
        static void Main(string[] args)
        {
            var concreteAggregate = new ConcreteAggregate();

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

            // Create Iterator and provide aggregate
            var iterator = concreteAggregate.CreateIterator();

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

            object item = iterator.First();

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

            // Wait for user
            Console.ReadKey();
        }
Beispiel #3
0
 private void DoneButton_Click(object sender, EventArgs e)
 {
     GuestListIterator       = GuestList.CreateIterator();
     AddGuestButton.Enabled  = false;
     AddGuestTextbox.Enabled = false;
     IterateButton.Enabled   = true;
     StartOverButton.Enabled = true;
     DoneButton.Enabled      = false;
 }
        static void Main(string[] args)
        {
            object[]  objects   = new[] { "hello", "jello", "welcome", "my", "home" };
            Aggregate aggregate = new ConcreteAggregate(objects);
            Iterator  iterator  = aggregate.CreateIterator();

            while (iterator.MoveNext())
            {
                Console.WriteLine(iterator.GetCurrent());
            }
            Console.ReadKey();
        }
Beispiel #5
0
        public static void Main(string[] args)
        {
            ConcreteAggregate a = new ConcreteAggregate(5);

            a[0] = new ConcreteAggregate();
            a[1] = new ConcreteAggregate();
            Iterator i = a.CreateIterator();

            while (i.HasNext())
            {
                Console.WriteLine(i.Next().GetType().ToString());
            }
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            ConcreteAggregate aggregate = new ConcreteAggregate();

            for (int i = 0; i < 10; i++)
            {
                aggregate.SetItems(i, "求职者" + (i + 1));
            }
            Iterator iterator = aggregate.CreateIterator();

            while (!iterator.IsDone())
            {
                Console.WriteLine(iterator.CurrentItem() + "来我公司面试");
                iterator.Next();
            }
        }
Beispiel #7
0
        private static void IteratorTest()
        {
            ConcreteAggregate <string> aggr = new ConcreteAggregate <string>();

            aggr.Add("One");
            aggr.Add("Two");
            aggr.Add("Three");
            aggr.Add("Four");
            aggr.Add("Five");

            IIterator <string> iterator = aggr.CreateIterator();

            do
            {
                Console.WriteLine(iterator.Current);
            }while (iterator.MoveNext());
        }
Beispiel #8
0
        private static void Main(string[] args)
        {
            Console.WriteLine("*********** ForEach ******************");
            var employees = new string[] { "Rajiv", "Ranjan", "Ribha" };

            foreach (var employee in employees)
            {
                Console.WriteLine(employee);
            }

            // Aggregate Interface : IEnumerable
            // Iterator Interface : IEnumerator

            Console.WriteLine("*********** IEnumerable and IEnumerator ******************");

            var employeeEnumerator = employees.GetEnumerator();

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

            Console.WriteLine("*********** Itegator Design Pattern ******************");

            ConcreteAggregate aggregate = new ConcreteAggregate();

            aggregate[0] = new Employee()
            {
                Name = "Rajiv", EmpCode = "100"
            };
            aggregate[1] = new Employee()
            {
                Name = "Ranjan", EmpCode = "101"
            };

            var iterator = aggregate.CreateIterator();

            while (iterator.MoveNext())
            {
                var employee = ((Employee)iterator.Current);
                Console.WriteLine("{0} {1}", employee.Name, employee.EmpCode);
            }

            Console.Read();
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("--------------------IteratorPattern--------------------");
            Console.WriteLine();
            IIterator iterator;
            var       concreteAggregate = new ConcreteAggregate();

            iterator = concreteAggregate.CreateIterator();
            while (iterator.MoveNext())
            {
                var temp = iterator.GetCurrent().ToString();
                Console.WriteLine(temp);
                iterator.Next();
            }

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            ConcreteAggregate aggregate = new ConcreteAggregate();

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

            Iterator iterator = aggregate.CreateIterator();
            object   item     = iterator.First();

            while (!iterator.IsDone())
            {
                Console.WriteLine(item);
                item = iterator.Next();
            }
        }
        static void Main(string[] args)
        {
            ConcreteAggregate ca = new ConcreteAggregate();

            ca[0] = "test1";
            ca[1] = "test2";
            ca[2] = "test3";

            //Iterator iterator = new ConcreteIterator(ca);

            Iterator iterator = ca.CreateIterator();
            object   obj      = iterator.First();

            while (!iterator.IsDone())
            {
                Console.WriteLine(iterator.CurrentItem().ToString());
                iterator.Next();
            }

            Console.Read();
        }
Beispiel #12
0
        static void Main()
        {
            ConcreteAggregate agr = new ConcreteAggregate();

            agr[0] = "Item 1";
            agr[1] = "Item 2";
            agr[2] = "Item 3";
            agr[3] = "Item 4";
            agr[4] = "Item 5";

            // Create Iterator and provide aggregate
            Iterator i = agr.CreateIterator();

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

            object item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }
        }
Beispiel #13
0
        public Form1()
        {
            InitializeComponent();

            // Create an aggregate to be turned in to an iterator.
            aggregate = new ConcreteAggregate();

            // Adding elements to the aggregate, this would normally not be
            // done this way. For the sake of the demo it will work. I made
            // the Person object just to learn about objects in C#, specifically
            // the ToString() method.
            aggregate.Add(new Person("Joe", 24));
            aggregate.Add(new Person("Alicia", 24));
            aggregate.Add(new Person("Roger", 58));
            aggregate.Add(new Person("Beth", 51));
            aggregate.Add(new Person("Emily", 22));
            aggregate.Add(new Person("Dave", 103));

            // Turn the aggregate into the iterator.
            iterator = aggregate.CreateIterator();

            FillListBox();
            SetLblTxt();
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            Console.WriteLine("Custom Iterator pattern implementation");
            ConcreteAggregate <int> collection = new ConcreteAggregate <int>();

            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            collection.Add(4);
            collection.Add(5);

            IIterator <int> iterator = collection.CreateIterator();

            while (iterator.Next())
            {
                int item = iterator.Current;
                Console.WriteLine(item);
            }

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

            //Itertator pattern implementation in .NET
            Console.WriteLine(".Net Iterator pattern implementation");
            IEnumerable <int> elements = new List <int>()
            {
                1, 2, 3, 4, 5
            };

            IEnumerator <int> enumerator = elements.GetEnumerator();

            while (enumerator.MoveNext())
            {
                int currentElement = enumerator.Current;
                Console.WriteLine(currentElement);
            }
        }
Beispiel #15
0
 private void btn_ageSort_Click(object sender, EventArgs e)
 {
     // Replaces the exsisting iterator with on that is sorted by age.
     iterator = aggregate.CreateIterator(Aggregate.IteratorType.SortedByAge);
     FillListBox();
 }