mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-14 22:21:20 +00:00
Extract logic to create Updater into DailyUpdaterFactory and some minor refactoring in DailyUpdater.cs
This commit is contained in:
parent
a585b181da
commit
7fbb554030
@ -4,13 +4,17 @@ namespace GildedRoseKata;
|
|||||||
|
|
||||||
public abstract class DailyUpdater
|
public abstract class DailyUpdater
|
||||||
{
|
{
|
||||||
protected const int MinQuality = 0;
|
private const int MinQuality = 0;
|
||||||
protected const int MaxQuality = 50;
|
private const int MaxQuality = 50;
|
||||||
|
|
||||||
public void DailyUpdate(Item item)
|
public void DailyUpdate(Item item)
|
||||||
{
|
{
|
||||||
UpdateSellIn(item);
|
UpdateSellIn(item);
|
||||||
UpdateQuality(item);
|
UpdateQuality(item);
|
||||||
|
if (IsExpired(item))
|
||||||
|
{
|
||||||
|
UpdateQuality(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void UpdateQuality(Item item);
|
public abstract void UpdateQuality(Item item);
|
||||||
@ -18,28 +22,22 @@ public abstract class DailyUpdater
|
|||||||
|
|
||||||
protected static bool IsExpired(Item item) => item.SellIn < 0;
|
protected static bool IsExpired(Item item) => item.SellIn < 0;
|
||||||
|
|
||||||
protected static void IncreaseQuality(Item item, int byValue)
|
protected static void IncreaseQuality(Item item, int byValue = 1)
|
||||||
{
|
{
|
||||||
item.Quality = int.Min(item.Quality + byValue, MaxQuality);
|
item.Quality = int.Min(item.Quality + byValue, MaxQuality);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void DecreaseQuality(Item item, int byValue)
|
protected static void DecreaseQuality(Item item, int byValue = 1)
|
||||||
{
|
{
|
||||||
item.Quality = int.Max(item.Quality - byValue, MinQuality);
|
item.Quality = int.Max(item.Quality - byValue, MinQuality);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DailyUpdaterForRegularItems : DailyUpdater
|
public class DailyUpdaterForRegularItems : DailyUpdater
|
||||||
{
|
{
|
||||||
public override void UpdateQuality(Item item)
|
public override void UpdateQuality(Item item)
|
||||||
{
|
{
|
||||||
DecreaseQuality(item, 1);
|
DecreaseQuality(item);
|
||||||
if (IsExpired(item))
|
|
||||||
{
|
|
||||||
DecreaseQuality(item, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,11 +45,7 @@ public class DailyUpdaterForBetterWithAgeItems : DailyUpdater
|
|||||||
{
|
{
|
||||||
public override void UpdateQuality(Item item)
|
public override void UpdateQuality(Item item)
|
||||||
{
|
{
|
||||||
IncreaseQuality(item, 1);
|
IncreaseQuality(item);
|
||||||
if (IsExpired(item))
|
|
||||||
{
|
|
||||||
IncreaseQuality(item, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +56,7 @@ public class DailyUpdaterForBackstagePassesItems : DailyUpdater
|
|||||||
{
|
{
|
||||||
if (item.SellIn > 9)
|
if (item.SellIn > 9)
|
||||||
{
|
{
|
||||||
IncreaseQuality(item, 1);
|
IncreaseQuality(item);
|
||||||
}
|
}
|
||||||
else if (item.SellIn > 4)
|
else if (item.SellIn > 4)
|
||||||
{
|
{
|
||||||
|
|||||||
31
csharpcore/GildedRose/DailyUpdaterFactory.cs
Normal file
31
csharpcore/GildedRose/DailyUpdaterFactory.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
namespace GildedRoseKata;
|
||||||
|
|
||||||
|
public static class DailyUpdaterFactory
|
||||||
|
{
|
||||||
|
public static DailyUpdater GetDailyUpdater(Item item)
|
||||||
|
{
|
||||||
|
if (IsLegendaryItem(item))
|
||||||
|
{
|
||||||
|
return new DailyUpdaterForLegendaryItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsBetterWithAgeItem(item))
|
||||||
|
{
|
||||||
|
return new DailyUpdaterForBetterWithAgeItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsBackstagePassesItem(item))
|
||||||
|
{
|
||||||
|
return new DailyUpdaterForBackstagePassesItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DailyUpdaterForRegularItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsLegendaryItem(Item item) => item.Name.ToLower().Contains("sulfuras");
|
||||||
|
|
||||||
|
private static bool IsBackstagePassesItem(Item item) => item.Name.ToLower().Contains("backstage passes");
|
||||||
|
|
||||||
|
private static bool IsBetterWithAgeItem(Item item) => item.Name.ToLower().Equals("aged brie");
|
||||||
|
|
||||||
|
}
|
||||||
@ -5,8 +5,6 @@ namespace GildedRoseKata;
|
|||||||
public class GildedRose
|
public class GildedRose
|
||||||
{
|
{
|
||||||
private readonly IList<Item> _items;
|
private readonly IList<Item> _items;
|
||||||
const int MinQuality = 0;
|
|
||||||
const int MaxQuality = 50;
|
|
||||||
|
|
||||||
public GildedRose(IList<Item> items)
|
public GildedRose(IList<Item> items)
|
||||||
{
|
{
|
||||||
@ -17,34 +15,9 @@ public class GildedRose
|
|||||||
{
|
{
|
||||||
foreach (var item in _items)
|
foreach (var item in _items)
|
||||||
{
|
{
|
||||||
DailyUpdater dailyUpdater = getDailyUpdater(item);
|
DailyUpdater dailyUpdater = DailyUpdaterFactory.GetDailyUpdater(item);
|
||||||
dailyUpdater.DailyUpdate(item);
|
dailyUpdater.DailyUpdate(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsLegendaryItem(Item item) => item.Name.ToLower().Contains("sulfuras");
|
|
||||||
|
|
||||||
private static bool IsBackstagePassesItem(Item item) => item.Name.ToLower().Contains("backstage passes");
|
|
||||||
|
|
||||||
private static bool IsBetterWithAgeItem(Item item) => item.Name.ToLower().Equals("aged brie");
|
|
||||||
|
|
||||||
private DailyUpdater getDailyUpdater(Item item)
|
|
||||||
{
|
|
||||||
if (IsLegendaryItem(item))
|
|
||||||
{
|
|
||||||
return new DailyUpdaterForLegendaryItems();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsBetterWithAgeItem(item))
|
|
||||||
{
|
|
||||||
return new DailyUpdaterForBetterWithAgeItems();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsBackstagePassesItem(item))
|
|
||||||
{
|
|
||||||
return new DailyUpdaterForBackstagePassesItems();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DailyUpdaterForRegularItems();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -16,7 +16,9 @@ I decorated these tests with `[Ignore]` until I get to a stage in the refactorin
|
|||||||
5. Used the `Template` design pattern to create DailyUpdater abstract class and derived classes to handle the DailyUpdate's steps for the different types of items.<br>
|
5. Used the `Template` design pattern to create DailyUpdater abstract class and derived classes to handle the DailyUpdate's steps for the different types of items.<br>
|
||||||
The different derived classes will be moved to their own files in the next PR. Left them together for now to show the steps.
|
The different derived classes will be moved to their own files in the next PR. Left them together for now to show the steps.
|
||||||
6. Created a `Simple Factory` method to return the appropriate DailyUpdater object for the item type.<br>
|
6. Created a `Simple Factory` method to return the appropriate DailyUpdater object for the item type.<br>
|
||||||
For this PR, I left this method in the GildedRose.cp to show the steps. But in the next PR I'll move it into its own class.
|
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.
|
||||||
|
|
||||||
|
|
||||||
This is the state of the code now
|
This is the state of the code now
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user