From b44574781072f061e0cbd8bcccc4bc1335155593 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 11 May 2021 20:36:07 +0300 Subject: [PATCH 1/2] itterator --- DotNetTest/DotNetTest/ExampleCollection.cs | 17 +++++- DotNetTest/DotNetTest/Iterator.cs | 67 ++++++++++++++++++++++ DotNetTest/DotNetTest/Program.cs | 24 ++------ 3 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 DotNetTest/DotNetTest/Iterator.cs diff --git a/DotNetTest/DotNetTest/ExampleCollection.cs b/DotNetTest/DotNetTest/ExampleCollection.cs index c77fb7a..269e2d6 100644 --- a/DotNetTest/DotNetTest/ExampleCollection.cs +++ b/DotNetTest/DotNetTest/ExampleCollection.cs @@ -1,10 +1,18 @@ -using System.Collections.Generic; +using System.Collections; +using System.Collections.Generic; namespace DotNetTest { - public class ExampleCollection + public class ExampleCollection : IteratorAggregate { private readonly List _collection = new List(); + + bool _direction = true; + + public void ReverseDirection() + { + _direction = !_direction; + } public int Length => _collection.Count; @@ -23,5 +31,10 @@ public string this[int index] get => _collection[index]; set => _collection[index] = value; } + + public override IEnumerator GetEnumerator() + { + return new AlphabeticalOrderIterator(this, _direction); + } } } \ No newline at end of file diff --git a/DotNetTest/DotNetTest/Iterator.cs b/DotNetTest/DotNetTest/Iterator.cs new file mode 100644 index 0000000..8fa3391 --- /dev/null +++ b/DotNetTest/DotNetTest/Iterator.cs @@ -0,0 +1,67 @@ +using System.Collections; + +namespace DotNetTest +{ + abstract class Iterator : IEnumerator + { + object IEnumerator.Current => Current(); + public abstract int Key(); + public abstract object Current(); + public abstract bool MoveNext(); + public abstract void Reset(); + } + + public abstract class IteratorAggregate : IEnumerable + { + public abstract IEnumerator GetEnumerator(); + } + + class AlphabeticalOrderIterator : Iterator + { + private ExampleCollection _collection; + + private int _position = -1; + + private bool _reverse = false; + + public AlphabeticalOrderIterator(ExampleCollection collection, bool reverse = false) + { + this._collection = collection; + this._reverse = reverse; + + if (reverse) + { + this._position = collection.GetItems().Count; + } + } + + public override object Current() + { + return this._collection.GetItems()[_position]; + } + + public override int Key() + { + return this._position; + } + + public override bool MoveNext() + { + var updatedPosition = this._position + (this._reverse ? -1 : 1); + if (updatedPosition >= 0 && updatedPosition < this._collection.GetItems().Count) + { + this._position = updatedPosition; + return true; + } + else + { + return false; + } + } + + public override void Reset() + { + this._position = this._reverse ? this._collection.GetItems().Count - 1 : 0; + } + } +} \ No newline at end of file diff --git a/DotNetTest/DotNetTest/Program.cs b/DotNetTest/DotNetTest/Program.cs index 350c57c..1979d30 100644 --- a/DotNetTest/DotNetTest/Program.cs +++ b/DotNetTest/DotNetTest/Program.cs @@ -6,25 +6,6 @@ namespace DotNetTest { class Program { - public static ExampleCollection AlphabeticalOrderSort(ExampleCollection collection) - { - string temp; - for (var i = 0; i < collection.Length; i++) - { - for (var j = i + 1; j < collection.Length; j++) - { - if (collection[i].ToCharArray()[0] > collection[j].ToCharArray()[0]) - { - temp = collection[i]; - collection[i] = collection[j]; - collection[j] = temp; - } - } - } - - return collection; - } - static void Main(string[] args) { var collection = new ExampleCollection(); @@ -32,7 +13,10 @@ static void Main(string[] args) collection.AddItem("bar"); collection.AddItem("a"); - var sorted = AlphabeticalOrderSort(collection); + foreach (var element in collection) + { + Console.WriteLine(element); + } } } From d3ae175005b15dce627d0aab5eca0f0c668d2d1f Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 11 May 2021 20:47:30 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BE=D1=82=D0=B2=D0=B5=D1=82=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=D0=BE=D0=BF=D1=80=D0=BE=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\262\320\276\320\277\321\200\320\276\321\201.txt" | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 "DotNetTest/10 \320\262\320\276\320\277\321\200\320\276\321\201.txt" diff --git "a/DotNetTest/10 \320\262\320\276\320\277\321\200\320\276\321\201.txt" "b/DotNetTest/10 \320\262\320\276\320\277\321\200\320\276\321\201.txt" new file mode 100644 index 0000000..2b6ced0 --- /dev/null +++ "b/DotNetTest/10 \320\262\320\276\320\277\321\200\320\276\321\201.txt" @@ -0,0 +1,10 @@ +Ложный агрегат - это большой сложный агрегат, который не отражает действительность и, скорее всего, имеет круговые зависимости. +Например Юзер имеет список Заказов, Комментов, Настроек и мы связываем эти коллекции в две стороны. +Кроме того, такой агрегат становится антипаттерном Божжественным объектом. +Ложный агрегат в реальном мире не существует. Например, скорее всего нет такого инварианта, что изменение одного Заказа, должно влиять на другие Заказы => нам не нужен список всех заказов у Юзера и тд. +Чтобы это исправить нужно изменить агрегат. Чтобы правильно определить корень агрегата, нужно ответить на следующие вопросы: +1) есть ли аналог в реальном мире? +2) не пересекает ли агрегат Bounded Context? +3) Есть ли инвариант для этой группы объектов? +4) Данные должны изменяться в рамках одной транзакции? +В нашем случае ответ на всё нет, поэтому нужно разделить этот агрегат на три маленьких, уменьшив количество циклических зависимостей. \ No newline at end of file