mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-07-27 01:31:23 +00:00
Introduce the Strategy + Factory design: IItemUpdater (Name + Update), an ItemUpdaterBase with the single shared [0,50] clamp helper, stateless strategies for normal/Aged Brie/Sulfuras/Backstage, and a factory with two-tier resolution (exact name via case-insensitive dictionary, then Conjured prefix rule, then the default fallback). Not wired into GildedRose yet; no behaviour change. The Conjured strategy itself arrives in the feature commit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace GildedRoseKata;
|
|
|
|
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(),
|
|
});
|
|
|
|
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;
|
|
}
|
|
}
|