mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-07-27 01:31:23 +00:00
Pure file move + namespace alignment, no logic changes: - Interfaces/ IItemUpdater, IItemUpdaterFactory (GildedRoseKata.Interfaces) - Factories/ ItemUpdaterFactory incl. static Default() (GildedRoseKata.Factories) - Strategies/ ItemUpdaterBase + the 5 concrete updaters (GildedRoseKata.Strategies) GildedRose.cs changes only in usings; Item.cs and Program.cs untouched. Full suite 24/24 green; approval snapshot byte-identical. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using GildedRoseKata.Interfaces;
|
|
using GildedRoseKata.Strategies;
|
|
|
|
namespace GildedRoseKata.Factories;
|
|
|
|
public class ItemUpdaterFactory : IItemUpdaterFactory
|
|
{
|
|
public const string ConjuredPrefix = "Conjured";
|
|
private const string DefaultName = "default";
|
|
|
|
private readonly Dictionary<string, IItemUpdater> _updatersByName;
|
|
private readonly IItemUpdater _fallback;
|
|
|
|
public ItemUpdaterFactory(IEnumerable<IItemUpdater> updaters)
|
|
{
|
|
_updatersByName = new Dictionary<string, IItemUpdater>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (var updater in updaters)
|
|
{
|
|
_updatersByName[updater.Name] = updater;
|
|
}
|
|
|
|
if (!_updatersByName.TryGetValue(DefaultName, out _fallback))
|
|
{
|
|
throw new ArgumentException($"A \"{DefaultName}\" fallback updater is required.", nameof(updaters));
|
|
}
|
|
}
|
|
|
|
public static ItemUpdaterFactory Default() => new(new IItemUpdater[]
|
|
{
|
|
new NormalItemUpdater(),
|
|
new AgedBrieUpdater(),
|
|
new SulfurasUpdater(),
|
|
new BackstagePassUpdater(),
|
|
new ConjuredItemUpdater(),
|
|
});
|
|
|
|
public IItemUpdater Get(Item item)
|
|
{
|
|
if (_updatersByName.TryGetValue(item.Name, out var updater))
|
|
{
|
|
return updater;
|
|
}
|
|
|
|
if (item.Name.StartsWith(ConjuredPrefix, StringComparison.OrdinalIgnoreCase) &&
|
|
_updatersByName.TryGetValue(ConjuredPrefix, out var conjured))
|
|
{
|
|
return conjured;
|
|
}
|
|
|
|
return _fallback;
|
|
}
|
|
}
|