Skip to content

Commit 8ce049a

Browse files
committed
迭代器模式
1 parent 76c388f commit 8ce049a

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

DesignPattern.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObserverPattern", "Observer
3333
EndProject
3434
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacadePattern", "FacadePattern\FacadePattern.csproj", "{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}"
3535
EndProject
36+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{04898517-CD60-419B-9C4D-2BE8A99C40FC}"
37+
EndProject
3638
Global
3739
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3840
Debug|Any CPU = Debug|Any CPU
@@ -99,6 +101,10 @@ Global
99101
{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
100102
{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
101103
{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}.Release|Any CPU.Build.0 = Release|Any CPU
104+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
105+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
106+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
107+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Release|Any CPU.Build.0 = Release|Any CPU
102108
EndGlobalSection
103109
GlobalSection(SolutionProperties) = preSolution
104110
HideSolutionNode = FALSE

IteratorPattern/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
5+
</startup>
6+
</configuration>

IteratorPattern/Iterator.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IteratorPattern
8+
{
9+
// 抽象聚合类
10+
public interface IListCollection
11+
{
12+
Iterator GetIterator();
13+
}
14+
15+
// 迭代器抽象类
16+
public interface Iterator
17+
{
18+
bool MoveNext();
19+
Object GetCurrent();
20+
void Next();
21+
void Reset();
22+
}
23+
24+
// 具体聚合类
25+
public class ConcreteList : IListCollection
26+
{
27+
readonly string[] _collection;
28+
public ConcreteList()
29+
{
30+
_collection = new string[] { "A", "B", "C", "D" };
31+
}
32+
33+
public Iterator GetIterator()
34+
{
35+
return new ConcreteIterator(this);
36+
}
37+
38+
public int Length
39+
{
40+
get { return _collection.Length; }
41+
}
42+
43+
public string GetElement(int index)
44+
{
45+
return _collection[index];
46+
}
47+
}
48+
49+
// 具体迭代器类
50+
public class ConcreteIterator : Iterator
51+
{
52+
// 迭代器要集合对象进行遍历操作,自然就需要引用集合对象
53+
private ConcreteList _list;
54+
private int _index;
55+
56+
public ConcreteIterator(ConcreteList list)
57+
{
58+
_list = list;
59+
_index = 0;
60+
}
61+
62+
public bool MoveNext()
63+
{
64+
if (_index < _list.Length)
65+
{
66+
return true;
67+
}
68+
return false;
69+
}
70+
71+
public Object GetCurrent()
72+
{
73+
return _list.GetElement(_index);
74+
}
75+
76+
public void Reset()
77+
{
78+
_index = 0;
79+
}
80+
81+
public void Next()
82+
{
83+
if (_index < _list.Length)
84+
{
85+
_index++;
86+
}
87+
88+
}
89+
}
90+
91+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{04898517-CD60-419B-9C4D-2BE8A99C40FC}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>IteratorPattern</RootNamespace>
11+
<AssemblyName>IteratorPattern</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Iterator.cs" />
47+
<Compile Include="Program.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55+
Other similar extension points exist, see Microsoft.Common.targets.
56+
<Target Name="BeforeBuild">
57+
</Target>
58+
<Target Name="AfterBuild">
59+
</Target>
60+
-->
61+
</Project>

IteratorPattern/Program.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IteratorPattern
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
Console.WriteLine("迭代器模式:");
14+
IListCollection list = new ConcreteList();
15+
var iterator = list.GetIterator();
16+
17+
while (iterator.MoveNext())
18+
{
19+
int i = (int)iterator.GetCurrent();
20+
Console.WriteLine(i.ToString());
21+
iterator.Next();
22+
}
23+
24+
Console.Read();
25+
}
26+
}
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的一般信息由以下
6+
// 控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("IteratorPattern")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("IteratorPattern")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
//将 ComVisible 设置为 false 将使此程序集中的类型
18+
//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
19+
//请将此类型的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("04898517-cd60-419b-9c4d-2be8a99c40fc")]
24+
25+
// 程序集的版本信息由下列四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”: :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)