Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Totorial on https://indiedevart.wordpress.com/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication9
- {
- class Program
- {
- static void Main(string[] args)
- {
- Aggregate aggr = new Aggregate();
- aggr[0] = 7;
- aggr[1] = 8;
- aggr[2] = 9;
- aggr[3] = 89;
- IIterator iter = aggr.GetIterator();
- for (int i = iter.FirstItem; iter.IsDone == false; i = iter.NextItem)
- {
- Console.WriteLine(i.ToString());
- }
- Console.ReadLine();
- }
- }
- interface IIterator
- {
- int FirstItem { get; }
- int NextItem { get; }
- int CurrentItem { get; }
- bool IsDone { get; }
- }
- class Iterator : IIterator
- {
- IAggregate aggregate = null;
- int currentIndex = 0;
- public Iterator(IAggregate newAggregate)
- {
- aggregate = newAggregate;
- }
- public int FirstItem
- {
- get
- {
- currentIndex = 0;
- return aggregate[currentIndex];
- }
- }
- public int NextItem
- {
- get
- {
- currentIndex += 1;
- if (IsDone == false)
- {
- return aggregate[currentIndex];
- }
- else
- {
- return -1;
- }
- }
- }
- public int CurrentItem
- {
- get
- {
- return aggregate[currentIndex];
- }
- }
- public bool IsDone
- {
- get
- {
- if (currentIndex < aggregate.Count)
- {
- return false;
- }
- return true;
- }
- }
- }
- interface IAggregate
- {
- IIterator GetIterator();
- int this[int itemIndex] { set; get; }
- int Count { get; }
- }
- class Aggregate : IAggregate
- {
- List<int> values = null;
- public Aggregate()
- {
- values = new List<int>();
- }
- public IIterator GetIterator()
- {
- return new Iterator(this);
- }
- public int this[int itemIndex]
- {
- get
- {
- if (itemIndex < values.Count)
- {
- return values[itemIndex];
- }
- else
- {
- return -1;
- }
- }
- set
- {
- values.Add(value);
- }
- }
- public int Count
- {
- get
- {
- return values.Count;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement