From 44cf4d4eb1e21289365c868fef29c92b02e41a36 Mon Sep 17 00:00:00 2001 From: israel Date: Fri, 20 Oct 2017 13:25:16 +0100 Subject: [PATCH] Corrected ItemWrapperContext to implement strategy pattern correctly. --- .../CategoryStrategiesFactory.cs | 78 ++++--------------- .../ItemWrapperContext.cs | 10 +-- 2 files changed, 20 insertions(+), 68 deletions(-) diff --git a/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs index 45dac99a..d917511d 100644 --- a/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs +++ b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs @@ -7,49 +7,19 @@ namespace csharp.StrategyPatternExample /// /// This class is responsible for providing a category strategy to a given item. /// - internal class CategoryStrategiesFactory + static internal class CategoryStrategiesFactory { - #region Variables - - static private CategoryStrategiesFactory _strategiesFactory = null; - - #endregion - - #region Constructor - - private CategoryStrategiesFactory() { } - - #endregion - #region Methods - public static CategoryStrategiesFactory GetInstance() + static public ICategoryStrategy GetCategoryStrategies(Item item) { - if (_strategiesFactory == null) + switch (item.Name) { - _strategiesFactory = new CategoryStrategiesFactory(); - } + case Global.NAME_ITEM_AGED_BRIE: + return new OlderIsBetterStrategy(); - return _strategiesFactory; - } - - public List GetCategoryStrategies(Item item) - { - List listCategoryStrategies = new List(); - - if (item.Name == Global.NAME_ITEM_AGED_BRIE) - { - listCategoryStrategies = new List() - { - new OlderIsBetterStrategy() - }; - - } - else if (item.Name == Global.NAME_ITEM_BACKSTAGE_PASSES) - { - listCategoryStrategies = new List() - { - new CloseExpiredImproveQualityStrategy(new List() { + case Global.NAME_ITEM_BACKSTAGE_PASSES: + return new CloseExpiredImproveQualityStrategy(new List() { new CloseExpiredImproveQualityStrategy.NextExpiredCondition() { SellInLimit = 5, @@ -60,31 +30,17 @@ namespace csharp.StrategyPatternExample SellInLimit = 10, Increment = 2 } - }) - }; - } - else if (item.Name == Global.NAME_ITEM_SULFURAS) - { - listCategoryStrategies = new List() - { - }; - } - else if (item.Name == Global.NAME_ITEM_CONJURED) - { - listCategoryStrategies = new List() - { - new TwiceFastDegradeQualityStrategy() - }; - } - else - { - listCategoryStrategies = new List() - { - new NormalDegradeStrategy() - }; - } + }); - return listCategoryStrategies; + case Global.NAME_ITEM_SULFURAS: + return new DoNothingStrategy(); + + case Global.NAME_ITEM_CONJURED: + return new TwiceFastDegradeQualityStrategy(); + + default: + return new NormalDegradeStrategy(); + } } #endregion diff --git a/csharp/StrategyPatternExample/ItemWrapperContext.cs b/csharp/StrategyPatternExample/ItemWrapperContext.cs index 441e82db..08322a7b 100644 --- a/csharp/StrategyPatternExample/ItemWrapperContext.cs +++ b/csharp/StrategyPatternExample/ItemWrapperContext.cs @@ -11,7 +11,7 @@ namespace csharp.StrategyPatternExample #region Variables private Item _item; - private List listCategoryStrategies; + private ICategoryStrategy strategy; #endregion @@ -42,8 +42,7 @@ namespace csharp.StrategyPatternExample public ItemWrapperContext(Item item) { this._item = item; - - listCategoryStrategies = CategoryStrategiesFactory.GetInstance().GetCategoryStrategies(item); + this.strategy = CategoryStrategiesFactory.GetCategoryStrategies(item); } #endregion @@ -52,10 +51,7 @@ namespace csharp.StrategyPatternExample public void UpdateQuality() { - foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies) - { - categoryStrategyItem.Update(this._item); - } + strategy.Update(this._item); } #endregion