From f6db7aa935724772ca272de52d3ac0a8045f01ce Mon Sep 17 00:00:00 2001 From: israel Date: Fri, 20 Oct 2017 13:14:29 +0100 Subject: [PATCH] =?UTF-8?q?=C2=B7=20Insert=20comments.=20=C2=B7=20DoNothin?= =?UTF-8?q?gStrategy=20created.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csharp/GildedRoseTest.cs | 33 ++++++++++++------- .../CategoryStrategiesFactory.cs | 5 ++- .../GildedRoseStrategyPatternExample.cs | 15 +++++++++ .../ICategoryStrategy.cs | 3 ++ .../ItemWrapperContext.cs | 3 ++ .../CloseExpiredImproveQualityStrategy.cs | 3 ++ .../Strategies/DoNothingStrategy.cs | 18 ++++++++++ .../Strategies/NormalDegradeStrategy.cs | 10 +++++- .../Strategies/OlderIsBetterStrategy.cs | 7 ++++ .../TwiceFastDegradeQualityStrategy.cs | 9 ++++- csharp/csharp.csproj | 1 + 11 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 csharp/StrategyPatternExample/Strategies/DoNothingStrategy.cs diff --git a/csharp/GildedRoseTest.cs b/csharp/GildedRoseTest.cs index 07dbf477..1025bede 100644 --- a/csharp/GildedRoseTest.cs +++ b/csharp/GildedRoseTest.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using csharp.StrategyPatternExample; +using NUnit.Framework; using System.Collections.Generic; namespace csharp @@ -6,19 +7,10 @@ namespace csharp [TestFixture] public class GildedRoseTest { - [Test] - public void foo() - { - IList Items = new List { new Item { Name = "foo", SellIn = 0, Quality = 0 } }; - GildedRose app = new GildedRose(Items); - app.UpdateQuality(); - Assert.AreNotEqual("fixme", Items[0].Name); - } - [Test] public void ConjuredQuality() { - IList Items = new List { new Item { Name = "Conjured Mana Cake", SellIn = 1, Quality = 8 } }; + IList Items = new List { new Item { Name = Global.NAME_ITEM_CONJURED, SellIn = 1, Quality = 8 } }; GildedRose app = new GildedRose(Items); // "Conjured" items degrade in Quality twice as fast as normal items. @@ -31,5 +23,24 @@ namespace csharp app.UpdateQuality(); Assert.AreEqual(2, Items[0].Quality); } + + [Test] + public void ConjuredQuality_StrategyPatternExample() + { + IList Items = new List { new Item { Name = Global.NAME_ITEM_CONJURED, SellIn = 1, Quality = 8 } }; + GildedRoseStrategyPatternExample app = new GildedRoseStrategyPatternExample(Items); + + // "Conjured" items degrade in Quality twice as fast as normal items. + // So, + // SellIn >= 0 => degrade = -2 + // SellIn < 0 => degrade = -4 + app.UpdateQuality(); + Assert.AreEqual(6, Items[0].Quality); + + app.UpdateQuality(); + Assert.AreEqual(2, Items[0].Quality); + } + + } } diff --git a/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs index a0fb1342..45dac99a 100644 --- a/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs +++ b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs @@ -4,7 +4,10 @@ using System.Collections.Generic; namespace csharp.StrategyPatternExample { - class CategoryStrategiesFactory + /// + /// This class is responsible for providing a category strategy to a given item. + /// + internal class CategoryStrategiesFactory { #region Variables diff --git a/csharp/StrategyPatternExample/GildedRoseStrategyPatternExample.cs b/csharp/StrategyPatternExample/GildedRoseStrategyPatternExample.cs index 8d5febd0..5f9204cc 100644 --- a/csharp/StrategyPatternExample/GildedRoseStrategyPatternExample.cs +++ b/csharp/StrategyPatternExample/GildedRoseStrategyPatternExample.cs @@ -4,15 +4,28 @@ using csharp.StrategyPatternExample; namespace csharp.StrategyPatternExample { + /// + /// GildedRose class refactored to adapt the strategy pattern without making a great refactor of main method of Program.cs. + /// public class GildedRoseStrategyPatternExample : IGildedRoseApp { + #region Variables + private IList Items; + #endregion + + #region Constructor + public GildedRoseStrategyPatternExample(IList Items) { this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList(); } + #endregion + + #region Methods + public void UpdateQuality() { for (var i = 0; i < Items.Count; i++) @@ -20,5 +33,7 @@ namespace csharp.StrategyPatternExample Items[i].UpdateQuality(); } } + + #endregion } } diff --git a/csharp/StrategyPatternExample/ICategoryStrategy.cs b/csharp/StrategyPatternExample/ICategoryStrategy.cs index 417cf7d4..464ee381 100644 --- a/csharp/StrategyPatternExample/ICategoryStrategy.cs +++ b/csharp/StrategyPatternExample/ICategoryStrategy.cs @@ -2,6 +2,9 @@ namespace csharp.StrategyPatternExample { + /// + /// Interface that every category strategy will have to implement. + /// interface ICategoryStrategy { void Update(Item item); diff --git a/csharp/StrategyPatternExample/ItemWrapperContext.cs b/csharp/StrategyPatternExample/ItemWrapperContext.cs index 2eca78b8..441e82db 100644 --- a/csharp/StrategyPatternExample/ItemWrapperContext.cs +++ b/csharp/StrategyPatternExample/ItemWrapperContext.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; namespace csharp.StrategyPatternExample { + /// + /// Wrapper class to allow to create a context for every defined item and thus to be able to bind a strategy. + /// public class ItemWrapperContext { #region Variables diff --git a/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs b/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs index 53605b26..04d05877 100644 --- a/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs +++ b/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; namespace csharp.StrategyPatternExample.Strategy { + /// + /// Implements the strategy; Increases in Quality as its SellIn value approaches. + /// internal class CloseExpiredImproveQualityStrategy : ICategoryStrategy { #region subclasses diff --git a/csharp/StrategyPatternExample/Strategies/DoNothingStrategy.cs b/csharp/StrategyPatternExample/Strategies/DoNothingStrategy.cs new file mode 100644 index 00000000..b6c813ac --- /dev/null +++ b/csharp/StrategyPatternExample/Strategies/DoNothingStrategy.cs @@ -0,0 +1,18 @@ +using System; + +namespace csharp.StrategyPatternExample.Strategy +{ + /// + /// Implements the strategy; Do nothing. No action is implemented. + /// + internal class DoNothingStrategy : ICategoryStrategy + { + #region Methods + + public void Update(Item item) + { + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs b/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs index 2e1d823b..1b505363 100644 --- a/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs +++ b/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs @@ -2,8 +2,14 @@ namespace csharp.StrategyPatternExample.Strategy { + /// + /// Implements the strategy; SellIn and Quality values are lowered. + /// Main strategy for the most items. + /// internal class NormalDegradeStrategy : ICategoryStrategy { + #region Methods + public void Update(Item item) { if (item.Quality > Global.MINIMUM_QUALITY) @@ -16,7 +22,9 @@ namespace csharp.StrategyPatternExample.Strategy if (item.SellIn < 0 && item.Quality > Global.MINIMUM_QUALITY) { item.Quality--; - } + } } + + #endregion } } diff --git a/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs b/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs index 688d7e07..bc55a0cf 100644 --- a/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs +++ b/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs @@ -2,8 +2,13 @@ namespace csharp.StrategyPatternExample.Strategy { + /// + /// Implements the strategy; Increase in Quality the older it gets. + /// internal class OlderIsBetterStrategy : ICategoryStrategy { + #region Methods + public void Update(Item item) { if (item.Quality < Global.MAXIMUM_QUALITY) @@ -18,5 +23,7 @@ namespace csharp.StrategyPatternExample.Strategy item.Quality++; } } + + #endregion } } diff --git a/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs b/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs index 4f71d653..2082f2ce 100644 --- a/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs +++ b/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs @@ -2,8 +2,13 @@ namespace csharp.StrategyPatternExample.Strategy { + /// + /// Implements the strategy; Items degrade in Quality twice as fast as normal items. + /// internal class TwiceFastDegradeQualityStrategy : ICategoryStrategy { + #region Methods + public void Update(Item item) { int degrade = 2; @@ -23,7 +28,9 @@ namespace csharp.StrategyPatternExample.Strategy if (item.Quality < Global.MINIMUM_QUALITY) { item.Quality = Global.MINIMUM_QUALITY; - } + } } + + #endregion } } diff --git a/csharp/csharp.csproj b/csharp/csharp.csproj index e6ca86ce..adf3ae16 100644 --- a/csharp/csharp.csproj +++ b/csharp/csharp.csproj @@ -76,6 +76,7 @@ +