Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            ConcreteCollection a = new ConcreteCollection();

            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();
        }
Exemplo n.º 2
0
        public ConcreteIterator(ConcreteCollection collection, bool reverse)
        {
            _collection = collection;
            _reverse    = reverse;

            if (_reverse)
            {
                this._position = _collection.GetItems().Count;
            }
        }
Exemplo n.º 3
0
        private static void Main(string[] args)
        {
            var coworkersProfileCollection = new ConcreteCollection <Profile>();

            coworkersProfileCollection[0] = new Profile
            {
                Name    = "Alexey",
                Surname = "Kolesnikov",
                Age     = 30
            };
            coworkersProfileCollection[1] = new Profile
            {
                Name    = "Anton",
                Surname = "Vlasik",
                Age     = 31
            };
            coworkersProfileCollection[2] = new Profile
            {
                Name    = "Sergey",
                Surname = "Egorov",
                Age     = 35
            };
            coworkersProfileCollection[3] = new Profile
            {
                Name    = "Maksim",
                Surname = "Zuy",
                Age     = 26
            };

            var concreteIterator = coworkersProfileCollection.GetIterator();

            Console.WriteLine("Read collection with default iterator");

            for (var profile = concreteIterator.FirstItem;
                 concreteIterator.HasNext();
                 profile = concreteIterator.GetNext)
            {
                Console.WriteLine($"{profile.Name}, {profile.Surname}, {profile.Age}");
            }

            Console.ReadKey();
        }
Exemplo n.º 4
0
        public static void Run()
        {
            ConcreteCollection collection = new ConcreteCollection();

            collection.AddEmployee(new Employee("Anurag", 100));
            collection.AddEmployee(new Employee("Pranaya", 101));
            collection.AddEmployee(new Employee("Santosh", 102));
            collection.AddEmployee(new Employee("Priyanka", 103));
            collection.AddEmployee(new Employee("Abinash", 104));
            collection.AddEmployee(new Employee("Preety", 105));

            Iterator iterator = collection.CreateIterator();

            Console.WriteLine("Iterating over collection:");
            for (Employee emp = iterator.First(); !iterator.IsCompleted; emp = iterator.Next())
            {
                Console.WriteLine($"ID : {emp.ID} & Name : {emp.Name}");
            }
            Console.Read();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var collection = new ConcreteCollection();

            collection.AddItem("Item A");
            collection.AddItem("Item B");
            collection.AddItem("Item C");

            foreach (var item in collection)
            {
                Console.WriteLine(item);
            }

            collection.Reverse();

            foreach (var item in collection)
            {
                Console.WriteLine(item);
            }
        }
Exemplo n.º 6
0
        public static void Main()
        {
            var collection = new ConcreteCollection();

            collection.AddCustomer(new Customer("mfk", 1));
            collection.AddCustomer(new Customer("mfk2", 2));
            collection.AddCustomer(new LocalCustomer("mfk3", 3));
            collection.AddCustomer(new LocalCustomer("mfk4", 4));
            collection.AddCustomer(new InternationalCustomer("mfk5", 5));
            collection.AddCustomer(new InternationalCustomer("mfk6", 6));

            var iterator = collection.CreateIterator();

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

            for (var customer = iterator.First(); !iterator.IsCompleted; customer = iterator.Next())
            {
                Console.WriteLine($"ID : {customer.Id} & Name : {customer.Name}");
            }
            Console.Read();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            // Создаём коллекцию, обход которой будем производить.
            ConcreteCollection collection = new ConcreteCollection();

            // Коллекцию можно сформировать и в конструкторе.
            collection[0] = "Элемент 1";
            collection[1] = "Элемент 2";
            collection[2] = "Элемент 3";
            collection[3] = "Элемент 4";

            // Создаём итератор
            Iterator i = collection.CreateIterator();

            // Обходим коллекцию итератором
            Console.WriteLine("Итерируем коллекцию через while:");
            object item1 = i.First();

            // Обход через while
            //while (item1 != null) // Можно итерировать, пока не получим пустой элемент или использовать счетчик
            while (!i.IsDone())
            {
                Console.WriteLine(item1);
                item1 = i.Next();
            }

            // Сбрасываем позицию в начало для повторного обхода коллекции
            i.Reset();

            Console.WriteLine("Итерируем коллекцию через for:");
            // Обход через for
            for (object item2 = i.First();
                 !i.IsDone(); item2 = i.Next())
            {
                Console.WriteLine(item2);
            }

            Console.ReadKey();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            // Build a collection
            ConcreteCollection collection = new ConcreteCollection();

            collection.AddEmployee(new Elempoyee("Anurag", 100));
            collection.AddEmployee(new Elempoyee("Pranaya", 101));
            collection.AddEmployee(new Elempoyee("Santosh", 102));
            collection.AddEmployee(new Elempoyee("Priyanka", 103));
            collection.AddEmployee(new Elempoyee("Abinash", 104));
            collection.AddEmployee(new Elempoyee("Preety", 105));

            // Create iterator
            Iterator iterator = collection.CreateIterator();

            //looping iterator
            Console.WriteLine("Iterating over collection:");

            for (Elempoyee emp = iterator.First(); !iterator.IsCompleted; emp = iterator.Next())
            {
                Console.WriteLine($"ID : {emp.ID} & Name : {emp.Name}");
            }
            Console.Read();
        }
Exemplo n.º 9
0
 public ConcreteIterator(ConcreteCollection aggregate)
 {
     this._collection = aggregate;
 }
Exemplo n.º 10
0
 public Iterator(ConcreteCollection collection)
 {
     _collection = collection;
 }