iterator pattern

remotes/origin/master
syneffort 3 years ago
parent b838668f64
commit 11cb7eb330
  1. 23
      DesignPattern/Iterator/Client.cs
  2. 20
      DesignPattern/Iterator/ColorAggregate.cs
  3. 35
      DesignPattern/Iterator/CommonIterator.cs
  4. 13
      DesignPattern/Iterator/IAggregate.cs
  5. 15
      DesignPattern/Iterator/IIterator.cs
  6. 5
      DesignPattern/Iterator/Iterator.csproj
  7. 3
      DesignPattern/Iterator/Program.cs

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
internal class Client
{
public static void HowToTest()
{
IAggregate agg = new ColorAggregate();
IIterator iter = agg.GetIterator();
object c1 = iter.Next();
object c2 = iter.Next();
object c3 = iter.Next();
Console.WriteLine($"{c1}, {c2}, {c3}");
}
}
}

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
internal class ColorAggregate : IAggregate
{
object[] colors = { "White", "Red", "Green", "Blue", "Black" };
public IIterator GetIterator()
{
return new CommonIterator(colors);
}
public int Count { get { return colors.Length; } }
}
}

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
internal class CommonIterator : IIterator
{
private object[] collection;
private int index;
public CommonIterator(object[] collection)
{
this.collection = collection;
this.index = -1;
}
public bool HasNext { get { return index + 1 < collection.Length; } }
public object Next()
{
if (HasNext)
{
index++;
return collection[index];
}
else
{
return null;
}
}
}
}

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
internal interface IAggregate
{
IIterator GetIterator();
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Iterator
{
internal interface IIterator
{
bool HasNext { get; }
object Next();
}
}

@ -43,6 +43,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Client.cs" />
<Compile Include="ColorAggregate.cs" />
<Compile Include="CommonIterator.cs" />
<Compile Include="IAggregate.cs" />
<Compile Include="IIterator.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

@ -16,6 +16,9 @@ namespace Iterator
{
static void Main(string[] args)
{
Client.HowToTest();
Console.ReadKey();
}
}
}

Loading…
Cancel
Save