Advertisement
Slash18

Iterate

Jun 26th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1.  
  2. // Totorial on https://indiedevart.wordpress.com/
  3.  
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ConsoleApplication9
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Aggregate aggr = new Aggregate();
  18.  
  19.             aggr[0] = 7;
  20.             aggr[1] = 8;
  21.             aggr[2] = 9;
  22.             aggr[3] = 89;
  23.            
  24.             IIterator iter = aggr.GetIterator();
  25.  
  26.             for (int i = iter.FirstItem; iter.IsDone == false; i = iter.NextItem)
  27.             {
  28.                 Console.WriteLine(i.ToString());
  29.             }
  30.             Console.ReadLine();
  31.         }
  32.     }
  33.     interface IIterator
  34.     {
  35.         int FirstItem { get; }
  36.         int NextItem { get; }
  37.         int CurrentItem { get; }
  38.         bool IsDone { get; }
  39.     }
  40.  
  41.     class Iterator : IIterator
  42.     {
  43.         IAggregate aggregate = null;
  44.         int currentIndex = 0;
  45.  
  46.         public Iterator(IAggregate newAggregate)
  47.         {
  48.             aggregate = newAggregate;
  49.         }
  50.  
  51.         public int FirstItem
  52.         {
  53.             get
  54.             {
  55.                 currentIndex = 0;
  56.                 return aggregate[currentIndex];
  57.             }
  58.         }
  59.  
  60.         public int NextItem
  61.         {
  62.             get
  63.             {
  64.                 currentIndex += 1;
  65.  
  66.                 if (IsDone == false)
  67.                 {
  68.                     return aggregate[currentIndex];
  69.                 }
  70.                 else
  71.                 {
  72.                     return -1;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         public int CurrentItem
  78.         {
  79.             get
  80.             {
  81.                 return aggregate[currentIndex];
  82.             }
  83.         }
  84.  
  85.         public bool IsDone
  86.         {
  87.             get
  88.             {
  89.                 if (currentIndex < aggregate.Count)
  90.                 {
  91.                     return false;
  92.                 }
  93.                 return true;
  94.             }
  95.         }
  96.  
  97.     }
  98.  
  99.     interface IAggregate
  100.     {
  101.         IIterator GetIterator();
  102.         int this[int itemIndex] { set; get; }
  103.         int Count { get; }
  104.     }
  105.  
  106.     class Aggregate : IAggregate
  107.     {
  108.         List<int> values = null;
  109.  
  110.         public Aggregate()
  111.         {
  112.             values = new List<int>();
  113.         }
  114.  
  115.        
  116.  
  117.         public IIterator GetIterator()
  118.         {
  119.             return new Iterator(this);
  120.         }
  121.  
  122.        
  123.  
  124.         public int this[int itemIndex]
  125.         {
  126.             get
  127.             {
  128.                 if (itemIndex < values.Count)
  129.                 {
  130.                     return values[itemIndex];
  131.                 }
  132.                 else
  133.                 {
  134.                     return -1;
  135.                 }
  136.             }
  137.             set
  138.             {
  139.                 values.Add(value);
  140.             }
  141.         }
  142.  
  143.         public int Count
  144.         {
  145.             get
  146.             {
  147.                 return values.Count;
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement