From 56857c00d2ca3ca0050eebcd48281e34e6137a9e Mon Sep 17 00:00:00 2001 From: syeffort Date: Fri, 7 Apr 2023 17:23:09 +0900 Subject: [PATCH] iterator --- DesignPatternGuru/DesignPatternGuru.sln | 9 +++- .../Iterator/Collections/WordsCollection.cs | 37 +++++++++++++ DesignPatternGuru/Iterator/Iterator.csproj | 10 ++++ .../Iterators/AlphabeticalOrderIterator.cs | 54 +++++++++++++++++++ .../Iterator/Iterators/Iterator.cs | 22 ++++++++ .../Iterator/Iterators/IteratorAggregate.cs | 14 +++++ DesignPatternGuru/Iterator/Program.cs | 30 +++++++++++ 7 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 DesignPatternGuru/Iterator/Collections/WordsCollection.cs create mode 100644 DesignPatternGuru/Iterator/Iterator.csproj create mode 100644 DesignPatternGuru/Iterator/Iterators/AlphabeticalOrderIterator.cs create mode 100644 DesignPatternGuru/Iterator/Iterators/Iterator.cs create mode 100644 DesignPatternGuru/Iterator/Iterators/IteratorAggregate.cs create mode 100644 DesignPatternGuru/Iterator/Program.cs diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index 32be10d..6ddec7a 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -35,7 +35,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BehavioralPatterns", "Behav EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibility", "ChainOfResponsibility\ChainOfResponsibility.csproj", "{9626EF73-1F68-46C8-B7C1-434F02768452}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "Command\Command.csproj", "{629A51B1-04D9-4636-8E21-1712DCFF21DF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command", "Command\Command.csproj", "{629A51B1-04D9-4636-8E21-1712DCFF21DF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Iterator\Iterator.csproj", "{6619433E-476B-404C-94A0-D0C0FE52DE41}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -99,6 +101,10 @@ Global {629A51B1-04D9-4636-8E21-1712DCFF21DF}.Debug|Any CPU.Build.0 = Debug|Any CPU {629A51B1-04D9-4636-8E21-1712DCFF21DF}.Release|Any CPU.ActiveCfg = Release|Any CPU {629A51B1-04D9-4636-8E21-1712DCFF21DF}.Release|Any CPU.Build.0 = Release|Any CPU + {6619433E-476B-404C-94A0-D0C0FE52DE41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6619433E-476B-404C-94A0-D0C0FE52DE41}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6619433E-476B-404C-94A0-D0C0FE52DE41}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6619433E-476B-404C-94A0-D0C0FE52DE41}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -118,6 +124,7 @@ Global {F0E63700-B550-4D57-B912-18C28B3FEAC6} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {9626EF73-1F68-46C8-B7C1-434F02768452} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} {629A51B1-04D9-4636-8E21-1712DCFF21DF} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} + {6619433E-476B-404C-94A0-D0C0FE52DE41} = {E3B8A482-1A9A-4B6E-8310-050A44C8E379} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} diff --git a/DesignPatternGuru/Iterator/Collections/WordsCollection.cs b/DesignPatternGuru/Iterator/Collections/WordsCollection.cs new file mode 100644 index 0000000..cc36ed5 --- /dev/null +++ b/DesignPatternGuru/Iterator/Collections/WordsCollection.cs @@ -0,0 +1,37 @@ +using Iterator.Iterators; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Iterator.Collections +{ + internal class WordsCollection : IteratorAggregate + { + List _collection = new List(); + + bool _direction = false; + + public void ReverseDirection() + { + _direction = !_direction; + } + + public List GetItems() + { + return _collection; + } + + public void AddItem(string item) + { + _collection.Add(item); + } + + public override IEnumerator GetEnumerator() + { + return new AlphabeticalOrderIterator(this, _direction); + } + } +} diff --git a/DesignPatternGuru/Iterator/Iterator.csproj b/DesignPatternGuru/Iterator/Iterator.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/Iterator/Iterator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/Iterator/Iterators/AlphabeticalOrderIterator.cs b/DesignPatternGuru/Iterator/Iterators/AlphabeticalOrderIterator.cs new file mode 100644 index 0000000..54a9dd0 --- /dev/null +++ b/DesignPatternGuru/Iterator/Iterators/AlphabeticalOrderIterator.cs @@ -0,0 +1,54 @@ +using Iterator.Collections; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Iterator.Iterators +{ + internal class AlphabeticalOrderIterator : Iterator + { + private WordsCollection _collection; + + private int _position = -1; + + private bool _reverse = false; + + public AlphabeticalOrderIterator(WordsCollection collection, bool reverse = false) + { + _collection = collection; + _reverse = reverse; + + if (_reverse) + { + _position = collection.GetItems().Count; + } + } + + public override object Current() + { + return _collection.GetItems()[_position]; + } + + public override int Key() + { + return _position; + } + + public override bool MoveNext() + { + int updatedPosition = _position + (_reverse ? -1: 1); + if (updatedPosition < 0 || updatedPosition >= _collection.GetItems().Count) + return false; + + _position = updatedPosition; + return true; + } + + public override void Reset() + { + _position = _reverse ? _collection.GetItems().Count - 1 : 0; + } + } +} diff --git a/DesignPatternGuru/Iterator/Iterators/Iterator.cs b/DesignPatternGuru/Iterator/Iterators/Iterator.cs new file mode 100644 index 0000000..d4d67db --- /dev/null +++ b/DesignPatternGuru/Iterator/Iterators/Iterator.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Iterator.Iterators +{ + abstract class Iterator : IEnumerator + { + object IEnumerator.Current => Current(); + + public abstract int Key(); + + public abstract object Current(); + + public abstract bool MoveNext(); + + public abstract void Reset(); + } +} diff --git a/DesignPatternGuru/Iterator/Iterators/IteratorAggregate.cs b/DesignPatternGuru/Iterator/Iterators/IteratorAggregate.cs new file mode 100644 index 0000000..70ebee1 --- /dev/null +++ b/DesignPatternGuru/Iterator/Iterators/IteratorAggregate.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Iterator.Iterators +{ + abstract class IteratorAggregate : IEnumerable + { + public abstract IEnumerator GetEnumerator(); + } +} diff --git a/DesignPatternGuru/Iterator/Program.cs b/DesignPatternGuru/Iterator/Program.cs new file mode 100644 index 0000000..54214e5 --- /dev/null +++ b/DesignPatternGuru/Iterator/Program.cs @@ -0,0 +1,30 @@ +using Iterator.Collections; + +namespace Iterator +{ + internal class Program + { + static void Main(string[] args) + { + WordsCollection collection = new WordsCollection(); + collection.AddItem("First"); + collection.AddItem("Second"); + collection.AddItem("Third"); + + Console.WriteLine("Straight traversal: "); + foreach (var element in collection) + { + Console.WriteLine(element); + } + + Console.WriteLine(); + + Console.WriteLine("Reverse traversal: "); + collection.ReverseDirection(); + foreach (var element in collection) + { + Console.WriteLine(element); + } + } + } +} \ No newline at end of file