Add Iterator pattern
This commit is contained in:
@@ -16,6 +16,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ChainOfResponssibility", "s
|
||||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "CommandPattern", "src\CommandPattern\CommandPattern.xproj", "{454B2A43-8251-4667-8DE3-67E489908DB9}"
|
||||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "IteratorPattern", "src\IteratorPattern\IteratorPattern.xproj", "{6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -34,6 +36,10 @@ Global
|
||||
{454B2A43-8251-4667-8DE3-67E489908DB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{454B2A43-8251-4667-8DE3-67E489908DB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{454B2A43-8251-4667-8DE3-67E489908DB9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -42,5 +48,6 @@ Global
|
||||
{E3092EE0-1282-4AB4-9FA2-0338348D8FD1} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||
{89536824-683F-4351-8789-406D7BDD922D} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||
{454B2A43-8251-4667-8DE3-67E489908DB9} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||
{6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using ChainOfResponssibility;
|
||||
using ChainOfResponssibility.PurchaseExample;
|
||||
using CommandPattern;
|
||||
using IteratorPattern;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
@@ -12,13 +13,17 @@ namespace BehavioralPatterns
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
{
|
||||
IteratorPatternExamples.Run();
|
||||
Console.ReadKey();
|
||||
//Chain of responsibillity
|
||||
//This is usefull when you have a request and you don't know who should process it
|
||||
ChainOfResponsibillityExamples.Run();
|
||||
Console.ReadKey();
|
||||
|
||||
CommandPatternExamples.Run();
|
||||
Console.ReadKey();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
"buildOptions": {
|
||||
"emitEntryPoint": true
|
||||
"emitEntryPoint": true,
|
||||
"copyToOutput": {
|
||||
"include": "../IteratorPattern/FileExample/SampleFiles/**"
|
||||
}
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"ChainOfResponssibility": "1.0.0-*",
|
||||
"CommandPattern": "1.0.0-*",
|
||||
"IteratorPattern": "1.0.0-*",
|
||||
"Microsoft.NETCore.App": {
|
||||
"type": "platform",
|
||||
"version": "1.0.0-rc2-3002702"
|
||||
|
||||
36
src/IteratorPattern/FileExample/LineReader.cs
Normal file
36
src/IteratorPattern/FileExample/LineReader.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.FileExample
|
||||
{
|
||||
public class LineReader : IEnumerable<string>
|
||||
{
|
||||
public string FilePath { get; private set; }
|
||||
public LineReader(string filePath)
|
||||
{
|
||||
FilePath = filePath;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<string> GetEnumerator()
|
||||
{
|
||||
using (TextReader reader = File.OpenText(FilePath))
|
||||
{
|
||||
string line;
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
{
|
||||
yield return line;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
30
src/IteratorPattern/FileExample/ReadBigFilesExample.cs
Normal file
30
src/IteratorPattern/FileExample/ReadBigFilesExample.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.FileExample
|
||||
{
|
||||
public class ReadBigFilesExample
|
||||
{
|
||||
public void Run()
|
||||
{
|
||||
string[] files = Directory.GetFiles("bin/Debug/IteratorPattern/FileExample/SampleFiles");
|
||||
var filesWithContent = from file in files
|
||||
where HasAnyLines(file)
|
||||
select file;
|
||||
|
||||
foreach (var fileWithContent in filesWithContent)
|
||||
{
|
||||
Console.WriteLine("File with content: {0}", fileWithContent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private bool HasAnyLines(string file)
|
||||
{
|
||||
return new LineReader(file).Any();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/IteratorPattern/IteratorPattern.xproj
Normal file
21
src/IteratorPattern/IteratorPattern.xproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>6f3b7f9a-4d9c-4506-a5f7-3ff5cf4376bd</ProjectGuid>
|
||||
<RootNamespace>IteratorPattern</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
29
src/IteratorPattern/IteratorPatternExamples.cs
Normal file
29
src/IteratorPattern/IteratorPatternExamples.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using IteratorPattern.FileExample;
|
||||
using IteratorPattern.TVExample.TVEnumerable;
|
||||
using IteratorPattern.TvIterator;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern
|
||||
{
|
||||
// This project can output the Class library as a NuGet Package.
|
||||
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
|
||||
public class IteratorPatternExamples
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
ReadBigFilesExample bigFileExample = new ReadBigFilesExample();
|
||||
bigFileExample.Run();
|
||||
|
||||
TVIteratorExample tvIteratorExample = new TVIteratorExample();
|
||||
tvIteratorExample.Run();
|
||||
|
||||
TVEnumerableExample tvEnumerableExample = new TVEnumerableExample();
|
||||
tvEnumerableExample.Run();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/IteratorPattern/Properties/AssemblyInfo.cs
Normal file
19
src/IteratorPattern/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Hewlett-Packard Company")]
|
||||
[assembly: AssemblyProduct("IteratorPattern")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("6f3b7f9a-4d9c-4506-a5f7-3ff5cf4376bd")]
|
||||
7
src/IteratorPattern/TVExample/Channel.cs
Normal file
7
src/IteratorPattern/TVExample/Channel.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace IteratorPattern.TVExample
|
||||
{
|
||||
public class Channel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
22
src/IteratorPattern/TVExample/TVCableSupplier.cs
Normal file
22
src/IteratorPattern/TVExample/TVCableSupplier.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TVExample
|
||||
{
|
||||
public class TVCableSupplier
|
||||
{
|
||||
public TVCableSupplier()
|
||||
{
|
||||
Channels = new List<Channel> {
|
||||
new Channel { Name = "DIGI24"},
|
||||
new Channel { Name = "Acasa TV"},
|
||||
new Channel { Name = "Taraf"},
|
||||
new Channel { Name = "Manele TV"}
|
||||
};
|
||||
}
|
||||
|
||||
public List<Channel> Channels { get; private set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TVExample.TVEnumerable
|
||||
{
|
||||
public class SamsungChannelIterator : IEnumerator<Channel>
|
||||
{
|
||||
int currPos;
|
||||
TVCableSupplier cableSupplier;
|
||||
public SamsungChannelIterator(TVCableSupplier cableSupplier)
|
||||
{
|
||||
this.cableSupplier = cableSupplier;
|
||||
currPos = -1;
|
||||
}
|
||||
|
||||
public Channel Current { get { return cableSupplier.Channels[currPos]; } }
|
||||
|
||||
object IEnumerator.Current { get { return cableSupplier.Channels[currPos]; } }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if(currPos < cableSupplier.Channels.Count -1)
|
||||
{
|
||||
currPos++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
currPos = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/IteratorPattern/TVExample/TVEnumerable/SamsungTV.cs
Normal file
27
src/IteratorPattern/TVExample/TVEnumerable/SamsungTV.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TVExample.TVEnumerable
|
||||
{
|
||||
public class SamsungTV : IEnumerable<Channel>
|
||||
{
|
||||
TVCableSupplier cableSupplier;
|
||||
public SamsungTV(TVCableSupplier cableSupplier)
|
||||
{
|
||||
this.cableSupplier = cableSupplier;
|
||||
}
|
||||
|
||||
public IEnumerator<Channel> GetEnumerator()
|
||||
{
|
||||
return new SamsungChannelIterator(cableSupplier);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return new SamsungChannelIterator(cableSupplier);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TVExample.TVEnumerable
|
||||
{
|
||||
public class TVEnumerableExample
|
||||
{
|
||||
public void Run()
|
||||
{
|
||||
IEnumerable<Channel> tv = new SamsungTV(new TVCableSupplier());
|
||||
foreach (var channel in tv)
|
||||
{
|
||||
Console.WriteLine(channel.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/IteratorPattern/TVExample/TvIterator/ChannelIterator.cs
Normal file
21
src/IteratorPattern/TVExample/TvIterator/ChannelIterator.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using IteratorPattern.TVExample;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TvIterator
|
||||
{
|
||||
/// <summary>
|
||||
/// Iterator interface
|
||||
/// The Iterator defines the interface for access and traversal of the elements
|
||||
/// </summary>
|
||||
public interface ChannelIterator
|
||||
{
|
||||
bool HasNext();
|
||||
|
||||
Channel Current { get; }
|
||||
|
||||
Channel MoveNext();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using IteratorPattern.TVExample;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TvIterator
|
||||
{
|
||||
/// <summary>
|
||||
/// Concrete implementation of Iterator interface
|
||||
/// </summary>
|
||||
public class SamsungChannelIterator : ChannelIterator
|
||||
{
|
||||
int currentPos;
|
||||
TVCableSupplier cableSupplier;
|
||||
public SamsungChannelIterator(TVCableSupplier cableSupplier)
|
||||
{
|
||||
currentPos = 0;
|
||||
this.cableSupplier = cableSupplier;
|
||||
}
|
||||
public Channel Current
|
||||
{
|
||||
get
|
||||
{
|
||||
return cableSupplier.Channels[currentPos];
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasNext()
|
||||
{
|
||||
return currentPos < cableSupplier.Channels.Count;
|
||||
|
||||
}
|
||||
|
||||
public Channel MoveNext()
|
||||
{
|
||||
return cableSupplier.Channels[currentPos++];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using IteratorPattern.TVExample;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TvIterator
|
||||
{
|
||||
/// <summary>
|
||||
/// Concrete implementation of Aggregate interface
|
||||
/// </summary>
|
||||
public class SamsungTV : TV
|
||||
{
|
||||
TVCableSupplier cableSupplier;
|
||||
public SamsungTV(TVCableSupplier cableSupplier)
|
||||
{
|
||||
this.cableSupplier = cableSupplier;
|
||||
}
|
||||
public ChannelIterator GetIterator()
|
||||
{
|
||||
return new SamsungChannelIterator(cableSupplier);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/IteratorPattern/TVExample/TvIterator/TV.cs
Normal file
16
src/IteratorPattern/TVExample/TvIterator/TV.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TvIterator
|
||||
{
|
||||
/// <summary>
|
||||
/// Aggregate interface - get iterator
|
||||
/// The Aggregate defines an interface for the creation of the Iterator object.
|
||||
/// </summary>
|
||||
public interface TV
|
||||
{
|
||||
ChannelIterator GetIterator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using IteratorPattern.TVExample;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IteratorPattern.TvIterator
|
||||
{
|
||||
public class TVIteratorExample
|
||||
{
|
||||
public void Run()
|
||||
{
|
||||
TV tv = new SamsungTV(new TVCableSupplier());
|
||||
ChannelIterator iterator = tv.GetIterator();
|
||||
|
||||
do
|
||||
{
|
||||
Console.WriteLine("Current channel is: " + iterator.Current.Name);
|
||||
iterator.MoveNext();
|
||||
|
||||
} while (iterator.HasNext());
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/IteratorPattern/project.json
Normal file
13
src/IteratorPattern/project.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"netstandard1.5": {
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user