From 9293c86c664ec4687d17e15f49c519c335bbf58c Mon Sep 17 00:00:00 2001 From: Sarah Ashri Date: Fri, 15 Mar 2024 09:03:42 +1000 Subject: [PATCH] DailyUpdaterFactory reuse existing updaters instead of creating new ones every time. --- csharpcore/GildedRose/DailyUpdaterFactory.cs | 37 ++++++++++++++++---- csharpcore/GildedRose/GildedRose.cs | 6 +++- csharpcore/GildedRose/RefactoringDiary.md | 4 +++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/csharpcore/GildedRose/DailyUpdaterFactory.cs b/csharpcore/GildedRose/DailyUpdaterFactory.cs index 75077d3b..47efd08f 100644 --- a/csharpcore/GildedRose/DailyUpdaterFactory.cs +++ b/csharpcore/GildedRose/DailyUpdaterFactory.cs @@ -1,25 +1,48 @@ +using System; +using System.Collections.Generic; + namespace GildedRoseKata; -public static class DailyUpdaterFactory +public class DailyUpdaterFactory { - public static DailyUpdater GetDailyUpdater(Item item) + private enum ItemType + { + Regular, + Legendary, + BetterWithAge, + BackstagePasses + } + + private readonly Dictionary _dailyUpdaters = new(); + + public DailyUpdater GetDailyUpdater(Item item) { if (IsLegendaryItem(item)) { - return new DailyUpdaterForLegendaryItems(); + return GetOrCreateDailyUpdater(ItemType.Legendary, () => new DailyUpdaterForLegendaryItems()); } if (IsBetterWithAgeItem(item)) { - return new DailyUpdaterForBetterWithAgeItems(); + return GetOrCreateDailyUpdater(ItemType.BetterWithAge, () => new DailyUpdaterForBetterWithAgeItems()); } - if (IsBackstagePassesItem(item)) + if(IsBackstagePassesItem(item)) { - return new DailyUpdaterForBackstagePassesItems(); + return GetOrCreateDailyUpdater(ItemType.BackstagePasses, () => new DailyUpdaterForBackstagePassesItems()); } - return new DailyUpdaterForRegularItems(); + return GetOrCreateDailyUpdater(ItemType.Regular, () => new DailyUpdaterForRegularItems()); + } + + private DailyUpdater GetOrCreateDailyUpdater(ItemType itemType, Func createDailyUpdater) + { + if (!_dailyUpdaters.ContainsKey(itemType)) + { + _dailyUpdaters.Add(itemType, createDailyUpdater()); + } + + return _dailyUpdaters[itemType]; } private static bool IsLegendaryItem(Item item) => item.Name.ToLower().Contains("sulfuras"); diff --git a/csharpcore/GildedRose/GildedRose.cs b/csharpcore/GildedRose/GildedRose.cs index d89324b3..d0c3ce92 100644 --- a/csharpcore/GildedRose/GildedRose.cs +++ b/csharpcore/GildedRose/GildedRose.cs @@ -5,17 +5,21 @@ namespace GildedRoseKata; public class GildedRose { private readonly IList _items; + private DailyUpdaterFactory _dailyUpdaterFactory; public GildedRose(IList items) { _items = items; + // The DailyUpdaterFactory should have been injected through the constructor. + // However, this will require changing the supporting tests which we don't want to do at this stage. + _dailyUpdaterFactory = new DailyUpdaterFactory(); } public void UpdateQuality() { foreach (var item in _items) { - DailyUpdater dailyUpdater = DailyUpdaterFactory.GetDailyUpdater(item); + DailyUpdater dailyUpdater = _dailyUpdaterFactory.GetDailyUpdater(item); dailyUpdater.DailyUpdate(item); } } diff --git a/csharpcore/GildedRose/RefactoringDiary.md b/csharpcore/GildedRose/RefactoringDiary.md index 750f821c..94778471 100644 --- a/csharpcore/GildedRose/RefactoringDiary.md +++ b/csharpcore/GildedRose/RefactoringDiary.md @@ -19,6 +19,10 @@ The different derived classes will be moved to their own files in the next PR. L 7. Moved DailyUpdaterFactory to its own class. 8. Moved the logic of "Once the sell by date has passed, Quality degrades twice as fast" into the DailyUpdater Template class since it's relevant to all types of items. 9. Moved Decrease/IncreaseItem byValue to be 1 by default. +10. Refactored DailyUpdaterFactory to reuse existing Updaters instead of creating new ones every time.
+This could have been done using a dependency injection framework as well (to automatically create and reuse all the updaters). This might actually be the better way to do this. +However, I've chosen to use the factory itself to manually manage it using a semi-flyweight pattern. + This is the state of the code now