GildedRose-Refactoring-Kata/csharp.xUnit/GildedRose/ItemUpdaterFactory.cs
Artur Goloyad 8965637f18 refactor: add IItemUpdater strategies and ItemUpdaterFactory
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>
2026-07-25 20:50:18 +02:00

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;
}
}