Skip to content

Commit 52032c5

Browse files
committed
举例迭代器模式
1 parent 6ace437 commit 52032c5

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

DesignPatterns/IteratorPattern/IteratorPattern/Aggregate.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ConcreteAggregate : Aggregate
2020

2121
public override Iterator CreateIterator()
2222
{
23-
23+
return new ConcreteIterator(this);
2424
}
2525

2626
public int Size()
@@ -35,7 +35,7 @@ public Object GetItem(int i)
3535

3636
public void SetItems(int i,Object obj)
3737
{
38-
items[i] = obj;
38+
items.Add(obj);
3939
}
4040
}
4141
}

DesignPatterns/IteratorPattern/IteratorPattern/IteratorPattern.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45+
<Compile Include="Aggregate.cs" />
46+
<Compile Include="Iterator.cs" />
4547
<Compile Include="Program.cs" />
4648
<Compile Include="Properties\AssemblyInfo.cs" />
4749
</ItemGroup>

DesignPatterns/IteratorPattern/IteratorPattern/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ class Program
1010
{
1111
static void Main(string[] args)
1212
{
13+
ConcreteAggregate aggregate = new ConcreteAggregate();
14+
15+
for (int i = 0; i < 10; i++)
16+
{
17+
aggregate.SetItems(i,"求职者"+(i+1));
18+
}
19+
Iterator iterator = aggregate.CreateIterator();
20+
while (!iterator.IsDone())
21+
{
22+
Console.WriteLine(iterator.CurrentItem()+"来我公司面试");
23+
iterator.Next();
24+
}
1325
}
1426
}
1527
}

0 commit comments

Comments
 (0)