Add Iterator pattern

This commit is contained in:
Petrutiu Mihai
2016-06-22 19:37:05 +03:00
parent 1ab706016e
commit 35c3c5722d
19 changed files with 409 additions and 2 deletions

View File

@@ -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();
}
}
}

View File

@@ -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"

View 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;
}
}
}
}
}

View 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();
}
}
}

View 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>

View 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();
}
}
}

View 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")]

View File

@@ -0,0 +1,7 @@
namespace IteratorPattern.TVExample
{
public class Channel
{
public string Name { get; set; }
}
}

View 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; }
}
}

View File

@@ -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;
}
}
}

View 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);
}
}
}

View File

@@ -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);
}
}
}
}

View 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();
}
}

View File

@@ -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++];
}
}
}

View File

@@ -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);
}
}
}

View 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();
}
}

View File

@@ -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());
}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"netstandard1.5": {
"imports": "dnxcore50"
}
}
}