diff --git a/BehavioralPatterns.sln b/BehavioralPatterns.sln index cb539ee..7926f0b 100644 --- a/BehavioralPatterns.sln +++ b/BehavioralPatterns.sln @@ -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 diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index 9cd1b02..8b6181e 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -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(); + } } } diff --git a/src/BehavioralPatterns/project.json b/src/BehavioralPatterns/project.json index ba86bcd..f699cfa 100644 --- a/src/BehavioralPatterns/project.json +++ b/src/BehavioralPatterns/project.json @@ -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" diff --git a/src/IteratorPattern/FileExample/LineReader.cs b/src/IteratorPattern/FileExample/LineReader.cs new file mode 100644 index 0000000..fca7572 --- /dev/null +++ b/src/IteratorPattern/FileExample/LineReader.cs @@ -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 + { + public string FilePath { get; private set; } + public LineReader(string filePath) + { + FilePath = filePath; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public IEnumerator GetEnumerator() + { + using (TextReader reader = File.OpenText(FilePath)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + yield return line; + } + } + } + } + +} diff --git a/src/IteratorPattern/FileExample/ReadBigFilesExample.cs b/src/IteratorPattern/FileExample/ReadBigFilesExample.cs new file mode 100644 index 0000000..d15d809 --- /dev/null +++ b/src/IteratorPattern/FileExample/ReadBigFilesExample.cs @@ -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(); + } + } +} diff --git a/src/IteratorPattern/IteratorPattern.xproj b/src/IteratorPattern/IteratorPattern.xproj new file mode 100644 index 0000000..44bd293 --- /dev/null +++ b/src/IteratorPattern/IteratorPattern.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + 6f3b7f9a-4d9c-4506-a5f7-3ff5cf4376bd + IteratorPattern + .\obj + .\bin\ + v4.6.1 + + + + 2.0 + + + diff --git a/src/IteratorPattern/IteratorPatternExamples.cs b/src/IteratorPattern/IteratorPatternExamples.cs new file mode 100644 index 0000000..83b2de0 --- /dev/null +++ b/src/IteratorPattern/IteratorPatternExamples.cs @@ -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(); + + + } + } +} diff --git a/src/IteratorPattern/Properties/AssemblyInfo.cs b/src/IteratorPattern/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..791e94a --- /dev/null +++ b/src/IteratorPattern/Properties/AssemblyInfo.cs @@ -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")] diff --git a/src/IteratorPattern/TVExample/Channel.cs b/src/IteratorPattern/TVExample/Channel.cs new file mode 100644 index 0000000..a37d0ef --- /dev/null +++ b/src/IteratorPattern/TVExample/Channel.cs @@ -0,0 +1,7 @@ +namespace IteratorPattern.TVExample +{ + public class Channel + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/IteratorPattern/TVExample/TVCableSupplier.cs b/src/IteratorPattern/TVExample/TVCableSupplier.cs new file mode 100644 index 0000000..c2798f7 --- /dev/null +++ b/src/IteratorPattern/TVExample/TVCableSupplier.cs @@ -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 { + new Channel { Name = "DIGI24"}, + new Channel { Name = "Acasa TV"}, + new Channel { Name = "Taraf"}, + new Channel { Name = "Manele TV"} + }; + } + + public List Channels { get; private set; } + } +} diff --git a/src/IteratorPattern/TVExample/TVEnumerable/SamsungChannelIterator.cs b/src/IteratorPattern/TVExample/TVEnumerable/SamsungChannelIterator.cs new file mode 100644 index 0000000..7bdfec8 --- /dev/null +++ b/src/IteratorPattern/TVExample/TVEnumerable/SamsungChannelIterator.cs @@ -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 + { + 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; + } + } +} diff --git a/src/IteratorPattern/TVExample/TVEnumerable/SamsungTV.cs b/src/IteratorPattern/TVExample/TVEnumerable/SamsungTV.cs new file mode 100644 index 0000000..137d756 --- /dev/null +++ b/src/IteratorPattern/TVExample/TVEnumerable/SamsungTV.cs @@ -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 + { + TVCableSupplier cableSupplier; + public SamsungTV(TVCableSupplier cableSupplier) + { + this.cableSupplier = cableSupplier; + } + + public IEnumerator GetEnumerator() + { + return new SamsungChannelIterator(cableSupplier); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return new SamsungChannelIterator(cableSupplier); + } + } +} diff --git a/src/IteratorPattern/TVExample/TVEnumerable/TVEnumerableExample.cs b/src/IteratorPattern/TVExample/TVEnumerable/TVEnumerableExample.cs new file mode 100644 index 0000000..4ea9b57 --- /dev/null +++ b/src/IteratorPattern/TVExample/TVEnumerable/TVEnumerableExample.cs @@ -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 tv = new SamsungTV(new TVCableSupplier()); + foreach (var channel in tv) + { + Console.WriteLine(channel.Name); + } + } + } +} diff --git a/src/IteratorPattern/TVExample/TvIterator/ChannelIterator.cs b/src/IteratorPattern/TVExample/TvIterator/ChannelIterator.cs new file mode 100644 index 0000000..dfaa4f4 --- /dev/null +++ b/src/IteratorPattern/TVExample/TvIterator/ChannelIterator.cs @@ -0,0 +1,21 @@ +using IteratorPattern.TVExample; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace IteratorPattern.TvIterator +{ + /// + /// Iterator interface + /// The Iterator defines the interface for access and traversal of the elements + /// + public interface ChannelIterator + { + bool HasNext(); + + Channel Current { get; } + + Channel MoveNext(); + } +} diff --git a/src/IteratorPattern/TVExample/TvIterator/Samsung/SamsungChannelIterator.cs b/src/IteratorPattern/TVExample/TvIterator/Samsung/SamsungChannelIterator.cs new file mode 100644 index 0000000..a043bc7 --- /dev/null +++ b/src/IteratorPattern/TVExample/TvIterator/Samsung/SamsungChannelIterator.cs @@ -0,0 +1,40 @@ +using IteratorPattern.TVExample; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace IteratorPattern.TvIterator +{ + /// + /// Concrete implementation of Iterator interface + /// + 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++]; + } + } +} diff --git a/src/IteratorPattern/TVExample/TvIterator/Samsung/SamsungTV.cs b/src/IteratorPattern/TVExample/TvIterator/Samsung/SamsungTV.cs new file mode 100644 index 0000000..7e2d2c9 --- /dev/null +++ b/src/IteratorPattern/TVExample/TvIterator/Samsung/SamsungTV.cs @@ -0,0 +1,24 @@ +using IteratorPattern.TVExample; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace IteratorPattern.TvIterator +{ + /// + /// Concrete implementation of Aggregate interface + /// + public class SamsungTV : TV + { + TVCableSupplier cableSupplier; + public SamsungTV(TVCableSupplier cableSupplier) + { + this.cableSupplier = cableSupplier; + } + public ChannelIterator GetIterator() + { + return new SamsungChannelIterator(cableSupplier); + } + } +} diff --git a/src/IteratorPattern/TVExample/TvIterator/TV.cs b/src/IteratorPattern/TVExample/TvIterator/TV.cs new file mode 100644 index 0000000..f81183e --- /dev/null +++ b/src/IteratorPattern/TVExample/TvIterator/TV.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace IteratorPattern.TvIterator +{ + /// + /// Aggregate interface - get iterator + /// The Aggregate defines an interface for the creation of the Iterator object. + /// + public interface TV + { + ChannelIterator GetIterator(); + } +} diff --git a/src/IteratorPattern/TVExample/TvIterator/TvIteratorExample.cs b/src/IteratorPattern/TVExample/TvIterator/TvIteratorExample.cs new file mode 100644 index 0000000..32f7622 --- /dev/null +++ b/src/IteratorPattern/TVExample/TvIterator/TvIteratorExample.cs @@ -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()); + } + } +} diff --git a/src/IteratorPattern/project.json b/src/IteratorPattern/project.json new file mode 100644 index 0000000..8f8aaaf --- /dev/null +++ b/src/IteratorPattern/project.json @@ -0,0 +1,13 @@ +{ + "version": "1.0.0-*", + + "dependencies": { + "NETStandard.Library": "1.5.0-rc2-24027" + }, + + "frameworks": { + "netstandard1.5": { + "imports": "dnxcore50" + } + } +}