mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-14 22:21:20 +00:00
DailyUpdaterFactory reuse existing updaters instead of creating new ones every time.
This commit is contained in:
parent
7fbb554030
commit
9293c86c66
@ -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<ItemType, DailyUpdater> _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<DailyUpdater> createDailyUpdater)
|
||||
{
|
||||
if (!_dailyUpdaters.ContainsKey(itemType))
|
||||
{
|
||||
_dailyUpdaters.Add(itemType, createDailyUpdater());
|
||||
}
|
||||
|
||||
return _dailyUpdaters[itemType];
|
||||
}
|
||||
|
||||
private static bool IsLegendaryItem(Item item) => item.Name.ToLower().Contains("sulfuras");
|
||||
|
||||
@ -5,17 +5,21 @@ namespace GildedRoseKata;
|
||||
public class GildedRose
|
||||
{
|
||||
private readonly IList<Item> _items;
|
||||
private DailyUpdaterFactory _dailyUpdaterFactory;
|
||||
|
||||
public GildedRose(IList<Item> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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.<br>
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user